mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 03:37:45 +00:00
LibWeb/Fetch: Store Response error message as a String{,View} variant
The majority of error strings are StringView literals, and it seems
silly to require heap-allocating strings for these.
This idea is stolen from a recent change in fd1fbad
:^)
This commit is contained in:
parent
03f32bdf86
commit
1032ac2453
5 changed files with 45 additions and 37 deletions
|
@ -37,16 +37,16 @@ JS::NonnullGCPtr<Response> Response::create(JS::VM& vm)
|
|||
// A network error is a response whose status is always 0, status message is always
|
||||
// the empty byte sequence, header list is always empty, and body is always null.
|
||||
|
||||
ErrorOr<JS::NonnullGCPtr<Response>> Response::aborted_network_error(JS::VM& vm)
|
||||
JS::NonnullGCPtr<Response> Response::aborted_network_error(JS::VM& vm)
|
||||
{
|
||||
auto response = network_error(vm, TRY("Fetch has been aborted"_string));
|
||||
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, String message)
|
||||
JS::NonnullGCPtr<Response> Response::network_error(JS::VM& vm, Variant<String, StringView> message)
|
||||
{
|
||||
dbgln_if(WEB_FETCH_DEBUG, "Fetch: Creating network error response with message: {}", message);
|
||||
dbgln_if(WEB_FETCH_DEBUG, "Fetch: Creating network error response with message: {}", message.visit([](auto const& s) -> StringView { return s; }));
|
||||
auto response = Response::create(vm);
|
||||
response->set_status(0);
|
||||
response->set_type(Type::Error);
|
||||
|
@ -56,15 +56,15 @@ JS::NonnullGCPtr<Response> Response::network_error(JS::VM& vm, String message)
|
|||
}
|
||||
|
||||
// https://fetch.spec.whatwg.org/#appropriate-network-error
|
||||
ErrorOr<JS::NonnullGCPtr<Response>> Response::appropriate_network_error(JS::VM& vm, FetchParams const& fetch_params)
|
||||
JS::NonnullGCPtr<Response> Response::appropriate_network_error(JS::VM& vm, FetchParams const& fetch_params)
|
||||
{
|
||||
// 1. Assert: fetchParams is canceled.
|
||||
VERIFY(fetch_params.is_canceled());
|
||||
|
||||
// 2. Return an aborted network error if fetchParams is aborted; otherwise return a network error.
|
||||
return fetch_params.is_aborted()
|
||||
? TRY(aborted_network_error(vm))
|
||||
: network_error(vm, TRY("Fetch has been terminated"_string));
|
||||
? aborted_network_error(vm)
|
||||
: network_error(vm, "Fetch has been terminated"sv);
|
||||
}
|
||||
|
||||
// https://fetch.spec.whatwg.org/#concept-aborted-network-error
|
||||
|
@ -170,6 +170,14 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> Response::clone(JS::VM& vm) cons
|
|||
return new_response;
|
||||
}
|
||||
|
||||
// Non-standard
|
||||
Optional<StringView> Response::network_error_message() const
|
||||
{
|
||||
if (!m_network_error_message.has_value())
|
||||
return {};
|
||||
return m_network_error_message->visit([](auto const& s) -> StringView { return s; });
|
||||
}
|
||||
|
||||
FilteredResponse::FilteredResponse(JS::NonnullGCPtr<Response> internal_response, JS::NonnullGCPtr<HeaderList> header_list)
|
||||
: Response(header_list)
|
||||
, m_internal_response(internal_response)
|
||||
|
|
|
@ -50,9 +50,9 @@ public:
|
|||
};
|
||||
|
||||
[[nodiscard]] static JS::NonnullGCPtr<Response> create(JS::VM&);
|
||||
static ErrorOr<JS::NonnullGCPtr<Response>> aborted_network_error(JS::VM&);
|
||||
[[nodiscard]] static JS::NonnullGCPtr<Response> network_error(JS::VM&, String message);
|
||||
static ErrorOr<JS::NonnullGCPtr<Response>> appropriate_network_error(JS::VM&, FetchParams const&);
|
||||
[[nodiscard]] static JS::NonnullGCPtr<Response> aborted_network_error(JS::VM&);
|
||||
[[nodiscard]] static JS::NonnullGCPtr<Response> network_error(JS::VM&, Variant<String, StringView> message);
|
||||
[[nodiscard]] static JS::NonnullGCPtr<Response> appropriate_network_error(JS::VM&, FetchParams const&);
|
||||
|
||||
virtual ~Response() = default;
|
||||
|
||||
|
@ -109,7 +109,7 @@ 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; }
|
||||
[[nodiscard]] Optional<StringView> network_error_message() const;
|
||||
|
||||
protected:
|
||||
explicit Response(JS::NonnullGCPtr<HeaderList>);
|
||||
|
@ -177,7 +177,7 @@ private:
|
|||
bool m_has_cross_origin_redirects { false };
|
||||
|
||||
// Non-standard
|
||||
Optional<String> m_network_error_message;
|
||||
Optional<Variant<String, StringView>> m_network_error_message;
|
||||
};
|
||||
|
||||
// https://fetch.spec.whatwg.org/#concept-filtered-response
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue