From 32e0f0aec8ab575789b8db01932b5ae05b15e64f Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sun, 23 Oct 2022 19:10:17 +0200 Subject: [PATCH] LibWeb: Require error message for Response::network_error() There will be a lot of different cases where we'll return an error response, and having a customized Promise rejection message seems quite useful. Note that this has to be distinct from the existing 'status message', which is required to be empty in those cases. --- .../LibWeb/Fetch/Infrastructure/HTTP/Responses.cpp | 7 ++++--- .../LibWeb/Fetch/Infrastructure/HTTP/Responses.h | 9 +++++++-- Userland/Libraries/LibWeb/Fetch/Response.cpp | 2 +- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.cpp b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.cpp index 679bf100db..b6bc986003 100644 --- a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.cpp +++ b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.cpp @@ -36,17 +36,18 @@ JS::NonnullGCPtr Response::create(JS::VM& vm) JS::NonnullGCPtr Response::aborted_network_error(JS::VM& vm) { - auto response = network_error(vm); + auto response = network_error(vm, "Fetch has been aborted"sv); response->set_aborted(true); return response; } -JS::NonnullGCPtr Response::network_error(JS::VM& vm) +JS::NonnullGCPtr Response::network_error(JS::VM& vm, String message) { auto response = Response::create(vm); response->set_status(0); response->set_type(Type::Error); VERIFY(!response->body().has_value()); + response->m_network_error_message = move(message); return response; } @@ -59,7 +60,7 @@ JS::NonnullGCPtr Response::appropriate_network_error(JS::VM& vm, Fetch // 2. Return an aborted network error if fetchParams is aborted; otherwise return a network error. return fetch_params.is_aborted() ? aborted_network_error(vm) - : network_error(vm); + : network_error(vm, "Fetch has been terminated"sv); } // https://fetch.spec.whatwg.org/#concept-aborted-network-error diff --git a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h index acfc2ad326..7a01f22c1b 100644 --- a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h +++ b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h @@ -50,7 +50,7 @@ public: [[nodiscard]] static JS::NonnullGCPtr create(JS::VM&); [[nodiscard]] static JS::NonnullGCPtr aborted_network_error(JS::VM&); - [[nodiscard]] static JS::NonnullGCPtr network_error(JS::VM&); + [[nodiscard]] static JS::NonnullGCPtr network_error(JS::VM&, String message); [[nodiscard]] static JS::NonnullGCPtr appropriate_network_error(JS::VM&, FetchParams const&); virtual ~Response() = default; @@ -107,6 +107,9 @@ public: [[nodiscard]] WebIDL::ExceptionOr> clone(JS::VM&) const; + // Non-standard + Optional const& network_error_message() const { return m_network_error_message; } + protected: explicit Response(JS::NonnullGCPtr); @@ -171,6 +174,9 @@ private: // https://fetch.spec.whatwg.org/#response-has-cross-origin-redirects // A response has an associated has-cross-origin-redirects (a boolean), which is initially false. bool m_has_cross_origin_redirects { false }; + + // Non-standard + Optional m_network_error_message; }; // https://fetch.spec.whatwg.org/#concept-filtered-response @@ -291,5 +297,4 @@ private: JS::NonnullGCPtr m_header_list; Optional m_body; }; - } diff --git a/Userland/Libraries/LibWeb/Fetch/Response.cpp b/Userland/Libraries/LibWeb/Fetch/Response.cpp index 286846adbc..cd5151be47 100644 --- a/Userland/Libraries/LibWeb/Fetch/Response.cpp +++ b/Userland/Libraries/LibWeb/Fetch/Response.cpp @@ -154,7 +154,7 @@ JS::NonnullGCPtr Response::error(JS::VM& vm) { // The static error() method steps are to return the result of creating a Response object, given a new network error, "immutable", and this’s relevant Realm. // FIXME: How can we reliably get 'this', i.e. the object the function was called on, in IDL-defined functions? - return Response::create(*vm.current_realm(), Infrastructure::Response::network_error(vm), Headers::Guard::Immutable); + return Response::create(*vm.current_realm(), Infrastructure::Response::network_error(vm, "Response created via `Response.error()`"sv), Headers::Guard::Immutable); } // https://fetch.spec.whatwg.org/#dom-response-redirect