0.9.2 • Published 3 years ago

client-errors v0.9.2

Weekly downloads
3
License
MIT
Repository
github
Last release
3 years ago

client-errors

Client (4xx) error classes including status codes and messages for each error in Node server.

Installation

$ npm install client-errors

Example

Basic Usage

const { ClientError, UnauthorizedError } = require('client-errors');

// Use of a derived class
throw new UnauthorizedError(); // default message: 'The user is not authorized'
throw new UnauthorizedError('you are a bad guy!'); // custom message: 'you are a bad guy'

// Use of a error code
throw new ClientError(401); // default message: 'The user is not authorized'
throw new ClientError(401, 'you are a bad guy!'); // custom message: 'you are a bad guy'

Usage of Sending Status Code

const express = require('express');
const router = express.Router();
const { ClientError, UnauthorizedError, BadRequestError } = require('client-errors');
const mongoose = require('mongoose');

router.put('/items/:id', updateItem);

function updateItem(req, res) {
  mongoose
    .model('Item')
    .findById(req.params.id)
    .then(item => {
      if (!item) throw new BadRequestError('item does not exist');
      if (item.ownedBy !== req.user.id) throw new UnauthorizedError('invalid access to this item');

      const data = req.body;
      if (data.ownedBy !== req.user.id) throw new ClientError(403, 'cannot update owners of items');

      item.set(data);
      item.save().then(res.json);
    })
    .catch(err => {
      let status, message;
      if (err.statusCode) {
        status = err.statusCode;
        message = err.message;
      } else {
        status = 422;
        message = err;
      }
      res.status(status).send({ message });
    });
}

Client Errors

CodeDescriptionCode NameDefault Message
400Bad RequestBadRequestErrorThe server cannot process the request due to a client error
401UnauthorizedUnauthorizedErrorThe user is not authorized
403ForbiddenForbiddenErrorThe server refused to authorize the request
404Not FoundNotFoundErrorThe server did not find a current representation for the target resource
405Method Not AllowedMethodNotAllowedErrorThe method received is not allowed
406Not AcceptableNotAcceptableErrorThe request is not acceptable to the user agent
407Proxy Authentication RequiredProxyAuthRequiredErrorThe client needs to authenticate itself in order to use a proxy
408Request TimeoutRequestTimeoutErrorThe request was not completed in the expected time
409ConflictConflictErrorThe request was not completed due to a conflict with the target resource
410GoneGoneErrorThe target resource is no longer available at the origin server
411Length RequiredLengthRequiredErrorThe server refuses to accept the request without a defined Content-Length
412Precondition FailedPreconditionFailedErrorOne or more conditions given in the request header fields evaluated to false
413Payload Too LargePayloadTooLargeErrorThe request payload is too large
414Request-URI Too LongUriTooLongErrorThe request target is too longer
415Unsupported Media TypUnsupportedMediaTypeErrorThe payload is in a format not supported
416Requested Range Not SatisfiableRequestedRangeNotSatisfiableErrorNone of the ranges in the request's Range header field overlap the current extent of the selected resource
417Expectation FailedExpectationFailedErrorThe expectation given in the request\'s Expect header field could not be met
418I'm a teapotTeapotErrorI'm a teapot
421Misdirected RequestMisdirectedRequestErrorThe request was directed at a server that is not able to produce a response
422Unprocessable EntityUnprocessableEntityErrorThe server is unable to process the request
423LockedLockedErrorThe source or destination resource of a method is locked
424Failed DependencyFailedDependencyErrorThe requested action depended on another action
426Upgrade RequiredUpgradeRequiredErrorThis service requires use of a different protocol
428Precondition RequiredPreconditionRequiredErrorThis request is required to be conditional
429Too Many RequestsTooManyRequestsErrorThe user has sent too many requests in a given amount of time
431Request Header Fields Too LargeRequestHeaderFieldsTooLargeErrorRequest header fields too large
451Unavailable For Legal ReasonsUnavailableForLegalReasonsErrorDenied access due to a consequence of a legal demand

MIT Licensed

0.9.2

3 years ago

0.9.1

5 years ago

0.9.0

5 years ago

0.8.3

5 years ago

0.8.2

5 years ago

0.8.1

5 years ago

0.8.0

5 years ago

1.0.0

5 years ago