Skip to content

Commit

Permalink
chore(http): remove http/unstable_errors (#3736)
Browse files Browse the repository at this point in the history
BREAKING: deprecate `std/http/http_errors`
  • Loading branch information
iuioiua authored Nov 1, 2023
1 parent 9560e44 commit 81dcc77
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 233 deletions.
166 changes: 139 additions & 27 deletions http/http_errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// This module is browser compatible.

/**
* @deprecated (will be removed after 0.210.0) Import from `std/http/unstable_errors.ts` instead.
* @deprecated (will be removed in 0.207.0) Use custom error classes and `http/status.ts` instead.
*
* A collection of HTTP errors and utilities.
*
Expand All @@ -17,7 +17,7 @@
*
* @example
* ```ts
* import { errors, isHttpError } from "https://deno.land/std@$STD_VERSION/http/unstable_errors.ts";
* import { errors, isHttpError } from "https://deno.land/std@$STD_VERSION/http/http_errors.ts";
*
* try {
* throw new errors.NotFound();
Expand All @@ -32,7 +32,7 @@
*
* @example
* ```ts
* import { createHttpError } from "https://deno.land/std@$STD_VERSION/http/unstable_errors.ts";
* import { createHttpError } from "https://deno.land/std@$STD_VERSION/http/http_errors.ts";
* import { Status } from "https://deno.land/std@$STD_VERSION/http/status.ts";
*
* try {
Expand All @@ -49,35 +49,132 @@
* @module
*/

/**
* @deprecated (will be removed after 0.210.0) Import from `std/http/unstable_errors.ts` instead.
*/
import {
createHttpError as createHttpError_,
errors as errors_,
type ErrorStatusKeys as ErrorStatusKeys_,
HttpError as HttpError_,
type HttpErrorOptions as HttpErrorOptions_,
isHttpError as isHttpError_,
} from "./unstable_errors.ts";
type ErrorStatus,
isClientErrorStatus,
Status,
STATUS_TEXT,
} from "./status.ts";

const ERROR_STATUS_MAP = {
"BadRequest": 400,
"Unauthorized": 401,
"PaymentRequired": 402,
"Forbidden": 403,
"NotFound": 404,
"MethodNotAllowed": 405,
"NotAcceptable": 406,
"ProxyAuthRequired": 407,
"RequestTimeout": 408,
"Conflict": 409,
"Gone": 410,
"LengthRequired": 411,
"PreconditionFailed": 412,
"RequestEntityTooLarge": 413,
"RequestURITooLong": 414,
"UnsupportedMediaType": 415,
"RequestedRangeNotSatisfiable": 416,
"ExpectationFailed": 417,
"Teapot": 418,
"MisdirectedRequest": 421,
"UnprocessableEntity": 422,
"Locked": 423,
"FailedDependency": 424,
"UpgradeRequired": 426,
"PreconditionRequired": 428,
"TooManyRequests": 429,
"RequestHeaderFieldsTooLarge": 431,
"UnavailableForLegalReasons": 451,
"InternalServerError": 500,
"NotImplemented": 501,
"BadGateway": 502,
"ServiceUnavailable": 503,
"GatewayTimeout": 504,
"HTTPVersionNotSupported": 505,
"VariantAlsoNegotiates": 506,
"InsufficientStorage": 507,
"LoopDetected": 508,
"NotExtended": 510,
"NetworkAuthenticationRequired": 511,
} as const;

/**
* @deprecated (will be removed after 0.210.0) Import from `std/http/unstable_errors.ts` instead.
* @deprecated (will be removed in 0.207.0) Use custom error classes instead.
*/
export type ErrorStatusKeys = ErrorStatusKeys_;
export type ErrorStatusKeys = keyof typeof ERROR_STATUS_MAP;

/**
* @deprecated (will be removed after 0.210.0) Import from `std/http/unstable_errors.ts` instead.
* @deprecated (will be removed in 0.207.0) Use custom error classes instead.
*/
export type HttpErrorOptions = HttpErrorOptions_;
export interface HttpErrorOptions extends ErrorOptions {
expose?: boolean;
headers?: HeadersInit;
}

