I use mongoose promises in my Node REST api and I have a lot of .catch blocks that are the same.

Here is a short route:

```
router.get('/', (req, res) => {
    Post.find()
        .populate('category', 'name')
        .exec()
        .then(posts => {
            res.status(200).json(posts);
        })
        .catch(err => {
            log.error(err);
            res.status(500).json(err);
        });
});

```

I have some routes where that exact .catch block appears three times in the same function.
Can I create a function where to have the log and result and pass the function to the .catch? Or can I use some middleware? How can I clean my code a bit?

Here is the mess:

```
router.delete('/:commentId', checkAuth, (req, res) => {
    // Find the comment
    Comment.findById(req.params.commentId).exec()
        .then(comment => {
            foundComment = comment;

            // Delete the comment
            Comment.deleteOne({ _id: req.params.commentId }).exec()
                .then(result => {
                    if (result.deletedCount) {

                        // If the comment has replies, remove them as well
                        if (foundComment.replies.length > 0) {
                            Comment.deleteMany({ _id: { $in: foundComment.replies } }).exec()
                                .then(result => {
                                    if (result.deletedCount) {
                                        res.status(200).json({
                                            deletedCount: result.deletedCount + 1
                                        });
                                    }
                                })
                                .catch(err => {
                                    log.error(err);
                                    res.status(500).json(err);
                                });

                            return;
                        }

                        res.status(200).json({
                            deletedCount: result.deletedCount
                        });
                    } else {
                        res.status(404).json({
                            message: `Comment with id ${req.params.commentId} doesn't exist.`
                        });
                    }
                })
                .catch(err => {
                    log.error(err);
                    res.status(500).json(err);
                });

        })
        .catch(err => {
            log.error(err);
            res.status(500).json(err);
        });
});

```