From 03d0be13e88ad3b32a939896f1172710ab85586e Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Sun, 5 Mar 2023 15:26:51 -0500 Subject: [PATCH] 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. --- Userland/Libraries/LibWeb/WebDriver/Error.cpp | 25 ++++++++++++++++--- Userland/Libraries/LibWeb/WebDriver/Error.h | 6 +++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/Userland/Libraries/LibWeb/WebDriver/Error.cpp b/Userland/Libraries/LibWeb/WebDriver/Error.cpp index a9982e8a24..6073a73fdc 100644 --- a/Userland/Libraries/LibWeb/WebDriver/Error.cpp +++ b/Userland/Libraries/LibWeb/WebDriver/Error.cpp @@ -4,6 +4,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include #include #include @@ -45,17 +46,33 @@ static Vector const s_error_code_data = { { ErrorCode::UnknownError, 500, "unknown error" }, { ErrorCode::UnknownMethod, 405, "unknown method" }, { ErrorCode::UnsupportedOperation, 500, "unsupported operation" }, + { ErrorCode::OutOfMemory, 500, "out of memory" }, }; Error Error::from_code(ErrorCode code, DeprecatedString message, Optional data) { auto const& error_code_data = s_error_code_data[to_underlying(code)]; + return { - .http_status = error_code_data.http_status, - .error = error_code_data.json_error_code, - .message = move(message), - .data = move(data) + error_code_data.http_status, + error_code_data.json_error_code, + move(message), + 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 data_) + : http_status(http_status_) + , error(move(error_)) + , message(move(message_)) + , data(move(data_)) +{ +} + } diff --git a/Userland/Libraries/LibWeb/WebDriver/Error.h b/Userland/Libraries/LibWeb/WebDriver/Error.h index 13e37b8d8d..fd5aaec2d3 100644 --- a/Userland/Libraries/LibWeb/WebDriver/Error.h +++ b/Userland/Libraries/LibWeb/WebDriver/Error.h @@ -42,6 +42,9 @@ enum class ErrorCode { UnknownError, UnknownMethod, UnsupportedOperation, + + // Non-standard error codes: + OutOfMemory, }; // https://w3c.github.io/webdriver/#errors @@ -52,6 +55,9 @@ struct Error { Optional data; static Error from_code(ErrorCode, DeprecatedString message, Optional data = {}); + + Error(unsigned http_status, DeprecatedString error, DeprecatedString message, Optional data); + Error(AK::Error const&); }; }