2.0.0 • Published 3 months ago

@cubedelement.com/civil-web v2.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

Civil Web

Test Wallaby.js

Sometimes you have to fight your own wars on the web! Anyway, this is a small library for http web error and statuses.

This yet another http web status code and http web error codes library. It's simple and clean and comprised of two parts:

  1. statusCodes: these are the numbers for http status codes. Sure you could have numbers only, but at least you have some meaning to them now!
  2. HttpStatusError: These are your extended Error objects, which are extended from HttpStatusError in here. If you need some specific type of error simply preprend the name in front. For example, you need I am a teapot, it's IAmATeapotHttpStatusError!

All items are based off of Mozilla's response docs, and deprecated items are removed.

Note: if you need deprecated errors, you can do so with the HttpStatusError object

Install it!

npm i -S civil-web

Use it!

Yes, these are horrible examples, but hopefully they at least show you that you have meaningful status codes, meaningful error, and a way to create custom http status errors!

Also note, that I'm not going to check if your status code is w/n a range. I assume you want to be the leader of your own destiny, so here you go, and I hope you enjoy! :)

const {HttpStatusError, BadRequestHttpStatusError, statusCodes} = require('civil-web');

const goingPlaces = function(value){
 if (typeof value === 'object' ) throw new BadRequestHttpStatusError('why did you pass an object?');

 return { statusCode: statusCodes.ok };  
};

const custom = function(value){
 if (value === 'we are jumping!' ) throw new HttpStatusError(2019, 'why did you pass an object?');

 return { statusCode: statusCodes.ok };  
};

goingPlaces({foo:  'this isn\'t happening'});
custom('we are jumping!');

Lists of supported items

StatusCodes

Status NameStatus CodeError Type
--HttpStatusError
continue100-
switchingProtocol101-
webDavProcessing102-
earlyHints103-
ok200-
created201-
accepted202-
nonAuthoritativeInformation203-
noContent204-
webDaveMutliStatus7207-
webDaveMutliStatus8208-
imUsed226-
multipleChoice300-
movedPermanently301-
found302-
seeOther303-
notModified304-
permanentRedirect308-
badRequest400BadRequestHttpStatusError
unauthorized401UnauthorizedHttpStatusError
paymentRequired402PaymentRequiredHttpStatusError
forbidden403ForbiddenHttpStatusError
notFound404NotFoundHttpStatusError
methodNotAllowed405MethodNotAllowedHttpStatusError
notAcceptable406NotAcceptableHttpStatusError
proxyAuthenticationRequired407ProxyAuthenticationRequiredHttpStatusError
requestTimeout408RequestTimeoutHttpStatusError
conflict409ConflictHttpStatusError
gone410GoneHttpStatusError
lengthRequired411LengthRequiredHttpStatusError
preconditionFailed412PreconditionFailedHttpStatusError
payloadTooLarge413PayloadTooLargeHttpStatusError
uriTooLong414UriTooLongHttpStatusError
unsupportedMediaType415UnsupportedMediaTypeHttpStatusError
requestedRangeNotSatisfiable416RequestedRangeNotSatisfiableHttpStatusError
expectationFailed417ExpectationFailedHttpStatusError
iAmATeapot418IAmATeapotHttpStatusError
misdirectedRequest421MisdirectedRequestHttpStatusError
webDavUnprocessableEntity422WebDavUnprocessableEntityHttpStatusError
webDavLocked423WebDavLockedHttpStatusError
webDavFailedDependency424WebDavFailedDependencyHttpStatusError
tooEarly425TooEarlyHttpStatusError
upgradeRequired426UpgradeRequiredHttpStatusError
preconditionRequired428PreconditionRequiredHttpStatusError
tooManyRequests429TooManyRequestsHttpStatusError
requestHeaderFieldsTooLarge431RequestHeaderFieldsTooLargeHttpStatusError
unavailableForLegalReasons451UnavailableForLegalReasonsHttpStatusError
internalServerError500InternalServerErrorHttpStatusError
notImplemented501NotImplementedHttpStatusError
badGateway502BadGatewayHttpStatusError
serviceUnavailable503ServiceUnavailableHttpStatusError
gatewayTimeout504GatewayTimeoutHttpStatusError
httpVersionNotSupported505HttpVersionNotSupportedHttpStatusError
variantAlsoNegotiates506VariantAlsoNegotiatesHttpStatusError
insufficientStorage507InsufficientStorageHttpStatusError
webDavLoopDetected508WebDavLoopDetectedHttpStatusError
notExtended510NotExtendedHttpStatusError
networkAuthenticationRequired511NetworkAuthenticationRequiredHttpStatusError