1.2.2 • Published 6 years ago
@rokuem/apierror v1.2.2
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.