@appsemble/node-utils
Version:
NodeJS utilities used by Appsemble internally.
27 lines • 869 B
JavaScript
export function errorMiddleware() {
return async (ctx, next) => {
try {
await next();
}
catch (error) {
ctx.response.type = 'json';
if (ctx.status && ctx.body) {
ctx.app.emit('event', error, ctx);
}
else if (error instanceof Error &&
error.message === 'Fields "subject" and "body" must be a valid string') {
ctx.response.status = 400;
ctx.response.body = {
error: 'Bad Request',
message: 'Fields "subject" and "body" must be a valid string',
statusCode: 400,
};
ctx.app.emit('event', error, ctx);
}
else {
throw error;
}
}
};
}
//# sourceMappingURL=error.js.map