/**
* @deprecated (will be removed after 0.210.0) Import from `std/http/unstable_errors.ts` instead.
* @deprecated (will be removed in 0.207.0) Use custom error classes instead.
*
* The base class that all derivative HTTP extend, providing a `status` and an expose` property.
* The base class that all derivative HTTP extend, providing a `status` and an
* `expose` property.
*/
export const HttpError = HttpError_;
export class HttpError extends Error {
#status: ErrorStatus = Status.InternalServerError;
#expose: boolean;
#headers?: Headers;
constructor(
message = "Http Error",
options?: HttpErrorOptions,
) {
super(message, options);
this.#expose = options?.expose === undefined
? isClientErrorStatus(this.status)
: options.expose;
if (options?.headers) {
this.#headers = new Headers(options.headers);
}
}
/** A flag to indicate if the internals of the error, like the stack, should
* be exposed to a client, or if they are "private" and should not be leaked.
* By default, all client errors are `true` and all server errors are
* `false`. */
get expose(): boolean {
return this.#expose;
}
/** The optional headers object that is set on the error. */
get headers(): Headers | undefined {
return this.#headers;
}
/** The error status that is set on the error. */
get status(): ErrorStatus {
return this.#status;
}
}

function createHttpErrorConstructor(status: ErrorStatus): typeof HttpError {
const name = `${Status[status]}Error`;
const ErrorCtor = class extends HttpError {
constructor(
message = STATUS_TEXT[status],
options?: HttpErrorOptions,
) {
super(message, options);
Object.defineProperty(this, "name", {
configurable: true,
enumerable: false,
value: name,
writable: true,
});
}

override get status() {
return status;
}
};
return ErrorCtor;
}

/**
* @deprecated (will be removed after 0.210.0) Import from `std/http/unstable_errors.ts` instead.
* @deprecated (will be removed in 0.207.0) Use custom `http/status.ts` instead.
*
* A namespace that contains each error constructor. Each error extends
* `HTTPError` and provides `.status` and `.expose` properties, where the
Expand All @@ -89,26 +186,41 @@ export const HttpError = HttpError_;
*
* @example
* ```ts
* import { errors } from "https://deno.land/std@$STD_VERSION/http/unstable_errors.ts";
* import { errors } from "https://deno.land/std@$STD_VERSION/http/http_errors.ts";
*
* throw new errors.InternalServerError("Ooops!");
* ```
*/
export const errors = errors_;
export const errors: Record<ErrorStatusKeys, typeof HttpError> = {} as Record<
ErrorStatusKeys,
typeof HttpError
>;

for (const [key, value] of Object.entries(ERROR_STATUS_MAP)) {
errors[key as ErrorStatusKeys] = createHttpErrorConstructor(value);
}

/**
* @deprecated (will be removed after 0.210.0) Import from `std/http/unstable_errors.ts` instead.
* @deprecated (will be removed in 0.207.0) Use custom error classes instead.
*
* A factory function which provides a way to create errors. It takes up to 3
* arguments, the error `Status`, an message, which defaults to the status text
* and error options, which includes the `expose` property to set the `.expose`
* value on the error.
*/
export const createHttpError = createHttpError_;
export function createHttpError(
status: ErrorStatus = Status.InternalServerError,
message?: string,
options?: HttpErrorOptions,
): HttpError {
return new errors[Status[status] as ErrorStatusKeys](message, options);
}

/**
* @deprecated (will be removed after 0.210.0) Import from `std/http/unstable_errors.ts` instead.
* @deprecated (will be removed in 0.207.0) Use custom error classes instead.
*
* A type guard that determines if the value is an HttpError or not.
*/
export const isHttpError = isHttpError_;
export function isHttpError(value: unknown): value is HttpError {
return value instanceof HttpError;
}
2 changes: 1 addition & 1 deletion http/unstable_errors_test.ts → http/http_errors_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
errors,
type ErrorStatusKeys,
HttpError,
} from "./unstable_errors.ts";
} from "./http_errors.ts";

const clientErrorStatus: ErrorStatus[] = [
Status.BadRequest,
Expand Down
Loading

0 comments on commit 81dcc77

Please sign in to comment.