2021.4.13 • Published 4 years ago

express-endpoint-wrapper v2021.4.13

Weekly downloads
-
License
ISC
Repository
github
Last release
4 years ago

express-endpoint-wrapper

This package provides a wrapper to avoid try-catch blocks in each express-endpoint. It detects errors byInstance or byMessage.

basic usage

const express = require("express");
const router = express.Router({mergeParams: true});
const eew = require("express-endpoint-wrapper");

module.exports = function () {
    router.get("/", eew({
        callback: (req, res) => {
            // controller belongs here - anything you would do inside a normal express-route
        },
    }));

    return router;
};

usage with errors detected by message

const express = require("express");
const router = express.Router({mergeParams: true});
const eew = require("express-endpoint-wrapper");

module.exports = function () {
    router.get("/", eew({
        callback: (req, res) => {
            // controller belongs here - anything you would do inside a normal express-route
        },
        errors: [
            {
                status: 400, byMessage: "message-that-should-be-detected",
                errorCallback: ({res, status, message}) => {    // is fully optional; if not provide -> console.log
                    // do something with the error...
                    res.status(status).json({ error: message }); // for example
                }
            }
        ]
    }));

    return router;
};

usage with errors detected by instance

const express = require("express");
const router = express.Router({mergeParams: true});
const eew = require("express-endpoint-wrapper");

module.exports = function () {
    router.get("/", eew({
        callback: (req, res) => {
            // controller belongs here - anything you would do inside a normal express-route
        },
        errors: [
            {
                status: 400, byInstance: SuperFancyCustomError,
                errorCallback: ({res, status, message}) => {    // is fully optional; if not provide -> console.log
                    // do something with the error...
                    res.status(status).json({ error: message }); // for example
                }
            }
        ]
    }));

    return router;
};
2021.4.13

4 years ago

2021.4.12

4 years ago

2021.4.11

4 years ago

2021.4.10

4 years ago

2021.5.0

4 years ago

2021.4.8

4 years ago

2021.4.7

4 years ago

2021.4.6

4 years ago

2021.4.5

4 years ago

2021.4.4

4 years ago

2021.4.3

4 years ago

2021.4.2

4 years ago

2021.4.1

4 years ago