1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 15:37:46 +00:00

Everywhere: Convert ByteBuffer factory methods from Optional -> ErrorOr

Apologies for the enormous commit, but I don't see a way to split this
up nicely. In the vast majority of cases it's a simple change. A few
extra places can use TRY instead of manual error checking though. :^)
This commit is contained in:
Sam Atkins 2022-01-20 17:47:39 +00:00 committed by Andreas Kling
parent 140f1d9e55
commit 45cf40653a
79 changed files with 202 additions and 274 deletions

View file

@ -109,17 +109,17 @@ Optional<ByteBuffer> get_buffer_source_copy(JS::Object const& buffer_source)
// 8. Let bytes be a new byte sequence of length equal to length.
auto bytes = ByteBuffer::create_zeroed(length);
if (!bytes.has_value())
if (bytes.is_error())
return {};
// 9. For i in the range offset to offset + length 1, inclusive, set bytes[i offset] to ! GetValueFromBuffer(esArrayBuffer, i, Uint8, true, Unordered).
for (u64 i = offset; i <= offset + length - 1; ++i) {
auto value = es_array_buffer->get_value<u8>(i, true, JS::ArrayBuffer::Unordered);
(*bytes)[i - offset] = (u8)value.as_u32();
bytes.value()[i - offset] = (u8)value.as_u32();
}
// 10. Return bytes.
return bytes;
return bytes.release_value();
}
}

View file

@ -64,7 +64,7 @@ JS::Promise* SubtleCrypto::digest(String const& algorithm, JS::Handle<JS::Object
auto digest = hash.digest();
auto result_buffer = ByteBuffer::copy(digest.immutable_data(), hash.digest_size());
if (!result_buffer.has_value()) {
if (result_buffer.is_error()) {
auto* error = wrap(wrapper()->global_object(), DOM::OperationError::create("Failed to create result buffer"));
promise->reject(error);
return promise;

View file

@ -69,7 +69,7 @@ void Resource::did_load(Badge<ResourceLoader>, ReadonlyBytes data, const HashMap
{
VERIFY(!m_loaded);
// FIXME: Handle OOM failure.
m_encoded_data = ByteBuffer::copy(data).release_value();
m_encoded_data = ByteBuffer::copy(data).release_value_but_fixme_should_propagate_errors();
m_response_headers = headers;
m_status_code = move(status_code);
m_loaded = true;

View file

@ -224,7 +224,7 @@ DOM::ExceptionOr<void> XMLHttpRequest::send(String body)
return;
auto& xhr = const_cast<XMLHttpRequest&>(*weak_this);
// FIXME: Handle OOM failure.
auto response_data = ByteBuffer::copy(data).release_value();
auto response_data = ByteBuffer::copy(data).release_value_but_fixme_should_propagate_errors();
// FIXME: There's currently no difference between transmitted and length.
u64 transmitted = response_data.size();
u64 length = response_data.size();