@senacor/azure-function-middleware
Version:
Middleware for azure functions to handle authentication, authorization, error handling and logging
38 lines (37 loc) • 1.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.errorHandler = void 0;
const stringify_1 = require("../util/stringify");
const ApplicationError_1 = require("./ApplicationError");
const errorHandler = (error, context, opts) => {
context.error(error);
if ((opts === null || opts === void 0 ? void 0 : opts.errorResponseHandler) === undefined) {
if (error instanceof ApplicationError_1.ApplicationError) {
context.error(`Received application error with message ${error.message}`);
if (typeof error.body === 'object') {
return {
status: error.status,
jsonBody: error.body,
};
}
else {
return {
status: error.status,
body: (0, stringify_1.stringify)(error.body),
};
}
}
else {
return {
status: 500,
jsonBody: {
message: 'Internal server error',
},
};
}
}
else {
return opts.errorResponseHandler(error, context);
}
};
exports.errorHandler = errorHandler;