redis-smq-rest-api
Version:
A RESTful API for RedisSMQ
37 lines • 1.2 kB
JavaScript
import { RedisSMQError } from 'redis-smq-common';
import { getErrorResponseParams } from '../../../app/errors/getErrorResponseParams.js';
import { ValidationError } from '../errors/ValidationError.js';
export const errorHandlerMiddleware = async (ctx, next) => {
try {
await next();
}
catch (e) {
console.error(e);
if (e instanceof RedisSMQError) {
const [code, message] = getErrorResponseParams(e);
ctx.body = {
error: {
code,
message,
details: e instanceof ValidationError ? e.errorObjects : {},
},
};
}
else {
ctx.status = 500;
ctx.body = {
error: {
code: ctx.status,
message: 'Internal server error',
details: (e instanceof Error && {
message: e.message,
name: e.name,
stack: e.stack,
}) ||
{},
},
};
}
}
};
//# sourceMappingURL=errorHandlerMiddleware.js.map