Skip to content

Commit 5e9d872

Browse files
committed
Fix error output
1 parent 8b09bf3 commit 5e9d872

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,11 @@ function formatError(req, h) {
122122
if (response.output.statusCode === 500) {
123123
req.log('error', response.data instanceof Buffer ? response.data.toString() : response.data);
124124
}
125+
}
125126

126-
if (response.data.isJoi && response.data.details && typeof response.data.details.map === 'function') {
127-
const message = response.data.details.map(getJoiErrorMessage).join(', ');
128-
errorObject.details = message;
129-
}
127+
if (response.isJoi && response.details && typeof response.details.map === 'function') {
128+
const message = response.details.map(getJoiErrorMessage).join(', ');
129+
errorObject.details = message;
130130
}
131131

132132
return h.response({

test/index.test.js

+30
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,42 @@
11
'use strict';
22

33
const Hapi = require('hapi');
4+
const Joi = require('joi');
45
const assert = require('chai').assert;
56
const jsonapi = require('../');
67
const plugin = jsonapi.plugin;
78

89
describe('JSON API Plugin', () => {
910

11+
describe('Error formatting', () => {
12+
it('should format joi errors to more human readable output', async () => {
13+
const server = new Hapi.Server({debug: {request: '*'}});
14+
await server.register({plugin});
15+
server.route({
16+
method: 'POST',
17+
path: '/test',
18+
config: {
19+
validate: {
20+
failAction: (req, h, err) => err,
21+
payload: {
22+
fo: Joi.object({
23+
bar: Joi.object({
24+
banana: Joi.number().required()
25+
}).required()
26+
}).required()
27+
}
28+
},
29+
handler(req, h) {
30+
return h.response().code(204);
31+
}
32+
}
33+
});
34+
35+
const res = await server.inject({method: 'POST', url: '/test', payload: {fo: {}}});
36+
assert.equal(res.result.errors[0].details, 'Validation error: "bar" is required (fo,bar)');
37+
});
38+
});
39+
1040
describe('Decorated reply functions', () => {
1141
let server;
1242

0 commit comments

Comments
 (0)