-
Notifications
You must be signed in to change notification settings - Fork 675
feat(http/unstable): HttpError
#7132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
iuioiua
wants to merge
8
commits into
denoland:main
Choose a base branch
from
iuioiua:http-error
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+200
−1
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1b7c0b2
feat(http/unstable): `HttpError`
iuioiua 4ba2ca2
Tweaks
iuioiua 5ce5c24
Cleanup
iuioiua 1686218
Remove `statusText` handling
iuioiua b3c043e
Apply suggestions
iuioiua efef3c2
Suggestions
iuioiua 393faba
Tweaks
iuioiua c656401
Merge branch 'main' into http-error
iuioiua File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| // Copyright 2018-2026 the Deno authors. MIT license. | ||
| // This module is browser compatible. | ||
| import { type ErrorStatus, STATUS_TEXT } from "./status.ts"; | ||
|
|
||
| /** | ||
| * Options for {@linkcode HttpError}. | ||
| * | ||
| * @experimental **UNSTABLE**: New API, yet to be vetted. | ||
| */ | ||
| export interface HttpErrorOptions extends ErrorOptions { | ||
| /** | ||
| * Configuration options for the HTTP response associated with this error. | ||
| */ | ||
| init?: ResponseInit; | ||
| } | ||
|
|
||
| /** | ||
| * An error class for representing HTTP errors with status codes. | ||
| * | ||
| * @experimental **UNSTABLE**: New API, yet to be vetted. | ||
| * | ||
| * Extends the standard {@linkcode Error} class to include HTTP-specific | ||
| * properties such as status codes and optional response initialization options. | ||
| * It's commonly used in route handlers to signal HTTP errors that should be | ||
| * returned to the client. | ||
| * | ||
| * @param status The HTTP status code (e.g., 404, 500, 403) | ||
| * @param message Optional error message. Defaults to the standard status text for the given status code | ||
| * @param options Optional error options including cause and response init configuration | ||
| * | ||
| * @example Usage without custom message or options | ||
| * ```ts | ||
| * import { HttpError } from "@std/http/unstable-error"; | ||
| * import { assertEquals, assertInstanceOf } from "@std/assert"; | ||
| * | ||
| * try { | ||
| * throw new HttpError(404); | ||
| * } catch (error) { | ||
| * assertInstanceOf(error, HttpError); | ||
| * assertEquals(error.status, 404); | ||
| * assertEquals(error.message, "Not Found"); | ||
| * } | ||
| * ``` | ||
| * | ||
| * @example Usage with custom message | ||
| * ```ts | ||
| * import { HttpError } from "@std/http/unstable-error"; | ||
| * import { assertEquals, assertInstanceOf } from "@std/assert"; | ||
| * | ||
| * try { | ||
| * throw new HttpError(500, "Something went wrong"); | ||
| * } catch (error) { | ||
| * assertInstanceOf(error, HttpError); | ||
| * assertEquals(error.status, 500); | ||
| * assertEquals(error.message, "Something went wrong"); | ||
| * } | ||
| * ``` | ||
| * | ||
| * @example Usage with response init options | ||
| * ```ts | ||
| * import { HttpError } from "@std/http/unstable-error"; | ||
| * import { assertEquals, assertInstanceOf } from "@std/assert"; | ||
| * | ||
| * try { | ||
| * throw new HttpError(403, "Forbidden", { | ||
| * init: { headers: { "WWW-Authenticate": 'Basic realm="Secure Area"' } }, | ||
| * }); | ||
| * } catch (error) { | ||
| * assertInstanceOf(error, HttpError); | ||
| * assertEquals(error.status, 403); | ||
| * assertEquals(error.message, "Forbidden"); | ||
| * assertEquals( | ||
| * (error.init.headers as Record<string, string>)["WWW-Authenticate"], | ||
| * 'Basic realm="Secure Area"', | ||
| * ); | ||
| * } | ||
| * ``` | ||
| * | ||
| * @example Usage with cause | ||
| * ```ts | ||
| * import { HttpError } from "@std/http/unstable-error"; | ||
| * import { assertEquals, assertInstanceOf } from "@std/assert"; | ||
| * | ||
| * try { | ||
| * throw new HttpError(500, "Internal Server Error", { | ||
| * cause: new Error("Database connection failed"), | ||
| * }); | ||
| * } catch (error) { | ||
| * assertInstanceOf(error, HttpError); | ||
| * assertEquals(error.status, 500); | ||
| * assertEquals(error.message, "Internal Server Error"); | ||
| * assertInstanceOf(error.cause, Error); | ||
| * assertEquals(error.cause?.message, "Database connection failed"); | ||
| * } | ||
| * ``` | ||
| */ | ||
| export class HttpError extends Error { | ||
| /** | ||
| * The HTTP status code (e.g., 404, 500, 403) | ||
| * | ||
| * @example Usage | ||
| * ```ts | ||
| * import { HttpError } from "@std/http/unstable-error"; | ||
| * import { assertEquals, assertInstanceOf } from "@std/assert"; | ||
| * | ||
| * try { | ||
| * throw new HttpError(404); | ||
| * } catch (error) { | ||
| * assertInstanceOf(error, HttpError); | ||
| * assertEquals(error.status, 404); | ||
| * assertEquals(error.message, "Not Found"); | ||
| * } | ||
| * ``` | ||
| */ | ||
| status: ErrorStatus; | ||
| /** | ||
| * Configuration options for the HTTP response associated with this error. | ||
| * | ||
| * `init.status` always equals the status argument passed to the constructor | ||
| * and represents the HTTP status code. Other {@linkcode ResponseInit} fields | ||
| * (`headers`, `statusText`) come from `options.init` if supplied. | ||
| * | ||
| * @example Usage | ||
| * ```ts | ||
| * import { HttpError } from "@std/http/unstable-error"; | ||
| * import { assertEquals, assertInstanceOf } from "@std/assert"; | ||
| * | ||
| * try { | ||
| * throw new HttpError(403, "Forbidden", { | ||
| * init: { headers: { "WWW-Authenticate": 'Basic realm="Secure Area"' } }, | ||
| * }); | ||
| * } catch (error) { | ||
| * assertInstanceOf(error, HttpError); | ||
| * assertEquals(error.status, 403); | ||
| * assertEquals(error.message, "Forbidden"); | ||
| * assertEquals( | ||
| * (error.init.headers as Record<string, string>)["WWW-Authenticate"], | ||
| * 'Basic realm="Secure Area"', | ||
| * ); | ||
| * } | ||
| * ``` | ||
| */ | ||
| init: ResponseInit; | ||
|
|
||
| /** | ||
| * Constructs a new instance. | ||
| * | ||
| * @param status The HTTP status code (e.g., 404, 500, 403) | ||
| * @param message Optional error message. Defaults to the standard status text for the given status code | ||
| * @param options Optional error options including cause and response init configuration | ||
| */ | ||
| constructor( | ||
| status: ErrorStatus, | ||
| message: string = STATUS_TEXT[status], | ||
| options?: HttpErrorOptions, | ||
| ) { | ||
| super(message, options); | ||
| this.name = this.constructor.name; | ||
| this.status = status; | ||
| this.init = { ...options?.init, status }; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| // Copyright 2018-2026 the Deno authors. MIT license. | ||
| import { assertEquals, assertInstanceOf } from "@std/assert"; | ||
| import { HttpError } from "./unstable_error.ts"; | ||
|
|
||
| Deno.test("new HttpError() defaults message to STATUS_TEXT[status]", () => { | ||
| const error = new HttpError(500); | ||
| assertInstanceOf(error, Error); | ||
| assertEquals(error.name, "HttpError"); | ||
| assertEquals(error.status, 500); | ||
| assertEquals(error.message, "Internal Server Error"); | ||
| assertEquals(error.cause, undefined); | ||
| assertEquals(error.init, { | ||
| status: 500, | ||
| }); | ||
| }); | ||
|
|
||
| Deno.test("new HttpError() forwards properties from options", () => { | ||
| const error = new HttpError(401, "Unauthorized", { | ||
| cause: new Error("Underlying error"), | ||
| init: { | ||
| headers: { "WWW-Authenticate": 'Basic realm="Secure Area"' }, | ||
| status: 400, | ||
| }, | ||
| }); | ||
| assertInstanceOf(error, Error); | ||
| assertEquals(error.name, "HttpError"); | ||
| assertEquals(error.status, 401); | ||
| assertEquals(error.message, "Unauthorized"); | ||
| assertInstanceOf(error.cause, Error); | ||
| assertEquals(error.cause?.message, "Underlying error"); | ||
| assertEquals( | ||
| (error.init.headers as Record<string, string>)["WWW-Authenticate"], | ||
| 'Basic realm="Secure Area"', | ||
| ); | ||
| assertEquals(error.init.status, error.status); | ||
| }); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing coverage: an explicit test for the override semantics of |
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fibibot's nit #2 was partially addressed (duplicated description gone) — but the three
@paramlines here just restate the class-level@paramlines verbatim. The constructor JSDoc can be dropped entirely; TypeDoc/LSP will show the class doc onnew HttpError(...). Keeping it adds drift risk (the@paramlines are now redundant copies).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The 3
@paramlines are here because of the doc lint checker requires them inconstructorJSDocs. Perhaps, this should be changed.