Skip to content

Commit

Permalink
Add support for request event error field
Browse files Browse the repository at this point in the history
  • Loading branch information
pthrasher authored and arb committed Feb 12, 2018
1 parent 4037636 commit 91385ff
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions API.md
Expand Up @@ -234,6 +234,7 @@ Event object associated with the "request" event. This is the hapi event emitter
- `timestamp` - timestamp of the incoming `event` object.
- `tags` - array of strings representing any tags associated with the 'log' event.
- `data` - the string or object mapped to `event.data`.
- `error` - the error instance mapped to `event.error`.
- `pid` - the current process id.
- `id` - id of the request, maps to `request.id`.
- `method` - method used by the request. Maps to `request.method`.
Expand Down
1 change: 1 addition & 0 deletions lib/utils.js
Expand Up @@ -142,6 +142,7 @@ class RequestLog {
this.timestamp = event.timestamp;
this.tags = event.tags;
this.data = event.data;
this.error = event.error;
this.pid = process.pid;
this.id = request.info.id;
this.method = request.method;
Expand Down
50 changes: 50 additions & 0 deletions test/utils.js
Expand Up @@ -94,6 +94,56 @@ describe('utils', () => {
});
});

describe('RequestLog()', () => {

it('accepts error on the event object when data is not present', { plan: 2 }, () => {

const errorInstance = new Error('This is a test');

const err = new Utils.RequestLog({}, {
info: {
id: 32
},
method: 'post',
path: '/graph',
config: {}
}, {
event: 'request',
timestamp: 1517592924723,
tags: ['error', 'authentication'],
error: errorInstance
});

expect(err.error).to.equal(errorInstance);
expect(err.data).to.be.undefined();

});

it('accepts data on the event object when error is not present', { plan: 2 }, () => {

const sampleData = { foo: 'bar' };

const err = new Utils.RequestLog({}, {
info: {
id: 32
},
method: 'post',
path: '/graph',
config: {}
}, {
event: 'request',
timestamp: 1517592924723,
tags: ['error', 'authentication'],
data: sampleData
});

expect(err.data).to.equal(sampleData);
expect(err.error).to.be.undefined();

});

});

describe('RequestError()', () => {

it('can be stringifyed', { plan: 1 }, () => {
Expand Down

0 comments on commit 91385ff

Please sign in to comment.