1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 03:37:43 +00:00

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.
This commit is contained in:
Linus Groh 2022-10-23 19:10:17 +02:00
parent d8ebdef010
commit 32e0f0aec8
3 changed files with 12 additions and 6 deletions

View file

@ -36,17 +36,18 @@ JS::NonnullGCPtr<Response> Response::create(JS::VM& vm)
JS::NonnullGCPtr<Response> 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> Response::network_error(JS::VM& vm)
JS::NonnullGCPtr<Response> 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> 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

View file

@ -50,7 +50,7 @@ public:
[[nodiscard]] static JS::NonnullGCPtr<Response> create(JS::VM&);
[[nodiscard]] static JS::NonnullGCPtr<Response> aborted_network_error(JS::VM&);
[[nodiscard]] static JS::NonnullGCPtr<Response> network_error(JS::VM&);
[[nodiscard]] static JS::NonnullGCPtr<Response> network_error(JS::VM&, String message);
[[nodiscard]] static JS::NonnullGCPtr<Response> appropriate_network_error(JS::VM&, FetchParams const&);
virtual ~Response() = default;
@ -107,6 +107,9 @@ public:
[[nodiscard]] WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> clone(JS::VM&) const;
// Non-standard
Optional<String> const& network_error_message() const { return m_network_error_message; }
protected:
explicit Response(JS::NonnullGCPtr<HeaderList>);
@ -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<String> m_network_error_message;
};
// https://fetch.spec.whatwg.org/#concept-filtered-response
@ -291,5 +297,4 @@ private:
JS::NonnullGCPtr<HeaderList> m_header_list;
Optional<Body> m_body;
};
}

View file

@ -154,7 +154,7 @@ JS::NonnullGCPtr<Response> 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 thiss 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