Skip to content

Commit

Permalink
catch exception when applying XSLT
Browse files Browse the repository at this point in the history
This should prevent the app from crashing in case the XSL transformation
fails.
  • Loading branch information
cmil committed Aug 15, 2020
1 parent dd93b76 commit b372386
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions routehandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,35 @@ module.exports = function (dbSession) {
return;
}

const body = stylesheet.apply(r.result, xslparams);
response.type(options.type || 'html');
if (options.send) {
response.send(body);
} else {
response.locals.body = body;
next();
let body;
let {send, type = 'html'} = options;
let status = 200;

try {
body = stylesheet.apply(r.result, xslparams);
} catch (error) {
console.log(error);
let line = '';
if (error.line) {
line = r.result.split('\n')[error.line - 1];
console.log({line});
}

send = true;
status = 500;
type = 'text/plain';
body = `Internal server error\n\n${error.message}\n`;
Object.keys(error).forEach(key => {
body += `${key}: ${error[key]}\n`;
});
} finally {
response.type(type);
if (send) {
response.status(status).send(body);
} else {
response.locals.body = body;
next();
}
}
});
}
Expand Down

0 comments on commit b372386

Please sign in to comment.