grassrootsgrocery / admin-portal

GNU Affero General Public License v3.0
10 stars 5 forks source link

Throw Error() on the backend instead of an object with a status and message #68

Closed jasoncavanaugh closed 1 year ago

jasoncavanaugh commented 1 year ago

The backend is inconsistent in how it throws errors. In some cases, we throw errors like

res.status(statusCode);
throw new Error("Some message");

In other cases, we have it do

throw {
    status: statusCode
    message: "Some message"
}

We should be doing the former of these two. Additionally, errroMiddleware.ts should be checking for res.statusCode instead of err.status. Like this

export const errorHandler: ErrorRequestHandler = (err, req, res, next) => {
  const statusCode = res.statusCode || INTERNAL_SERVER_ERROR;
  res.status(statusCode);
  res.json({
    message: err.message,
    stack: process.env.NODE_ENV === "production" ? null : err.stack,
  });
};