1

I google for it and found some examples (like this).

I done everything like there, and all just fine!

But now every my router function contains try...catch block. Like that:

accounts = express.Router()

accounts.post('/following', async (req, res, next) => {
    try {
        ***do some stuff***
        if (smth_bad)
            next(new ErrorHandler(413, 400, "Wrong data"));

    } catch (e) {
        next(e)
    }
});

accounts.post('/followers', async (req, res, next) => {
    try {
        ***do some stuff***
        if (smth_bad)
            next(new ErrorHandler(413, 400, "Wrong data"));
    } catch (e) {
        next(e)
    }
});


accounts.post('/posts', async (req, res, next) => {
    try {
        ***do some stuff***
        if (smth_bad)
            next(new ErrorHandler(413, 400, "Wrong data"));
    } catch (e) {
        next(e)
    }
});

accounts.post('/pizza', async (req, res, next) => {
    try {
        ***do some stuff***
        if (smth_bad)
            next(new ErrorHandler(413, 400, "Wrong data"));
    } catch (e) {
        next(e)
    }
});

app.use('/api/v1/account', accounts);

app.use((err, req, res, next) => {
    handleError(err, res);
});

I know, that i can use next() without try...catch, but I want to handle unexpected errors and tell about it to user. My handling looks like this:

class ErrorHandler extends Error {
    constructor(statusCode, httpStatus, message) {
        super();
        this.statusCode = statusCode;
        this.httpStatus = httpStatus;
        this.message = message;
    }
}

const handleError = (err, res) => {
    if(err instanceof ErrorHandler){
        const { statusCode, message, httpStatus } = err;
        res.status(httpStatus).json({
            status: "error",
            statusCode,
            message
        });
    } else {
        console.error(err);
        res.status(500).json({
            status: "error",
            statusCode: '510',
            message: 'Server error',
        });
    }

};

Is there a way to simplify try...catch blocks in every router?

Dimabytes
  • 526
  • 1
  • 6
  • 17

1 Answers1

0

First off, try/catch will not catch errors asynchronously. What you're doing WILL work if all your code inside the try/catch is synchronous, but as you've already discovered, that's a really cumbersome way to approach the problem.

There are quite a few ways you could go about this, but I think maybe you should be asking a different question: "What does good modular code look like?" or "How do I implement a router inside my code?"

I would suggest you go looking for patterns on how to structure a router.

One possible approach is to write functions that you define outside your try/catch blocks (possibly in another module, for tidiness's sake) and use them inside the try/catch. Like this:

const getStuffFunction = function(req,callback){
  //do stuff here
}

accounts.post('/pizza', async (req, res, next) => {
  getStuffFunction(req,function(){
    //do router stuff here after your business logic is done
  })

Hope that helps.

Brightstar
  • 315
  • 3
  • 18