1.2.2 • Published 6 years ago

@rokuem/apierror v1.2.2

Weekly downloads
1
License
ISC
Repository
-
Last release
6 years ago

How to use:

  • Install the package: npm i -S @rokuem/apierror
  • import it in your node app: import ApiError from "@rokuem/apierror"
  • use it as an error passing the name, statusCode and message: new ApiError("userNotFound", 404, "the requested user couldn't be found!")
  • use it on api responses: res.status(err.statusCode).json(err)
  • by default the response doesn't show the message of an error in the json, so i placed another key "error" to handle that
  • enable debug: process.env.DEBUG = "@rokuem/apierror*"
  • the debug message will be red and will have an uid, but u can also pass a custom identifier as the 4th argument

  • full example:

import ApiError from "@rokuem/apierror"

export default function (req, res) {
    new Promise((resolve, reject) => {
        const email = req.body.email;

        if ((email || "").length === 0) {
            return reject("noEmail", 400, "we couldn't find an e-mail in the request!")
        }

        if (!req.body.password) {
            return reject(new ApiError("noPassword", 400, "password is missing!", email))
        }
    }).catch(err => {
        res.status(err.statusCode).json(err);
    })
}

Note:

  • this is a very simple package developed for personal usage, but anyone is welcome to use it too.
1.2.2

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago