1.0.3 • Published 3 years ago

simple-http-exceptions v1.0.3

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

Simple Http Exceptions

Objective

To supply basic classes with minimal http error status model. This is helpful in REST APIs implementation scenario to standardize return codes.

How to use

Throw an specific HttpException instance for an specific scenario:

//import then
const NotFoundException = require("simple-http-exceptions/errors/not-found-exception");

//...omitted code

//Simple example with express framework
app.get("/api/find/:id", async (req, res) => {
  try {
    const id = req.params.id;
    const data = await entityServer.findById(id);

    if (data === null) {
      throw new NotFoundException("Entity not found"); // ### Here is the trick ###
    }

    return res.status(200).json(data);
  } catch (error) {
    return res
      .status(error._httpStatus) // ### assume 404 http status ###
      .json({ message: error.message, errorType: error._name });
  }
});

Alternatively, you could implements a centralized error handler in a middleware:

/**
 * To handle all errors that have been throw
 * @param error
 * @param req
 * @param res
 * @param next
 * @returns {response}
 */
module.exports = function (error, req, res, next) {
  if (error._httpStatus === 500) {
    logger.error(error.stack);
  } else {
    logger.error(error.message);
  }

  return res.status(error._httpStatus).json({
    message: error.message,
    errorType: error._httpStatus,
  });
};

If you need more custom error map class, could you implement SimpleHttpException by yourself:

const SimpleHttpException = require("simple-http-exception");

class MyCustomHttpException extends SimpleHttpException {
  constructor(message) {
    super(message, "MY_CUSTOM_HTTP_ERROR", 666);
  }
}

module.exports = MyCustomHttpException;

Errors actually implemented

ClasshttpStatusname
BadRequestException400BAD_REQUEST
UnauthorizedException401UNAUTHORIZED
ForbiddenException403FORBIDDEN
NotFoundException404NOT_FOUND
UnprocessableEntityException422UNPROCESSABLE_ENTITY
InternalServerErrorException500INTERNAL_SERVER_ERROR

 

REPOSITORY

https://github.com/eviccari/simple-http-exceptions

 

ENVOLVED TECHNOLOGIES

  • NodeJS 14
  • Javascript
  • Node Package Management

 

MASTER DEPENDENCIES

 

ROADMAP