/** * Creates an API error. * @param {string} message - Error message. * @param {number} status - HTTP status code of error. * @param {boolean} isPublic - Whether the message should be visible to user or not. */ constructor({ message, errors, stack, status = httpStatus.INTERNAL_SERVER_ERROR, isPublic = false, }) { super({ message, errors, status, isPublic, stack, }); }
delete(req, res) { const id = { _id: req.params.id }; CrushController.delete(id) .then(result => sendResponse(res, httpStatus.OK, result)) .catch((err) => { console.error('Erro: ' + err); sendResponse(res, httpStatus.INTERNAL_SERVER_ERROR, {}); }); }
it('handler middleware should return error without error stack', () => { const err = new Error(); err.status = httpStatus.INTERNAL_SERVER_ERROR; process.env.NODE_ENV = 'test'; handler(err, req, res); expect(statusStub).toBeCalledWith(httpStatus.INTERNAL_SERVER_ERROR); expect(jsonStub).toHaveBeenCalledTimes(1); expect(endStub).toHaveBeenCalledTimes(1); });
create(req, res) { const crush = req.body; CrushController.create(crush) .then(crush => sendResponse(res, httpStatus.CREATED, 'Crush criado com amor!'), ) .catch(err => { console.error('Erro: ' + err); sendResponse(res, httpStatus.INTERNAL_SERVER_ERROR, {}); }); }
async findById(id) { try { return await Customers.findByPk(id, { attributes: ['name', 'email'] }) } catch (err) { return { statusCode: status.INTERNAL_SERVER_ERROR, message: status.classes['5XX_MESSAGE'], error: err } } }
getAll(req, res) { CrushController.getAll() .then(crushs => sendResponse(res, httpStatus.OK, crushs)) .catch(err => { console.error('Erro: ' + err); sendResponse(res, httpStatus.INTERNAL_SERVER_ERROR, {}); }); }
app.use((err, req, res, next) => { const status = err.status || httpStatus.INTERNAL_SERVER_ERROR; res.status(status).send(`Unidentified Error - ${err.message}`); next(); });
getByID(req, res) { const id = { _id: req.params.id }; if (!id) { sendResponse(res, httpStatus.OK, 'Crush não encontrado'); } CrushController.getByID(id) .then(crush => sendResponse(res, httpStatus.OK, crush)) .catch(err => { console.error('Erro: ' + err); sendResponse(res, httpStatus.INTERNAL_SERVER_ERROR, {}); }); }
it('handler middleware should return http status message for error without message', () => { const err = new Error(); err.status = httpStatus.INTERNAL_SERVER_ERROR; handler(err, req, res); expect(statusStub).toBeCalledWith(httpStatus.INTERNAL_SERVER_ERROR); expect(jsonStub).toHaveBeenCalledTimes(1); expect(endStub).toHaveBeenCalledTimes(1); });
update(req, res) { const id = { _id: req.params.id }; const crush = req.body; CrushController.update(id, crush) .then(crush => sendResponse(res, httpStatus.OK, 'Crush alterado!')) .catch((err) => { console.error('Erro: ' + err); sendResponse(res, httpStatus.INTERNAL_SERVER_ERROR, {}); }); }
it('error converter should convert generic error', () => { const error = new Error('Something went wrongasdasdasddsa'); converter(error, req, res); expect(statusStub).toBeCalledWith(httpStatus.INTERNAL_SERVER_ERROR); expect(jsonStub).toHaveBeenCalledTimes(1); expect(endStub).toHaveBeenCalledTimes(1); });
async create(body) { try { return await Customers.create(body) } catch (err) { return { statusCode: status.INTERNAL_SERVER_ERROR, message: status.classes['5XX_MESSAGE'], error: err } } }
it('handler middleware should convert error with http status = 0, into internal server error', () => { const err = new Error(); err.status = 0; handler(err, req, res); expect(statusStub).toBeCalledWith(httpStatus.INTERNAL_SERVER_ERROR); expect(jsonStub).toHaveBeenCalledTimes(1); expect(endStub).toHaveBeenCalledTimes(1); });
it('handler middleware should return error with error stack', () => { const err = new Error(); err.status = httpStatus.INTERNAL_SERVER_ERROR; process.env.NODE_ENV = 'development'; handler(err, req, res); expect(statusStub).toBeCalledWith(httpStatus.INTERNAL_SERVER_ERROR); expect(jsonStub).toHaveBeenCalledTimes(1); expect(endStub).toHaveBeenCalledTimes(1); });
/** * Creates an API error. * @param {string} message - Error message. * @param {number} status - HTTP status code of error. * @param {boolean} isPublic - Whether the message should be visible to user or not. */ constructor(message, status = httpStatus.INTERNAL_SERVER_ERROR, isPublic = false) { super(message, status, isPublic); }