1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

refactor(ext): align error messages (#25496)

Aligns the error messages in the ext/http and a few messages in the
ext/fetch folder to be in-line with the Deno style guide.

This change-set also removes some unnecessary checks in the 00_serve.ts.
These options were recently removed, so it doesn't make sense to check
for them anymore.

https://github.com/denoland/deno/issues/25269
This commit is contained in:
Ian Bull 2024-09-18 18:19:45 -07:00 committed by GitHub
parent fd23e8ec4f
commit 282c4c262d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 31 additions and 29 deletions

View file

@ -122,7 +122,7 @@ function newInnerRequest(method, url, headerList, body, maybeBlob) {
try { try {
this.headerListInner = headerList(); this.headerListInner = headerList();
} catch { } catch {
throw new TypeError("cannot read headers: request closed"); throw new TypeError("Cannot read headers: request closed");
} }
} }
return this.headerListInner; return this.headerListInner;
@ -153,7 +153,7 @@ function newInnerRequest(method, url, headerList, body, maybeBlob) {
try { try {
this.urlListProcessed[currentIndex] = this.urlList[currentIndex](); this.urlListProcessed[currentIndex] = this.urlList[currentIndex]();
} catch { } catch {
throw new TypeError("cannot read url: request closed"); throw new TypeError("Cannot read url: request closed");
} }
} }
return this.urlListProcessed[currentIndex]; return this.urlListProcessed[currentIndex];
@ -193,7 +193,7 @@ function cloneInnerRequest(request, skipBody = false) {
try { try {
this.urlListProcessed[0] = this.urlList[0](); this.urlListProcessed[0] = this.urlList[0]();
} catch { } catch {
throw new TypeError("cannot read url: request closed"); throw new TypeError("Cannot read url: request closed");
} }
} }
return this.urlListProcessed[0]; return this.urlListProcessed[0];
@ -204,7 +204,7 @@ function cloneInnerRequest(request, skipBody = false) {
try { try {
this.urlListProcessed[currentIndex] = this.urlList[currentIndex](); this.urlListProcessed[currentIndex] = this.urlList[currentIndex]();
} catch { } catch {
throw new TypeError("cannot read url: request closed"); throw new TypeError("Cannot read url: request closed");
} }
} }
return this.urlListProcessed[currentIndex]; return this.urlListProcessed[currentIndex];
@ -236,13 +236,13 @@ const KNOWN_METHODS = {
*/ */
function validateAndNormalizeMethod(m) { function validateAndNormalizeMethod(m) {
if (RegExpPrototypeExec(HTTP_TOKEN_CODE_POINT_RE, m) === null) { if (RegExpPrototypeExec(HTTP_TOKEN_CODE_POINT_RE, m) === null) {
throw new TypeError("Method is not valid."); throw new TypeError("Method is not valid");
} }
const upperCase = byteUpperCase(m); const upperCase = byteUpperCase(m);
if ( if (
upperCase === "CONNECT" || upperCase === "TRACE" || upperCase === "TRACK" upperCase === "CONNECT" || upperCase === "TRACE" || upperCase === "TRACK"
) { ) {
throw new TypeError("Method is forbidden."); throw new TypeError("Method is forbidden");
} }
return upperCase; return upperCase;
} }
@ -418,7 +418,7 @@ class Request {
((init.body !== undefined && init.body !== null) || ((init.body !== undefined && init.body !== null) ||
inputBody !== null) inputBody !== null)
) { ) {
throw new TypeError("Request with GET/HEAD method cannot have body."); throw new TypeError("Request with GET/HEAD method cannot have body");
} }
// 36. // 36.
@ -442,7 +442,7 @@ class Request {
// 41. // 41.
if (initBody === null && inputBody !== null) { if (initBody === null && inputBody !== null) {
if (input[_body] && input[_body].unusable()) { if (input[_body] && input[_body].unusable()) {
throw new TypeError("Input request's body is unusable."); throw new TypeError("Input request's body is unusable");
} }
finalBody = inputBody.createProxy(); finalBody = inputBody.createProxy();
} }
@ -489,7 +489,7 @@ class Request {
const prefix = "Failed to execute 'Request.clone'"; const prefix = "Failed to execute 'Request.clone'";
webidl.assertBranded(this, RequestPrototype); webidl.assertBranded(this, RequestPrototype);
if (this[_body] && this[_body].unusable()) { if (this[_body] && this[_body].unusable()) {
throw new TypeError("Body is unusable."); throw new TypeError("Body is unusable");
} }
const clonedReq = cloneInnerRequest(this[_request]); const clonedReq = cloneInnerRequest(this[_request]);

View file

@ -123,7 +123,7 @@ function upgradeHttpRaw(req, conn) {
if (inner._wantsUpgrade) { if (inner._wantsUpgrade) {
return inner._wantsUpgrade("upgradeHttpRaw", conn); return inner._wantsUpgrade("upgradeHttpRaw", conn);
} }
throw new TypeError("upgradeHttpRaw may only be used with Deno.serve"); throw new TypeError("'upgradeHttpRaw' may only be used with Deno.serve");
} }
function addTrailers(resp, headerList) { function addTrailers(resp, headerList) {
@ -170,10 +170,10 @@ class InnerRequest {
_wantsUpgrade(upgradeType, ...originalArgs) { _wantsUpgrade(upgradeType, ...originalArgs) {
if (this.#upgraded) { if (this.#upgraded) {
throw new Deno.errors.Http("already upgraded"); throw new Deno.errors.Http("Already upgraded");
} }
if (this.#external === null) { if (this.#external === null) {
throw new Deno.errors.Http("already closed"); throw new Deno.errors.Http("Already closed");
} }
// upgradeHttpRaw is sync // upgradeHttpRaw is sync
@ -257,7 +257,7 @@ class InnerRequest {
if (this.#methodAndUri === undefined) { if (this.#methodAndUri === undefined) {
if (this.#external === null) { if (this.#external === null) {
throw new TypeError("request closed"); throw new TypeError("Request closed");
} }
// TODO(mmastrac): This is quite slow as we're serializing a large number of values. We may want to consider // TODO(mmastrac): This is quite slow as we're serializing a large number of values. We may want to consider
// splitting this up into multiple ops. // splitting this up into multiple ops.
@ -315,7 +315,7 @@ class InnerRequest {
} }
if (this.#methodAndUri === undefined) { if (this.#methodAndUri === undefined) {
if (this.#external === null) { if (this.#external === null) {
throw new TypeError("request closed"); throw new TypeError("Request closed");
} }
this.#methodAndUri = op_http_get_request_method_and_url(this.#external); this.#methodAndUri = op_http_get_request_method_and_url(this.#external);
} }
@ -329,7 +329,7 @@ class InnerRequest {
get method() { get method() {
if (this.#methodAndUri === undefined) { if (this.#methodAndUri === undefined) {
if (this.#external === null) { if (this.#external === null) {
throw new TypeError("request closed"); throw new TypeError("Request closed");
} }
this.#methodAndUri = op_http_get_request_method_and_url(this.#external); this.#methodAndUri = op_http_get_request_method_and_url(this.#external);
} }
@ -338,7 +338,7 @@ class InnerRequest {
get body() { get body() {
if (this.#external === null) { if (this.#external === null) {
throw new TypeError("request closed"); throw new TypeError("Request closed");
} }
if (this.#body !== undefined) { if (this.#body !== undefined) {
return this.#body; return this.#body;
@ -356,7 +356,7 @@ class InnerRequest {
get headerList() { get headerList() {
if (this.#external === null) { if (this.#external === null) {
throw new TypeError("request closed"); throw new TypeError("Request closed");
} }
const headers = []; const headers = [];
const reqHeaders = op_http_get_request_headers(this.#external); const reqHeaders = op_http_get_request_headers(this.#external);
@ -457,7 +457,7 @@ function fastSyncResponseOrStream(
// At this point in the response it needs to be a stream // At this point in the response it needs to be a stream
if (!ObjectPrototypeIsPrototypeOf(ReadableStreamPrototype, stream)) { if (!ObjectPrototypeIsPrototypeOf(ReadableStreamPrototype, stream)) {
innerRequest?.close(); innerRequest?.close();
throw new TypeError("invalid response"); throw new TypeError("Invalid response");
} }
const resourceBacking = getReadableStreamResourceBacking(stream); const resourceBacking = getReadableStreamResourceBacking(stream);
let rid, autoClose; let rid, autoClose;
@ -619,13 +619,15 @@ function serve(arg1, arg2) {
if (handler === undefined) { if (handler === undefined) {
if (options === undefined) { if (options === undefined) {
throw new TypeError( throw new TypeError(
"No handler was provided, so an options bag is mandatory.", "Cannot serve HTTP requests: either a `handler` or `options` must be specified",
); );
} }
handler = options.handler; handler = options.handler;
} }
if (typeof handler !== "function") { if (typeof handler !== "function") {
throw new TypeError("A handler function must be provided."); throw new TypeError(
`Cannot serve HTTP requests: handler must be a function, received ${typeof handler}`,
);
} }
if (options === undefined) { if (options === undefined) {
options = { __proto__: null }; options = { __proto__: null };
@ -679,7 +681,7 @@ function serve(arg1, arg2) {
if (wantsHttps) { if (wantsHttps) {
if (!options.cert || !options.key) { if (!options.cert || !options.key) {
throw new TypeError( throw new TypeError(
"Both cert and key must be provided to enable HTTPS.", "Both 'cert' and 'key' must be provided to enable HTTPS",
); );
} }
listenOpts.cert = options.cert; listenOpts.cert = options.cert;

View file

@ -207,7 +207,7 @@ function createRespondWith(
resp = await resp; resp = await resp;
if (!(ObjectPrototypeIsPrototypeOf(ResponsePrototype, resp))) { if (!(ObjectPrototypeIsPrototypeOf(ResponsePrototype, resp))) {
throw new TypeError( throw new TypeError(
"First argument to respondWith must be a Response or a promise resolving to a Response.", "First argument to 'respondWith' must be a Response or a promise resolving to a Response",
); );
} }
@ -220,7 +220,7 @@ function createRespondWith(
let respBody = null; let respBody = null;
if (innerResp.body !== null) { if (innerResp.body !== null) {
if (innerResp.body.unusable()) { if (innerResp.body.unusable()) {
throw new TypeError("Body is unusable."); throw new TypeError("Body is unusable");
} }
if ( if (
ObjectPrototypeIsPrototypeOf( ObjectPrototypeIsPrototypeOf(
@ -295,7 +295,7 @@ function createRespondWith(
let reader; let reader;
if (resourceBacking) { if (resourceBacking) {
if (respBody.locked) { if (respBody.locked) {
throw new TypeError("ReadableStream is locked."); throw new TypeError("ReadableStream is locked");
} }
reader = respBody.getReader(); // Acquire JS lock. reader = respBody.getReader(); // Acquire JS lock.
try { try {

View file

@ -1592,7 +1592,7 @@ Deno.test(
Deno.upgradeWebSocket(request); Deno.upgradeWebSocket(request);
}, },
Deno.errors.Http, Deno.errors.Http,
"already upgraded", "Already upgraded",
); );
socket.onerror = (e) => { socket.onerror = (e) => {
console.error(e); console.error(e);
@ -3694,7 +3694,7 @@ Deno.test(
} catch (cloneError) { } catch (cloneError) {
assert(cloneError instanceof TypeError); assert(cloneError instanceof TypeError);
assert( assert(
cloneError.message.endsWith("Body is unusable."), cloneError.message.endsWith("Body is unusable"),
); );
ac.abort(); ac.abort();
@ -3743,7 +3743,7 @@ Deno.test({
} catch (cloneError) { } catch (cloneError) {
assert(cloneError instanceof TypeError); assert(cloneError instanceof TypeError);
assert( assert(
cloneError.message.endsWith("Body is unusable."), cloneError.message.endsWith("Body is unusable"),
); );
ac.abort(); ac.abort();
@ -3895,7 +3895,7 @@ async function readTrailers(
const tp = new TextProtoReader(r); const tp = new TextProtoReader(r);
const result = await tp.readMimeHeader(); const result = await tp.readMimeHeader();
if (result == null) { if (result == null) {
throw new Deno.errors.InvalidData("Missing trailer header."); throw new Deno.errors.InvalidData("Missing trailer header");
} }
const undeclared = [...result.keys()].filter( const undeclared = [...result.keys()].filter(
(k) => !trailerNames.includes(k), (k) => !trailerNames.includes(k),
@ -3923,7 +3923,7 @@ function parseTrailer(field: string | null): Headers | undefined {
} }
const trailerNames = field.split(",").map((v) => v.trim().toLowerCase()); const trailerNames = field.split(",").map((v) => v.trim().toLowerCase());
if (trailerNames.length === 0) { if (trailerNames.length === 0) {
throw new Deno.errors.InvalidData("Empty trailer header."); throw new Deno.errors.InvalidData("Empty trailer header");
} }
const prohibited = trailerNames.filter((k) => isProhibitedForTrailer(k)); const prohibited = trailerNames.filter((k) => isProhibitedForTrailer(k));
if (prohibited.length > 0) { if (prohibited.length > 0) {