Skip to content

Commit

Permalink
Merge pull request #7 from firstandthird/fix-error-resp
Browse files Browse the repository at this point in the history
Update to add error object to response.
  • Loading branch information
ecwillis committed Nov 6, 2018
2 parents c65fe3a + b977e64 commit ec4420b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
45 changes: 34 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const register = async function(server, options) {
const register = function(server, options) {
server.ext('onPreResponse', async (request, h) => {
const response = request.response;
const path = request.path;
Expand All @@ -26,15 +26,23 @@ const register = async function(server, options) {
//error page is erroring
return `${request.query.statusCode} - ${request.query.error}`;
}
const { statusCode, error, message } = response.output.payload
const { statusCode, error, message } = response.output.payload;
const res = await server.inject({
url: `${options.url}?statusCode=${statusCode}&error=${error}&message=${message}`,
method: 'GET',
});

return h
.response(res.payload)
.code(response.output.statusCode);
const resp = h
.response(res.payload)
.code(response.output.statusCode);

resp._error = {
stack: null,
data: response.data,
output: response.output
};

return resp;
}

const payload = response.output.payload;
Expand All @@ -55,13 +63,28 @@ const register = async function(server, options) {
}
// a default error message:
if (!options.view) {
return h
.response(`<h1>There was an error</h1><h2>${context.error}: ${context.message}</h2>`)
.code(response.output.statusCode);
const resp = h
.response(`<h1>There was an error</h1><h2>${context.error}: ${context.message}</h2>`)
.code(response.output.statusCode);
resp._error = {
stack: null,
data: response.data,
output: response.output
};

return resp;
}
return h
.view(options.view, context)
.code(response.output.statusCode);
const resp = h
.view(options.view, context)
.code(response.output.statusCode);

resp._error = {
stack: null,
data: response.data,
output: response.output
};

return resp;
}
return h.continue;
});
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@
"extends": "firstandthird"
},
"devDependencies": {
"eclint": "^2.4.3",
"eslint": "^4.17.0",
"eslint-config-firstandthird": "^4.0.1",
"eslint-plugin-import": "^2.9.0",
"boom": "^4.2.0",
"code": "^5.1.2",
"coveralls": "^2.11.8",
"eslint": "^3.9.1",
"eslint-config-firstandthird": "^3.0.2",
"eslint-plugin-import": "^2.2.0",
"handlebars": "^4.0.5",
"hapi": "^17.0.1",
"lab": "^15.1.2",
Expand Down

0 comments on commit ec4420b

Please sign in to comment.