@curveball/http-errors
Version:
A standard package for HTTP exceptions
373 lines (372 loc) • 9.27 kB
TypeScript
export interface HttpError extends Error {
httpStatus: number;
}
export interface HttpProblem extends HttpError {
type: string | null;
title: string;
detail: string | null;
instance: string | null;
}
export declare function isHttpError(e: unknown): e is HttpError;
export declare function isHttpProblem(e: unknown): e is HttpProblem;
export declare class HttpErrorBase extends Error implements HttpProblem {
type: string | null;
httpStatus: number;
title: string;
detail: string | null;
instance: string | null;
constructor(detail?: string | null);
}
export declare function isClientError(e: Error): boolean;
export declare function isServerError(e: Error): boolean;
export declare class BadRequest extends HttpErrorBase {
httpStatus: number;
title: string;
}
type AuthenticateChallenge = string | string[];
/**
* Emits a 401 Unauthorized.
*
* This response must come with a WWW-Authenticate header. This challenge can
* optionally be provided via the constructor.
*
* examples:
* new Unauthorized('Login failed', 'Basic');
* new Unauthorized('Login failed', 'Basic; realm="secret area"');
* new Unauthorized('Login failed', ['Basic; realm="secret area', 'Bearer']);
*/
export declare class Unauthorized extends HttpErrorBase {
httpStatus: number;
title: string;
wwwAuthenticate?: AuthenticateChallenge;
constructor(detail?: string | null, wwwAuthenticate?: AuthenticateChallenge);
}
/**
* Emits 402 Payment Required
*/
export declare class PaymentRequired extends HttpErrorBase {
httpStatus: number;
title: string;
}
/**
* Emits 403 Forbidden
*/
export declare class Forbidden extends HttpErrorBase {
httpStatus: number;
title: string;
}
/**
* Emits 404 Not Found
*/
export declare class NotFound extends HttpErrorBase {
httpStatus: number;
title: string;
}
/**
* Emits 405 Method Not Allowed.
*
* The 405 Method Not Allowed HTTP response requires an Allow header.
* You can optionally use the second argument to provide this.
*
* Example:
* new MethodNotAllowed('This resource is read-only', ['GET', 'HEAD', 'OPTIONS']);
*/
export declare class MethodNotAllowed extends HttpErrorBase {
httpStatus: number;
title: string;
allow?: string[];
constructor(detail?: string | null, allow?: string[]);
}
/**
* Emits 406 Not Acceptable
*/
export declare class NotAcceptable extends HttpErrorBase {
httpStatus: number;
title: string;
}
/**
* Emits a 407 Proxy Autentication Required
*
* This response must come with a Proxy-Authenticate header. This challenge can
* optionally be provided via the constructor.
*
* examples:
* new ProxyAuthenticationRequired('Login failed', 'Basic');
* new ProxyAuthenticationRequired('Login failed', 'Basic; realm="secret area"');
* new ProxyAuthenticationRequired('Login failed', ['Basic; realm="secret area', 'Bearer']);
*/
export declare class ProxyAuthenticationRequired extends HttpErrorBase {
httpStatus: number;
title: string;
proxyAuthenticate?: AuthenticateChallenge;
constructor(detail?: string | null, proxyAuthenticate?: AuthenticateChallenge);
}
/**
* Emits 408 Request Timeout
*/
export declare class RequestTimeout extends HttpErrorBase {
httpStatus: number;
title: string;
}
/**
* Emits 409 Conflict
*/
export declare class Conflict extends HttpErrorBase {
httpStatus: number;
title: string;
}
/**
* Emits 410 Gone
*/
export declare class Gone extends HttpErrorBase {
httpStatus: number;
title: string;
}
/**
* Emits 411 Length Required
*/
export declare class LengthRequired extends HttpErrorBase {
httpStatus: number;
title: string;
}
/**
* Emits 412 Precondition Failed
*/
export declare class PreconditionFailed extends HttpErrorBase {
httpStatus: number;
title: string;
}
/**
* Emits 413 Content Too Large
*
* If the status is temporary, it's possible for a server to send a
* Retry-After header to try again. This value may be embeded in this
* exception.
*
* Example:
*
* throw new PayloadTooLarge('Send the large file again in 10 minutes', 600);
*/
export declare class ContentTooLarge extends HttpErrorBase {
httpStatus: number;
title: string;
retryAfter: number | null;
constructor(detail?: string | null, retryAfter?: number | null);
}
/**
* Payload Too Large was the old name of this error, but all instances of
* Payload have been renamed to Content in RFC9110.
*
* @deprecated
*/
export declare class PayloadTooLarge extends ContentTooLarge {
}
/**
* Emits 414 URI Too Long
*/
export declare class UriTooLong extends HttpErrorBase {
httpStatus: number;
title: string;
}
/**
* Emits 415 Unsupported Media Type
*/
export declare class UnsupportedMediaType extends HttpErrorBase {
httpStatus: number;
title: string;
}
/**
* Emits 416 Range Not Satisfiable
*/
export declare class RangeNotSatisfiable extends HttpErrorBase {
httpStatus: number;
title: string;
}
/**
* Emits 417 Expectation Failed
*/
export declare class ExpectationFailed extends HttpErrorBase {
httpStatus: number;
title: string;
}
/**
* Emits 421 Misdirected Request
*/
export declare class MisdirectedRequest extends HttpErrorBase {
httpStatus: number;
title: string;
}
/**
* Emits 422 Unprocessable Content
*/
export declare class UnprocessableContent extends HttpErrorBase {
httpStatus: number;
title: string;
}
/**
* RFC9110 renamed this to Unprocessable Content.
*
* @deprecated
*/
export declare class UnprocessableEntity extends UnprocessableContent {
}
/**
* Emits 423 Locked
*/
export declare class Locked extends HttpErrorBase {
httpStatus: number;
title: string;
}
/**
* Emits 424 FailedDependency
*/
export declare class FailedDependency extends HttpErrorBase {
httpStatus: number;
title: string;
}
/**
* Emits 425 Too Early
*/
export declare class TooEarly extends HttpErrorBase {
httpStatus: number;
title: string;
}
/**
* Emits 426 Upgrade Required
*/
export declare class UpgradeRequired extends HttpErrorBase {
httpStatus: number;
title: string;
}
/**
* Emits 428 Precondition Required
*/
export declare class PreconditionRequired extends HttpErrorBase {
httpStatus: number;
title: string;
}
/**
* Emits 429 Too Many Requests
*
* When sending this status the server may also send a Retry-After header
* indicating when it's safe try again.
*
* It's possible to supply this information via the second argument.
*
* Example:
* throw new TooManyRequests('Try again in 10 minutes', 600)
*
*/
export declare class TooManyRequests extends HttpErrorBase {
httpStatus: number;
title: string;
retryAfter: number | null;
constructor(detail?: string | null, retryAfter?: number | null);
}
/**
* Emits 431 Request Header Fields Too Large
*/
export declare class RequestHeaderFieldsTooLarge extends HttpErrorBase {
httpStatus: number;
title: string;
}
/**
* Emits 451 Unavailable For Legal Reasons
*/
export declare class UnavailableForLegalReasons extends HttpErrorBase {
httpStatus: number;
title: string;
}
/**
* Emits 500 Internal Server Error
*/
export declare class InternalServerError extends HttpErrorBase {
httpStatus: number;
title: string;
}
/**
* Emits 501 Not Implemented
*/
export declare class NotImplemented extends HttpErrorBase {
httpStatus: number;
title: string;
}
/**
* Emits 502 Bad Gateway
*/
export declare class BadGateway extends HttpErrorBase {
httpStatus: number;
title: string;
}
/**
* Emits 503 Service Unavailable.
*
* When sending this status the server may also send a Retry-After header
* indicating when it's safe try again.
*
* It's possible to supply this information via the second argument.
*
* Example:
* throw new ServiceUnavailable('We\'re down temporarily', 600)
*
*/
export declare class ServiceUnavailable extends HttpErrorBase {
httpStatus: number;
title: string;
retryAfter: number | null;
constructor(detail?: string | null, retryAfter?: number | null);
}
/**
* Emits 504 Gateway Timeout
*/
export declare class GatewayTimeout extends HttpErrorBase {
httpStatus: number;
title: string;
}
/**
* Emits 505 HTTP Version Not Supported
*/
export declare class HttpVersionNotSupported extends HttpErrorBase {
httpStatus: number;
title: string;
}
/**
* Emits 506 Variant Also Negotiates
*/
export declare class VariantAlsoNegotiates extends HttpErrorBase {
httpStatus: number;
title: string;
}
/**
* Emits 507 Insufficient Storage
*/
export declare class UnsufficientStorage extends HttpErrorBase {
httpStatus: number;
title: string;
}
/**
* Emits 508 Loop Detected
*/
export declare class LoopDetected extends HttpErrorBase {
httpStatus: number;
title: string;
}
/**
* Emits 510 Not Extended.
*
* This status code has been marked as obsolute.
*
* @deprecated
*/
export declare class NotExtended extends HttpErrorBase {
httpStatus: number;
title: string;
}
/**
* Emits 511 Network Authentication Required
*/
export declare class NetworkAuthenticationRequired extends HttpErrorBase {
httpStatus: number;
title: string;
}
export {};