DEV Community

mkop
mkop

Posted on

Library for Http errors in Typescript

Alt Text

HTTP errors response

This library provide some Error response classes to map the problems that you may want to report to your clients.

Usage



npm install http-errors-response-ts --save

Enter fullscreen mode Exit fullscreen mode




Usage

import error response from lib


import { BadRequestResponse } from 'http-errors-response-ts';

throw new BadRequestResponse();

Enter fullscreen mode Exit fullscreen mode



expexted object



{
"statusCode": 400,
"message": "Bad Request"
}
Enter fullscreen mode Exit fullscreen mode



with custom message



import { BadRequestResponse } from 'http-errors-response-ts';

throw new BadRequestResponse('Custom message');

Enter fullscreen mode Exit fullscreen mode



expexted object



{
"statusCode": 400,
"message": "Custom message"
}
Enter fullscreen mode Exit fullscreen mode




List of all Error

Status Code Error Name
400 BadRequestResponse
401 UnauthorizedResponse
402 PaymentRequiredResponse
403 ForbiddenResponse
404 NotFoundResponse
405 MethodNotAllowedResponse
406 NotAcceptableResponse
407 ProxyAuthenticationRequiredResponse
408 RequestTimeoutResponse
409 ConflictResponse
410 GoneResponse
411 LengthRequiredResponse
412 PreconditionFailedResponse
413 PayloadTooLargeResponse
414 URITooLongResponse
415 UnsupportedMediaTypeResponse
416 RangeNotSatisfiableResponse
417 ExpectationFailedResponse
418 ImATeapotResponse
421 MisdirectedRequestResponse
422 UnprocessableEntityResponse
423 LockedResponse
424 FailedDependencyResponse
425 UnorderedCollectionResponse
426 UpgradeRequiredResponse
428 PreconditionRequiredResponse
429 TooManyRequestsResponse
431 RequestHeaderFieldsTooLargeResponse
451 UnavailableForLegalReasonsResponse
500 InternalServerErrorResponse
501 NotImplementedResponse
502 BadGatewayResponse
503 ServiceUnavailableResponse
504 GatewayTimeoutResponse
505 HTTPVersionNotSupportedResponse
506 VariantAlsoNegotiatesResponse
507 InsufficientStorageResponse
508 LoopDetectedResponse
509 BandwidthLimitExceededResponse
510 NotExtendedResponse
511 NetworkAuthenticationRequiredResponse

That's it!

npm package

Feel free to ask questions, make comments or suggestions, or just say hello in the comments below.

Top comments (4)

Collapse
 
inversemetric profile image
inversemetric

this is quality

Collapse
 
evert profile image
Evert Pot

I made something very similar a while back: npmjs.com/package/@curveball/http-...

cool though! it's a useful package

Collapse
 
kop7 profile image
mkop • Edited

Yes, it's pretty similar :)

What do you think about my package, do you think its easier to use?

Collapse
 
evert profile image
Evert Pot

I think they are so close, that they're probably similar in terms of how easy they are to use.

I needed a few things that your package does not have though, maybe it's interesting.

  1. I needed the 'Http error' to exist as a type, so existing exceptions can expose their 'http status' without extending a class (which is not always possible)
  2. Some HTTP errors have additional information, like Retry-After and Allow headers, so I added those too.
  3. I added another type that can expose more information about the error, and directly maps to the application/problem+json type.

These together allowed me to make a good error-handling middleware:

github.com/curveball/problem/

So if you're looking for ideas, maybe that helps!