Skip to content
This repository has been archived by the owner on Apr 3, 2019. It is now read-only.

Commit

Permalink
fix(push): do not throw if push fails on the notify endpoint
Browse files Browse the repository at this point in the history
Fixes #1510
  • Loading branch information
vladikoff committed Oct 29, 2016
1 parent 04d9fc5 commit 90b37d5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
2 changes: 1 addition & 1 deletion bin/key_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ function main() {
function (err) {
if (err) {
log.error({ op: 'server.start.1', msg: 'failed startup with error',
err: { message: err.message } })
err: { message: err.message } })
process.exit(1)
} else {
log.info({ op: 'server.start.1', msg: 'running on ' + server.info.uri })
Expand Down
13 changes: 13 additions & 0 deletions lib/routes/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -1201,12 +1201,25 @@ module.exports = function (

var endpointAction = 'devicesNotify'
var stringUid = uid.toString('hex')

function catchPushError(err) {
// push may fail due to not found devices or a bad push action
// log the error but still respond with a 200.
log.error({
op: 'Account.devicesNotify',
uid: stringUid,
error: err
})
}

return customs.checkAuthenticated(endpointAction, ip, stringUid)
.then(function () {
if (body.to === 'all') {
return push.pushToAllDevices(uid, endpointAction, pushOptions)
.catch(catchPushError)
} else {
return push.pushToDevices(uid, body.to, endpointAction, pushOptions)
.catch(catchPushError)
}
})
.done(
Expand Down
29 changes: 28 additions & 1 deletion test/local/account_routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ test('/account/device', function (t) {
})

test('/account/devices/notify', function (t) {
t.plan(5)
t.plan(6)
var config = {}
var uid = uuid.v4('binary')
var mockRequest = mocks.mockRequest({
Expand Down Expand Up @@ -599,6 +599,33 @@ test('/account/devices/notify', function (t) {
t.equal(err.message, 'Client has sent too many requests')
})
})

t.test('logs error if no devices found', function (t) {
t.plan(1)
mockRequest.payload = {
to: ['bogusid1', 'bogusid2'],
TTL: 60,
payload: pushPayload
}

var mockLog = mocks.spyLog()
var mockPush = mocks.mockPush({
pushToDevices: () => P.reject('Devices ids not found in devices')
})
var mockCustoms = {
checkAuthenticated: () => P.resolve()
}

route = getRoute(makeRoutes({
customs: mockCustoms,
log: mockLog,
push: mockPush
}), '/account/devices/notify')

return runTest(route, mockRequest, function (response) {
t.equal(JSON.stringify(response), '{}', 'response should not throw push errors')
})
})
})

test('/account/device/destroy', function (t) {
Expand Down

0 comments on commit 90b37d5

Please sign in to comment.