@curveball/http-errors
Advanced tools
Comparing version 0.2.2 to 0.3.0
@@ -0,1 +1,9 @@ | ||
0.3.0 (2018-10-01) | ||
================= | ||
* Mass-renamed httpCode to the more common httpStatus. | ||
* Added all missing status codes. | ||
* Fully unittested. | ||
0.2.2 (2018-09-14) | ||
@@ -2,0 +10,0 @@ ================== |
export interface HttpError extends Error { | ||
httpCode: number; | ||
httpStatus: number; | ||
} | ||
export interface HttpProblem extends HttpError { | ||
type: string | null; | ||
title: string; | ||
detail: string | null; | ||
instance: string | null; | ||
} | ||
@@ -11,5 +13,7 @@ export declare function isHttpError(e: Error): e is HttpError; | ||
export declare class HttpErrorBase extends Error implements HttpProblem { | ||
httpCode: number; | ||
type: string | null; | ||
httpStatus: number; | ||
title: string; | ||
detail: string | null; | ||
instance: string | null; | ||
constructor(detail?: string | null); | ||
@@ -20,3 +24,3 @@ } | ||
export declare class BadRequest extends HttpErrorBase { | ||
httpCode: number; | ||
httpStatus: number; | ||
title: string; | ||
@@ -37,3 +41,3 @@ } | ||
export declare class Unauthorized extends HttpErrorBase { | ||
httpCode: number; | ||
httpStatus: number; | ||
title: string; | ||
@@ -47,3 +51,3 @@ wwwAuthenticate: AuthenticateChallenge; | ||
export declare class PaymentRequired extends HttpErrorBase { | ||
httpCode: number; | ||
httpStatus: number; | ||
title: string; | ||
@@ -55,3 +59,3 @@ } | ||
export declare class Forbidden extends HttpErrorBase { | ||
httpCode: number; | ||
httpStatus: number; | ||
title: string; | ||
@@ -63,3 +67,3 @@ } | ||
export declare class NotFound extends HttpErrorBase { | ||
httpCode: number; | ||
httpStatus: number; | ||
title: string; | ||
@@ -77,3 +81,3 @@ } | ||
export declare class MethodNotAllowed extends HttpErrorBase { | ||
httpCode: number; | ||
httpStatus: number; | ||
title: string; | ||
@@ -87,3 +91,3 @@ allow: string[]; | ||
export declare class NotAcceptable extends HttpErrorBase { | ||
httpCode: number; | ||
httpStatus: number; | ||
title: string; | ||
@@ -103,3 +107,3 @@ } | ||
export declare class ProxyAuthenticationRequired extends HttpErrorBase { | ||
httpCode: number; | ||
httpStatus: number; | ||
title: string; | ||
@@ -113,3 +117,3 @@ proxyAuthenticate: AuthenticateChallenge; | ||
export declare class RequestTimeout extends HttpErrorBase { | ||
httpCode: number; | ||
httpStatus: number; | ||
title: string; | ||
@@ -121,3 +125,3 @@ } | ||
export declare class Conflict extends HttpErrorBase { | ||
httpCode: number; | ||
httpStatus: number; | ||
title: string; | ||
@@ -129,3 +133,3 @@ } | ||
export declare class Gone extends HttpErrorBase { | ||
httpCode: number; | ||
httpStatus: number; | ||
title: string; | ||
@@ -137,3 +141,3 @@ } | ||
export declare class LengthRequired extends HttpErrorBase { | ||
httpCode: number; | ||
httpStatus: number; | ||
title: string; | ||
@@ -145,3 +149,3 @@ } | ||
export declare class PreconditionFailed extends HttpErrorBase { | ||
httpCode: number; | ||
httpStatus: number; | ||
title: string; | ||
@@ -161,3 +165,3 @@ } | ||
export declare class PayloadTooLarge extends HttpErrorBase { | ||
httpCode: number; | ||
httpStatus: number; | ||
title: string; | ||
@@ -168,13 +172,115 @@ retryAfter: number | null; | ||
/** | ||
* 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 Entity | ||
*/ | ||
export declare class UnprocessableEntity extends HttpErrorBase { | ||
httpCode: number; | ||
httpStatus: number; | ||
title: string; | ||
} | ||
/** | ||
* 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 ServiceUnavailable('We\'re down temporarily', 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 { | ||
httpCode: number; | ||
httpStatus: number; | ||
title: string; | ||
@@ -186,5 +292,79 @@ } | ||
export declare class NotImplemented extends HttpErrorBase { | ||
httpCode: number; | ||
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 | ||
*/ | ||
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 {}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function isHttpError(e) { | ||
return Number.isInteger(e.httpCode); | ||
return Number.isInteger(e.httpStatus); | ||
} | ||
@@ -14,9 +14,9 @@ exports.isHttpError = isHttpError; | ||
super(detail); | ||
this.httpCode = 500; | ||
this.type = null; | ||
this.httpStatus = 500; | ||
this.title = 'Internal Server Error'; | ||
this.detail = null; | ||
if (!detail) { | ||
this.message = this.title; | ||
} | ||
this.instance = null; | ||
this.detail = detail; | ||
this.message = detail; | ||
} | ||
@@ -26,7 +26,7 @@ } | ||
function isClientError(e) { | ||
return isHttpError(e) && e.httpCode >= 400 && e.httpCode <= 499; | ||
return isHttpError(e) && e.httpStatus >= 400 && e.httpStatus <= 499; | ||
} | ||
exports.isClientError = isClientError; | ||
function isServerError(e) { | ||
return isHttpError(e) && e.httpCode >= 500 && e.httpCode <= 599; | ||
return isHttpError(e) && e.httpStatus >= 500 && e.httpStatus <= 599; | ||
} | ||
@@ -37,3 +37,3 @@ exports.isServerError = isServerError; | ||
super(...arguments); | ||
this.httpCode = 400; | ||
this.httpStatus = 400; | ||
this.title = 'Bad Request'; | ||
@@ -57,3 +57,3 @@ } | ||
super(detail); | ||
this.httpCode = 401; | ||
this.httpStatus = 401; | ||
this.title = 'Unauthorized'; | ||
@@ -70,3 +70,3 @@ this.wwwAuthenticate = wwwAuthenticate; | ||
super(...arguments); | ||
this.httpCode = 402; | ||
this.httpStatus = 402; | ||
this.title = 'Payment Required'; | ||
@@ -82,3 +82,3 @@ } | ||
super(...arguments); | ||
this.httpCode = 403; | ||
this.httpStatus = 403; | ||
this.title = 'Forbiddden'; | ||
@@ -94,3 +94,3 @@ } | ||
super(...arguments); | ||
this.httpCode = 404; | ||
this.httpStatus = 404; | ||
this.title = 'Not Found'; | ||
@@ -112,3 +112,3 @@ } | ||
super(detail); | ||
this.httpCode = 405; | ||
this.httpStatus = 405; | ||
this.title = 'Method Not Allowed'; | ||
@@ -125,3 +125,3 @@ this.allow = allow; | ||
super(...arguments); | ||
this.httpCode = 406; | ||
this.httpStatus = 406; | ||
this.title = 'Not Acceptable'; | ||
@@ -145,3 +145,3 @@ } | ||
super(detail); | ||
this.httpCode = 407; | ||
this.httpStatus = 407; | ||
this.title = 'Proxy Authentication Required'; | ||
@@ -158,3 +158,3 @@ this.proxyAuthenticate = proxyAuthenticate; | ||
super(...arguments); | ||
this.httpCode = 408; | ||
this.httpStatus = 408; | ||
this.title = 'Request Timeout'; | ||
@@ -170,3 +170,3 @@ } | ||
super(...arguments); | ||
this.httpCode = 409; | ||
this.httpStatus = 409; | ||
this.title = 'Conflict'; | ||
@@ -182,3 +182,3 @@ } | ||
super(...arguments); | ||
this.httpCode = 410; | ||
this.httpStatus = 410; | ||
this.title = 'Gone'; | ||
@@ -194,3 +194,3 @@ } | ||
super(...arguments); | ||
this.httpCode = 411; | ||
this.httpStatus = 411; | ||
this.title = 'LengthRequired'; | ||
@@ -206,3 +206,3 @@ } | ||
super(...arguments); | ||
this.httpCode = 412; | ||
this.httpStatus = 412; | ||
this.title = 'PreconditionFailed'; | ||
@@ -226,3 +226,3 @@ } | ||
super(detail); | ||
this.httpCode = 413; | ||
this.httpStatus = 413; | ||
this.title = 'Payload Too Large'; | ||
@@ -234,2 +234,57 @@ this.retryAfter = retryAfter; | ||
/** | ||
* Emits 414 URI Too Long | ||
*/ | ||
class UriTooLong extends HttpErrorBase { | ||
constructor() { | ||
super(...arguments); | ||
this.httpStatus = 414; | ||
this.title = 'URI Too Long'; | ||
} | ||
} | ||
exports.UriTooLong = UriTooLong; | ||
/** | ||
* Emits 415 Unsupported Media Type | ||
*/ | ||
class UnsupportedMediaType extends HttpErrorBase { | ||
constructor() { | ||
super(...arguments); | ||
this.httpStatus = 415; | ||
this.title = 'Unsupported Media Type'; | ||
} | ||
} | ||
exports.UnsupportedMediaType = UnsupportedMediaType; | ||
/** | ||
* Emits 416 Range Not Satisfiable | ||
*/ | ||
class RangeNotSatisfiable extends HttpErrorBase { | ||
constructor() { | ||
super(...arguments); | ||
this.httpStatus = 416; | ||
this.title = 'Range Not Satisfiable'; | ||
} | ||
} | ||
exports.RangeNotSatisfiable = RangeNotSatisfiable; | ||
/** | ||
* Emits 417 Expectation Failed | ||
*/ | ||
class ExpectationFailed extends HttpErrorBase { | ||
constructor() { | ||
super(...arguments); | ||
this.httpStatus = 417; | ||
this.title = 'Expectation Failed'; | ||
} | ||
} | ||
exports.ExpectationFailed = ExpectationFailed; | ||
/** | ||
* Emits 421 Misdirected Request | ||
*/ | ||
class MisdirectedRequest extends HttpErrorBase { | ||
constructor() { | ||
super(...arguments); | ||
this.httpStatus = 421; | ||
this.title = 'Misdirected Request'; | ||
} | ||
} | ||
exports.MisdirectedRequest = MisdirectedRequest; | ||
/** | ||
* Emits 422 Unprocessable Entity | ||
@@ -240,3 +295,3 @@ */ | ||
super(...arguments); | ||
this.httpCode = 422; | ||
this.httpStatus = 422; | ||
this.title = 'Unprocessable Entity'; | ||
@@ -247,2 +302,100 @@ } | ||
/** | ||
* Emits 423 Locked | ||
*/ | ||
class Locked extends HttpErrorBase { | ||
constructor() { | ||
super(...arguments); | ||
this.httpStatus = 423; | ||
this.title = 'Locked'; | ||
} | ||
} | ||
exports.Locked = Locked; | ||
/** | ||
* Emits 424 FailedDependency | ||
*/ | ||
class FailedDependency extends HttpErrorBase { | ||
constructor() { | ||
super(...arguments); | ||
this.httpStatus = 424; | ||
this.title = 'Failed Dependency'; | ||
} | ||
} | ||
exports.FailedDependency = FailedDependency; | ||
/** | ||
* Emits 425 Too Early | ||
*/ | ||
class TooEarly extends HttpErrorBase { | ||
constructor() { | ||
super(...arguments); | ||
this.httpStatus = 425; | ||
this.title = 'Too Early'; | ||
} | ||
} | ||
exports.TooEarly = TooEarly; | ||
/** | ||
* Emits 426 Upgrade Required | ||
*/ | ||
class UpgradeRequired extends HttpErrorBase { | ||
constructor() { | ||
super(...arguments); | ||
this.httpStatus = 426; | ||
this.title = 'Upgrade Required'; | ||
} | ||
} | ||
exports.UpgradeRequired = UpgradeRequired; | ||
/** | ||
* Emits 428 Precondition Required | ||
*/ | ||
class PreconditionRequired extends HttpErrorBase { | ||
constructor() { | ||
super(...arguments); | ||
this.httpStatus = 428; | ||
this.title = 'Precondition Required'; | ||
} | ||
} | ||
exports.PreconditionRequired = PreconditionRequired; | ||
/** | ||
* 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 ServiceUnavailable('We\'re down temporarily', 600) | ||
* | ||
*/ | ||
class TooManyRequests extends HttpErrorBase { | ||
constructor(detail = null, retryAfter = null) { | ||
super(detail); | ||
this.httpStatus = 429; | ||
this.title = 'Too Many Requests'; | ||
this.retryAfter = retryAfter; | ||
} | ||
} | ||
exports.TooManyRequests = TooManyRequests; | ||
/** | ||
* Emits 431 Request Header Fields Too Large | ||
*/ | ||
class RequestHeaderFieldsTooLarge extends HttpErrorBase { | ||
constructor() { | ||
super(...arguments); | ||
this.httpStatus = 431; | ||
this.title = 'Request Header Fields Too Large'; | ||
} | ||
} | ||
exports.RequestHeaderFieldsTooLarge = RequestHeaderFieldsTooLarge; | ||
/** | ||
* Emits 451 Unavailable For Legal Reasons | ||
*/ | ||
class UnavailableForLegalReasons extends HttpErrorBase { | ||
constructor() { | ||
super(...arguments); | ||
this.httpStatus = 451; | ||
this.title = 'Unavailable For Legal Reasons'; | ||
} | ||
} | ||
exports.UnavailableForLegalReasons = UnavailableForLegalReasons; | ||
/** | ||
* Emits 500 Internal Server Error | ||
@@ -253,3 +406,3 @@ */ | ||
super(...arguments); | ||
this.httpCode = 500; | ||
this.httpStatus = 500; | ||
this.title = 'Internal Server Error'; | ||
@@ -265,3 +418,3 @@ } | ||
super(...arguments); | ||
this.httpCode = 501; | ||
this.httpStatus = 501; | ||
this.title = 'Not Implemented'; | ||
@@ -271,2 +424,111 @@ } | ||
exports.NotImplemented = NotImplemented; | ||
/** | ||
* Emits 502 Bad Gateway | ||
*/ | ||
class BadGateway extends HttpErrorBase { | ||
constructor() { | ||
super(...arguments); | ||
this.httpStatus = 502; | ||
this.title = 'Bad Gateway'; | ||
} | ||
} | ||
exports.BadGateway = BadGateway; | ||
/** | ||
* 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) | ||
* | ||
*/ | ||
class ServiceUnavailable extends HttpErrorBase { | ||
constructor(detail = null, retryAfter = null) { | ||
super(detail); | ||
this.httpStatus = 503; | ||
this.title = 'Service Unavailable'; | ||
this.retryAfter = retryAfter; | ||
} | ||
} | ||
exports.ServiceUnavailable = ServiceUnavailable; | ||
/** | ||
* Emits 504 Gateway Timeout | ||
*/ | ||
class GatewayTimeout extends HttpErrorBase { | ||
constructor() { | ||
super(...arguments); | ||
this.httpStatus = 504; | ||
this.title = 'Gateway Timeout'; | ||
} | ||
} | ||
exports.GatewayTimeout = GatewayTimeout; | ||
/** | ||
* Emits 505 HTTP Version Not Supported | ||
*/ | ||
class HttpVersionNotSupported extends HttpErrorBase { | ||
constructor() { | ||
super(...arguments); | ||
this.httpStatus = 505; | ||
this.title = 'HTTP Version Not Supported'; | ||
} | ||
} | ||
exports.HttpVersionNotSupported = HttpVersionNotSupported; | ||
/** | ||
* Emits 506 Variant Also Negotiates | ||
*/ | ||
class VariantAlsoNegotiates extends HttpErrorBase { | ||
constructor() { | ||
super(...arguments); | ||
this.httpStatus = 506; | ||
this.title = 'Variant Also Negotiates'; | ||
} | ||
} | ||
exports.VariantAlsoNegotiates = VariantAlsoNegotiates; | ||
/** | ||
* Emits 507 Insufficient Storage | ||
*/ | ||
class UnsufficientStorage extends HttpErrorBase { | ||
constructor() { | ||
super(...arguments); | ||
this.httpStatus = 507; | ||
this.title = 'Unsufficient Storage'; | ||
} | ||
} | ||
exports.UnsufficientStorage = UnsufficientStorage; | ||
/** | ||
* Emits 508 Loop Detected | ||
*/ | ||
class LoopDetected extends HttpErrorBase { | ||
constructor() { | ||
super(...arguments); | ||
this.httpStatus = 508; | ||
this.title = 'Loop Detected'; | ||
} | ||
} | ||
exports.LoopDetected = LoopDetected; | ||
/** | ||
* Emits 510 Not Extended | ||
*/ | ||
class NotExtended extends HttpErrorBase { | ||
constructor() { | ||
super(...arguments); | ||
this.httpStatus = 510; | ||
this.title = 'Not Extended'; | ||
} | ||
} | ||
exports.NotExtended = NotExtended; | ||
/** | ||
* Emits 511 Network Authentication Required | ||
*/ | ||
class NetworkAuthenticationRequired extends HttpErrorBase { | ||
constructor() { | ||
super(...arguments); | ||
this.httpStatus = 511; | ||
this.title = 'Network Authentication Required'; | ||
} | ||
} | ||
exports.NetworkAuthenticationRequired = NetworkAuthenticationRequired; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@curveball/http-errors", | ||
"version": "0.2.2", | ||
"version": "0.3.0", | ||
"description": "A standard package for HTTP exceptions", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
200
README.md
@@ -16,6 +16,17 @@ HTTP Errors | ||
Instead of doing this over an over again for every project, it made more sense | ||
to be to create 1 generic package that I can re-use for every project I'm | ||
working on. | ||
Instead of doing this over an over again for every client and server-sideproject, | ||
it made more sense to be to create 1 generic package that I can re-use for every | ||
project I'm working on. | ||
I had trouble finding something similar on NPM, so I hope this is useful to | ||
others. | ||
This package has 0 dependencies and is not a part of any frameworks. Someone | ||
could use this package and integrate it into their own frameworks as a | ||
middleware. | ||
The hope is that as long as this package is independent, it could be used by | ||
library authors to throw generic errors with HTTP error information. | ||
Installation | ||
@@ -30,7 +41,187 @@ ------------ | ||
... | ||
After installing the NPM package, you can very easily reference all the | ||
exceptions contained within: | ||
```typescript | ||
import { NotFound, Forbidden } from '@curveball/http-errors'; | ||
throw new NotFound('Article not found'); | ||
``` | ||
This also works fine with plain javascript: | ||
```javascript | ||
const { NotFound, Forbidden } = require('@curveball/http-errors'); | ||
throw new Forbidden('You\'re not allowed to update this article'); | ||
``` | ||
The library also ships with a simple utility function to see if _any_ error has | ||
HTTP error information: | ||
```typescript | ||
import { isHttpError } from '@curveball/http-errors'; | ||
const myError = new Error('Custom error'); | ||
myError.httpStatus = 500; | ||
console.log(isHttpError(myError)); | ||
``` | ||
The idea is that as long as libraries follow this pattern, they don't need to | ||
depend on this library but they can be used automatically by middlewares. | ||
Built-in Error classes such as `NotFound`, `Unauthorized`, all have a `title` | ||
property with the default HTTP error string. | ||
```typescript | ||
const myError = new MethodNotAllowed('I can\'t believe youi\'ve done this'); | ||
console.log(myError.title); // Emits "Method Not Allowed" | ||
``` | ||
It also has a utility function to see if something was a Client or Server error: | ||
```typescript | ||
import { BadRequest, isServerError, isClientError } from '@curveball/http-errors'; | ||
const myError = new BadRequest('I didn\'t understand it'); | ||
console.log(isClientError(e)); // true | ||
console.log(isServerError(e)); // false | ||
``` | ||
Lastly, a few HTTP errors require or suggest including extra HTTP headers with | ||
more information about headers. For example, the `405 Method Not Allowed` | ||
header should contain an `Allow` header, and the `503 Service Unavailable` | ||
should have a `Retry-After` header. The built-in error classes include support | ||
for these: | ||
```typescript | ||
import { MethodNotAllowed, ServiceUnavailable } from '@curveball/http-errors'; | ||
try { | ||
throw new MethodNotAllowed('Not allowed', ['GET', 'PUT']); | ||
} catch (e) { | ||
console.log(e.allow); | ||
} | ||
try { | ||
throw new ServiceUnavailable('Not open on sundays', 3600*24); | ||
} catch (e) { | ||
console.log(e.retryAfter); | ||
} | ||
``` | ||
Lastly, it the package has an interface that makes it easier to work with the | ||
`application/problem+json` format, as defined in [RFC7807][3]. The interface | ||
has `type`, `title`, `detail` and `instance` properties, which makes it easier | ||
to write a generic interface that emits this JSON error format. | ||
API | ||
--- | ||
```typescript | ||
export function isHttpError(e: Error): e is HttpError; | ||
export function isHttpProblem(e: Error): e is HttpProblem; | ||
export function isClientError(e: Error): boolean; | ||
export function isServerError(e: Error): boolean; | ||
export interface HttpError extends Error { | ||
httpStatus: number; | ||
} | ||
export interface HttpProblem extends HttpError { | ||
type: string | null; | ||
title: string; | ||
detail: string | null; | ||
instance: string | null; | ||
} | ||
export class HttpErrorBase extends Error implements HttpProblem { | ||
type: string | null = null; | ||
httpStatus: number = 500; | ||
title: string = 'Internal Server Error'; | ||
detail: string|null = null; | ||
instance: string | null = null; | ||
constructor(detail: string|null = null) { } | ||
} | ||
export class BadRequest extends HttpErrorBase { } | ||
type AuthenticateChallenge = string | string[]; | ||
export class Unauthorized extends HttpErrorBase { | ||
wwwAuthenticate: AuthenticateChallenge; | ||
constructor(detail: string|null = null, wwwAuthenticate?: AuthenticateChallenge) {} | ||
} | ||
export class PaymentRequired extends HttpErrorBase {} | ||
export class Forbidden extends HttpErrorBase {} | ||
export class NotFound extends HttpErrorBase {} | ||
export class MethodNotAllowed extends HttpErrorBase { | ||
allow: string[] | ||
constructor(detail: string|null = null, allow?: string[]) {} | ||
} | ||
export class NotAcceptable extends HttpErrorBase {} | ||
export class ProxyAuthenticationRequired extends HttpErrorBase { | ||
proxyAuthenticate: AuthenticateChallenge; | ||
constructor(detail: string|null = null, proxyAuthenticate?: AuthenticateChallenge) {} | ||
} | ||
export class RequestTimeout extends HttpErrorBase {} | ||
export class Conflict extends HttpErrorBase {} | ||
export class Gone extends HttpErrorBase {} | ||
export class LengthRequired extends HttpErrorBase {} | ||
export class PreconditionFailed extends HttpErrorBase {} | ||
export class PayloadTooLarge extends HttpErrorBase { | ||
retryAfter: number | null; | ||
constructor(detail: string|null = null, retryAfter: number|null = null) {} | ||
} | ||
export class UriTooLong extends HttpErrorBase { } | ||
export class UnsupportedMediaType extends HttpErrorBase {} | ||
export class RangeNotSatisfiable extends HttpErrorBase {} | ||
export class ExpectationFailed extends HttpErrorBase {} | ||
export class MisdirectedRequest extends HttpErrorBase {} | ||
export class UnprocessableEntity extends HttpErrorBase {} | ||
export class Locked extends HttpErrorBase {} | ||
export class FailedDependency extends HttpErrorBase {} | ||
export class TooEarly extends HttpErrorBase {} | ||
export class UpgradeRequired extends HttpErrorBase {} | ||
export class PreconditionRequired extends HttpErrorBase {} | ||
export class TooManyRequests extends HttpErrorBase { | ||
retryAfter: number | null; | ||
constructor(detail: string|null = null, retryAfter: number|null = null) {} | ||
} | ||
export class RequestHeaderFieldsTooLarge extends HttpErrorBase {} | ||
export class UnavailableForLegalReasons extends HttpErrorBase {} | ||
export class InternalServerError extends HttpErrorBase {} | ||
export class NotImplemented extends HttpErrorBase {} | ||
export class BadGateway extends HttpErrorBase {} | ||
export class ServiceUnavailable extends HttpErrorBase { | ||
retryAfter: number | null; | ||
constructor(detail: string|null = null, retryAfter: number|null = null) {} | ||
} | ||
export class GatewayTimeout extends HttpErrorBase {} | ||
export class HttpVersionNotSupported extends HttpErrorBase {} | ||
export class VariantAlsoNegotiates extends HttpErrorBase {} | ||
export class UnsufficientStorage extends HttpErrorBase {} | ||
export class LoopDetected extends HttpErrorBase {} | ||
export class NotExtended extends HttpErrorBase {} | ||
export class NetworkAuthenticationRequired extends HttpErrorBase {} | ||
``` | ||
... | ||
@@ -40,1 +231,2 @@ | ||
[2]: http://koajs.com/ | ||
[3]: https://tools.ietf.org/html/rfc7807 |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
38576
864
230