Skip to content

Commit

Permalink
Implementing logging in RequestLib
Browse files Browse the repository at this point in the history
Now if either the `get()` or `post()` functions in `RequestLib` fail we'll both log the error and send a notification to Airbrake.
  • Loading branch information
Cruikshanks committed Jan 31, 2023
1 parent 67aa036 commit dc6854f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
21 changes: 21 additions & 0 deletions app/lib/request.lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ async function get (url, additionalOptions = {}) {
result.response = error
}

if (!result.succeeded) {
_logFailure('GET', result, url, additionalOptions)
}

return result
}

Expand All @@ -72,6 +76,10 @@ async function post (url, additionalOptions = {}) {
result.response = error
}

if (!result.succeeded) {
_logFailure('POST', result, url, additionalOptions)
}

return result
}

Expand All @@ -85,6 +93,19 @@ async function _importGot () {
return got
}

function _logFailure (method, result, url, additionalOptions) {
const data = {
method,
result,
url,
additionalOptions
}

const severity = result.response instanceof Error ? 'errored' : 'failed'

global.GlobalNotifier.omfg(`${method} request ${severity}`, data)
}

/**
* Combines the custom Got options provided by the caller with our defaults
*
Expand Down
34 changes: 34 additions & 0 deletions test/lib/request.lib.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,20 @@ const RequestLib = require('../../app/lib/request.lib.js')

describe('RequestLib', () => {
const testDomain = 'http://example.com'
let notifierStub

beforeEach(() => {
// RequestLib depends on the GlobalNotifier to have been set. This happens in app/plugins/global-notifier.plugin.js
// when the app starts up and the plugin is registered. As we're not creating an instance of Hapi server in this
// test we recreate the condition by setting it directly with our own stub
notifierStub = { omfg: Sinon.stub() }
global.GlobalNotifier = notifierStub
})

afterEach(() => {
Sinon.restore()
Nock.cleanAll()
delete global.GlobalNotifier
})

describe('#get()', () => {
Expand Down Expand Up @@ -52,6 +62,12 @@ describe('RequestLib', () => {
Nock(testDomain).get('/').reply(500, { data: 'hello world' })
})

it('records the failure', async () => {
await RequestLib.get(testDomain)

expect(notifierStub.omfg.calledWith('GET request failed')).to.be.true()
})

describe('the result it returns', () => {
it("has a 'succeeded' property marked as false", async () => {
const result = await RequestLib.get(testDomain)
Expand All @@ -74,6 +90,12 @@ describe('RequestLib', () => {
Nock(testDomain).get('/').replyWithError({ code: 'ECONNRESET' })
})

it('records the error', async () => {
await RequestLib.get(testDomain)

expect(notifierStub.omfg.calledWith('GET request errored')).to.be.true()
})

describe('the result it returns', () => {
it("has a 'succeeded' property marked as false", async () => {
const result = await RequestLib.get(testDomain)
Expand Down Expand Up @@ -218,6 +240,12 @@ describe('RequestLib', () => {
Nock(testDomain).post('/').reply(500, { data: 'hello world' })
})

it('records the failure', async () => {
await RequestLib.post(testDomain)

expect(notifierStub.omfg.calledWith('POST request failed')).to.be.true()
})

describe('the result it returns', () => {
it("has a 'succeeded' property marked as false", async () => {
const result = await RequestLib.post(testDomain)
Expand All @@ -240,6 +268,12 @@ describe('RequestLib', () => {
Nock(testDomain).post('/').replyWithError({ code: 'ECONNRESET' })
})

it('records the error', async () => {
await RequestLib.post(testDomain)

expect(notifierStub.omfg.calledWith('POST request errored')).to.be.true()
})

describe('the result it returns', () => {
it("has a 'succeeded' property marked as false", async () => {
const result = await RequestLib.post(testDomain)
Expand Down

0 comments on commit dc6854f

Please sign in to comment.