Skip to content

Commit

Permalink
refactor(ext/web): align error messages
Browse files Browse the repository at this point in the history
Aligns the error messages in the ext/web folder to be in-line with the
Deno style guide.

#25269
  • Loading branch information
irbull committed Sep 25, 2024
1 parent 8cdb309 commit 25801fb
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 80 deletions.
8 changes: 4 additions & 4 deletions ext/web/00_infra.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ function addPaddingToBase64url(base64url) {
if (base64url.length % 4 === 2) return base64url + "==";
if (base64url.length % 4 === 3) return base64url + "=";
if (base64url.length % 4 === 1) {
throw new TypeError("Illegal base64url string!");
throw new TypeError("Illegal base64url string");
}
return base64url;
}
Expand Down Expand Up @@ -382,7 +382,7 @@ function assert(cond, msg = "Assertion failed.") {
function serializeJSValueToJSONString(value) {
const result = JSONStringify(value);
if (result === undefined) {
throw new TypeError("Value is not JSON serializable.");
throw new TypeError("Value is not JSON serializable");
}
return result;
}
Expand Down Expand Up @@ -429,7 +429,7 @@ function pathFromURLWin32(url) {
*/
function pathFromURLPosix(url) {
if (url.hostname !== "") {
throw new TypeError(`Host must be empty.`);
throw new TypeError("Host must be empty");
}

return decodeURIComponent(
Expand All @@ -444,7 +444,7 @@ function pathFromURLPosix(url) {
function pathFromURL(pathOrUrl) {
if (ObjectPrototypeIsPrototypeOf(URLPrototype, pathOrUrl)) {
if (pathOrUrl.protocol != "file:") {
throw new TypeError("Must be a file URL.");
throw new TypeError("Must be a file URL");
}

return core.build.os == "windows"
Expand Down
4 changes: 2 additions & 2 deletions ext/web/02_event.js
Original file line number Diff line number Diff line change
Expand Up @@ -1031,11 +1031,11 @@ class EventTarget {
}

if (getDispatched(event)) {
throw new DOMException("Invalid event state.", "InvalidStateError");
throw new DOMException("Invalid event state", "InvalidStateError");
}

if (event.eventPhase !== Event.NONE) {
throw new DOMException("Invalid event state.", "InvalidStateError");
throw new DOMException("Invalid event state", "InvalidStateError");
}

return dispatch(self, event);
Expand Down
2 changes: 1 addition & 1 deletion ext/web/03_abort_signal.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ class AbortSignal extends EventTarget {

constructor(key = null) {
if (key !== illegalConstructorKey) {
throw new TypeError("Illegal constructor.");
throw new TypeError("Illegal constructor");
}
super();
}
Expand Down
6 changes: 3 additions & 3 deletions ext/web/04_global_interfaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const illegalConstructorKey = Symbol("illegalConstructorKey");
class Window extends EventTarget {
constructor(key = null) {
if (key !== illegalConstructorKey) {
throw new TypeError("Illegal constructor.");
throw new TypeError("Illegal constructor");
}
super();
}
Expand All @@ -29,7 +29,7 @@ class Window extends EventTarget {
class WorkerGlobalScope extends EventTarget {
constructor(key = null) {
if (key != illegalConstructorKey) {
throw new TypeError("Illegal constructor.");
throw new TypeError("Illegal constructor");
}
super();
}
Expand All @@ -42,7 +42,7 @@ class WorkerGlobalScope extends EventTarget {
class DedicatedWorkerGlobalScope extends WorkerGlobalScope {
constructor(key = null) {
if (key != illegalConstructorKey) {
throw new TypeError("Illegal constructor.");
throw new TypeError("Illegal constructor");
}
super();
}
Expand Down
2 changes: 1 addition & 1 deletion ext/web/05_base64.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function btoa(data) {
} catch (e) {
if (ObjectPrototypeIsPrototypeOf(TypeErrorPrototype, e)) {
throw new DOMException(
"The string to be encoded contains characters outside of the Latin1 range.",
"Cannot encode string: string contains characters outside of the Latin1 range",
"InvalidCharacterError",
);
}
Expand Down
58 changes: 31 additions & 27 deletions ext/web/06_streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,10 +524,14 @@ function dequeueValue(container) {
function enqueueValueWithSize(container, value, size) {
assert(container[_queue] && typeof container[_queueTotalSize] === "number");
if (isNonNegativeNumber(size) === false) {
throw new RangeError("chunk size isn't a positive number");
throw new RangeError(
"Cannot enqueue value with size: chunk size must be a positive number",
);
}
if (size === Infinity) {
throw new RangeError("chunk size is invalid");
throw new RangeError(
"Cannot enqueue value with size: chunk size is invalid",
);
}
container[_queue].enqueue({ value, size });
container[_queueTotalSize] += size;
Expand Down Expand Up @@ -1098,7 +1102,7 @@ async function readableStreamCollectIntoUint8Array(stream) {

if (TypedArrayPrototypeGetSymbolToStringTag(chunk) !== "Uint8Array") {
throw new TypeError(
"Can't convert value to Uint8Array while consuming the stream",
"Cannot convert value to Uint8Array while consuming the stream",
);
}

Expand Down Expand Up @@ -1348,7 +1352,7 @@ function readableByteStreamControllerEnqueue(controller, chunk) {

if (isDetachedBuffer(buffer)) {
throw new TypeError(
"chunk's buffer is detached and so cannot be enqueued",
"Chunk's buffer is detached and so cannot be enqueued",
);
}
const transferredBuffer = ArrayBufferPrototypeTransferToFixedLength(buffer);
Expand Down Expand Up @@ -2096,22 +2100,22 @@ function readableByteStreamControllerRespond(controller, bytesWritten) {
if (state === "closed") {
if (bytesWritten !== 0) {
throw new TypeError(
"bytesWritten must be 0 when calling respond() on a closed stream",
`"bytesWritten" must be 0 when calling respond() on a closed stream: received ${bytesWritten}`,
);
}
} else {
assert(state === "readable");
if (bytesWritten === 0) {
throw new TypeError(
"bytesWritten must be greater than 0 when calling respond() on a readable stream",
'"bytesWritten" must be greater than 0 when calling respond() on a readable stream',
);
}
if (
(firstDescriptor.bytesFilled + bytesWritten) >
// deno-lint-ignore prefer-primordials
firstDescriptor.byteLength
) {
throw new RangeError("bytesWritten out of range");
throw new RangeError('"bytesWritten" out of range');
}
}
firstDescriptor.buffer = ArrayBufferPrototypeTransferToFixedLength(
Expand Down Expand Up @@ -2306,7 +2310,7 @@ function readableByteStreamControllerRespondWithNewView(controller, view) {
if (state === "closed") {
if (byteLength !== 0) {
throw new TypeError(
"The view's length must be 0 when calling respondWithNewView() on a closed stream",
`The view's length must be 0 when calling respondWithNewView() on a closed stream: received ${byteLength}`,
);
}
} else {
Expand Down Expand Up @@ -3578,7 +3582,7 @@ function setUpReadableByteStreamControllerFromUnderlyingSource(
}
const autoAllocateChunkSize = underlyingSourceDict["autoAllocateChunkSize"];
if (autoAllocateChunkSize === 0) {
throw new TypeError("autoAllocateChunkSize must be greater than 0");
throw new TypeError('"autoAllocateChunkSize" must be greater than 0');
}
setUpReadableByteStreamController(
stream,
Expand Down Expand Up @@ -3707,7 +3711,7 @@ function setUpReadableStreamDefaultControllerFromUnderlyingSource(
*/
function setUpReadableStreamBYOBReader(reader, stream) {
if (isReadableStreamLocked(stream)) {
throw new TypeError("ReadableStream is locked.");
throw new TypeError("ReadableStream is locked");
}
if (
!(ObjectPrototypeIsPrototypeOf(
Expand All @@ -3728,7 +3732,7 @@ function setUpReadableStreamBYOBReader(reader, stream) {
*/
function setUpReadableStreamDefaultReader(reader, stream) {
if (isReadableStreamLocked(stream)) {
throw new TypeError("ReadableStream is locked.");
throw new TypeError("ReadableStream is locked");
}
readableStreamReaderGenericInitialize(reader, stream);
reader[_readRequests] = new Queue();
Expand Down Expand Up @@ -3962,7 +3966,7 @@ function setUpWritableStreamDefaultControllerFromUnderlyingSink(
*/
function setUpWritableStreamDefaultWriter(writer, stream) {
if (isWritableStreamLocked(stream) === true) {
throw new TypeError("The stream is already locked.");
throw new TypeError("The stream is already locked");
}
writer[_stream] = stream;
stream[_writer] = writer;
Expand Down Expand Up @@ -4020,7 +4024,7 @@ function transformStreamDefaultControllerEnqueue(controller, chunk) {
/** @type {ReadableStreamDefaultController<O>} */ readableController,
) === false
) {
throw new TypeError("Readable stream is unavailable.");
throw new TypeError("Readable stream is unavailable");
}
try {
readableStreamDefaultControllerEnqueue(
Expand Down Expand Up @@ -5172,7 +5176,7 @@ class ReadableStream {
if (underlyingSourceDict.type === "bytes") {
if (strategy.size !== undefined) {
throw new RangeError(
`${prefix}: When underlying source is "bytes", strategy.size must be undefined.`,
`${prefix}: When underlying source is "bytes", strategy.size must be 'undefined'`,
);
}
const highWaterMark = extractHighWaterMark(strategy, 0);
Expand Down Expand Up @@ -5310,10 +5314,10 @@ class ReadableStream {
const { readable, writable } = transform;
const { preventClose, preventAbort, preventCancel, signal } = options;
if (isReadableStreamLocked(this)) {
throw new TypeError("ReadableStream is already locked.");
throw new TypeError("ReadableStream is already locked");
}
if (isWritableStreamLocked(writable)) {
throw new TypeError("Target WritableStream is already locked.");
throw new TypeError("Target WritableStream is already locked");
}
const promise = readableStreamPipeTo(
this,
Expand Down Expand Up @@ -5851,7 +5855,7 @@ class ReadableByteStreamController {
}
if (this[_stream][_state] !== "readable") {
throw new TypeError(
"ReadableByteStreamController's stream is not in a readable state.",
"ReadableByteStreamController's stream is not in a readable state",
);
}
readableByteStreamControllerClose(this);
Expand Down Expand Up @@ -5883,27 +5887,27 @@ class ReadableByteStreamController {
if (byteLength === 0) {
throw webidl.makeException(
TypeError,
"length must be non-zero",
"Length must be non-zero",
prefix,
arg1,
);
}
if (getArrayBufferByteLength(buffer) === 0) {
throw webidl.makeException(
TypeError,
"buffer length must be non-zero",
"Buffer length must be non-zero",
prefix,
arg1,
);
}
if (this[_closeRequested] === true) {
throw new TypeError(
"Cannot enqueue chunk after a close has been requested.",
"Cannot enqueue chunk after a close has been requested",
);
}
if (this[_stream][_state] !== "readable") {
throw new TypeError(
"Cannot enqueue chunk when underlying stream is not readable.",
"Cannot enqueue chunk when underlying stream is not readable",
);
}
return readableByteStreamControllerEnqueue(this, chunk);
Expand Down Expand Up @@ -6043,7 +6047,7 @@ class ReadableStreamDefaultController {
close() {
webidl.assertBranded(this, ReadableStreamDefaultControllerPrototype);
if (readableStreamDefaultControllerCanCloseOrEnqueue(this) === false) {
throw new TypeError("The stream controller cannot close or enqueue.");
throw new TypeError("The stream controller cannot close or enqueue");
}
readableStreamDefaultControllerClose(this);
}
Expand All @@ -6058,7 +6062,7 @@ class ReadableStreamDefaultController {
chunk = webidl.converters.any(chunk);
}
if (readableStreamDefaultControllerCanCloseOrEnqueue(this) === false) {
throw new TypeError("The stream controller cannot close or enqueue.");
throw new TypeError("The stream controller cannot close or enqueue");
}
readableStreamDefaultControllerEnqueue(this, chunk);
}
Expand Down Expand Up @@ -6183,12 +6187,12 @@ class TransformStream {
);
if (transformerDict.readableType !== undefined) {
throw new RangeError(
`${prefix}: readableType transformers not supported.`,
`${prefix}: readableType transformers not supported`,
);
}
if (transformerDict.writableType !== undefined) {
throw new RangeError(
`${prefix}: writableType transformers not supported.`,
`${prefix}: writableType transformers not supported`,
);
}
const readableHighWaterMark = extractHighWaterMark(readableStrategy, 0);
Expand Down Expand Up @@ -6393,7 +6397,7 @@ class WritableStream {
);
if (underlyingSinkDict.type != null) {
throw new RangeError(
`${prefix}: WritableStream does not support 'type' in the underlying sink.`,
`${prefix}: WritableStream does not support 'type' in the underlying sink`,
);
}
initializeWritableStream(this);
Expand Down Expand Up @@ -6520,7 +6524,7 @@ class WritableStreamDefaultWriter {
webidl.assertBranded(this, WritableStreamDefaultWriterPrototype);
if (this[_stream] === undefined) {
throw new TypeError(
"A writable stream is not associated with the writer.",
"A writable stream is not associated with the writer",
);
}
return writableStreamDefaultWriterGetDesiredSize(this);
Expand Down
2 changes: 1 addition & 1 deletion ext/web/10_filereader.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class FileReader extends EventTarget {
// 1. If fr's state is "loading", throw an InvalidStateError DOMException.
if (this[state] === "loading") {
throw new DOMException(
"Invalid FileReader state.",
"Invalid FileReader state",
"InvalidStateError",
);
}
Expand Down
Loading

0 comments on commit 25801fb

Please sign in to comment.