1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 19:27:44 +00:00

LibWeb: Allow constructing a WebDriver::Error from an OOM AK::Error

This will allow easily surrounding operations that may fail due to OOM
with TRY. Note that we now also have to define a "normal" constructor
for WebDriver::Error in order to add the AK::Error constructor.
This commit is contained in:
Timothy Flynn 2023-03-05 15:26:51 -05:00 committed by Linus Groh
parent a7bb72a3d6
commit 03d0be13e8
2 changed files with 27 additions and 4 deletions

View file

@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include <AK/Error.h>
#include <AK/Vector.h> #include <AK/Vector.h>
#include <LibWeb/WebDriver/Error.h> #include <LibWeb/WebDriver/Error.h>
@ -45,17 +46,33 @@ static Vector<ErrorCodeData> const s_error_code_data = {
{ ErrorCode::UnknownError, 500, "unknown error" }, { ErrorCode::UnknownError, 500, "unknown error" },
{ ErrorCode::UnknownMethod, 405, "unknown method" }, { ErrorCode::UnknownMethod, 405, "unknown method" },
{ ErrorCode::UnsupportedOperation, 500, "unsupported operation" }, { ErrorCode::UnsupportedOperation, 500, "unsupported operation" },
{ ErrorCode::OutOfMemory, 500, "out of memory" },
}; };
Error Error::from_code(ErrorCode code, DeprecatedString message, Optional<JsonValue> data) Error Error::from_code(ErrorCode code, DeprecatedString message, Optional<JsonValue> data)
{ {
auto const& error_code_data = s_error_code_data[to_underlying(code)]; auto const& error_code_data = s_error_code_data[to_underlying(code)];
return { return {
.http_status = error_code_data.http_status, error_code_data.http_status,
.error = error_code_data.json_error_code, error_code_data.json_error_code,
.message = move(message), move(message),
.data = move(data) move(data)
}; };
} }
Error::Error(AK::Error const& error)
{
VERIFY(error.code() == ENOMEM);
*this = from_code(ErrorCode::OutOfMemory, {}, {});
}
Error::Error(unsigned http_status_, DeprecatedString error_, DeprecatedString message_, Optional<JsonValue> data_)
: http_status(http_status_)
, error(move(error_))
, message(move(message_))
, data(move(data_))
{
}
} }

View file

@ -42,6 +42,9 @@ enum class ErrorCode {
UnknownError, UnknownError,
UnknownMethod, UnknownMethod,
UnsupportedOperation, UnsupportedOperation,
// Non-standard error codes:
OutOfMemory,
}; };
// https://w3c.github.io/webdriver/#errors // https://w3c.github.io/webdriver/#errors
@ -52,6 +55,9 @@ struct Error {
Optional<JsonValue> data; Optional<JsonValue> data;
static Error from_code(ErrorCode, DeprecatedString message, Optional<JsonValue> data = {}); static Error from_code(ErrorCode, DeprecatedString message, Optional<JsonValue> data = {});
Error(unsigned http_status, DeprecatedString error, DeprecatedString message, Optional<JsonValue> data);
Error(AK::Error const&);
}; };
} }