Skip to content

Commit

Permalink
Report errors to user in response.
Browse files Browse the repository at this point in the history
Move test into the appropriate directory.
Refactor tests to use common fixture.
Add exception notification based on error type.
Detect wrapped errors and report them correctly.
  • Loading branch information
Oliver Esser committed Dec 4, 2016
1 parent 15930ec commit fc2eb59
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 41 deletions.
19 changes: 17 additions & 2 deletions lib/server/post-api-score.js
@@ -1,3 +1,5 @@
const VError = require('verror').VError

const config = require('../../config')
const scoreManager = require('../score-manager')

Expand All @@ -6,8 +8,21 @@ const scoreManager = require('../score-manager')
*/
function postApiScore (req, res) {
let requestConfig = req.body.config || config.scoreManager
let manager = scoreManager.create(requestConfig)
res.json(manager.score(req.body))
try {
let manager = scoreManager.create(requestConfig)
let result = manager.score(req.body)

// If there was no exception up to this point, we have success.
res.json({ success: true, result: result })
} catch (err) {
if (VError.hasCauseWithName(err, 'InvalidInputError')) {
res.json({ success: false, error: err.message })
return
}

res.json({ success: false, error: 'Internal Server Error' })
throw err
}
}

module.exports = postApiScore
36 changes: 0 additions & 36 deletions test/api-test.js

This file was deleted.

3 changes: 0 additions & 3 deletions test/buster.js
Expand Up @@ -3,9 +3,6 @@ var config = module.exports
config['My tests'] = {
rootPath: '../',
environment: 'node', // or 'browser'
sources: [
'lib/**/*.js'
],
tests: [
'test/**/*-test.js'
]
Expand Down
59 changes: 59 additions & 0 deletions test/server/post-api-score-test.js
@@ -0,0 +1,59 @@
const buster = require('buster')
const VError = require('verror').VError
const postApiScore = require('../../lib/server/post-api-score')
const scoreManager = require('../../lib/score-manager')
const config = require('../../config')
const InvalidInputError = require('../../lib/invalid-input-error')

buster.testCase('postApiScore', {
setUp: function () {
this.fakeScoreManager = { score: this.stub() }
this.create = this.stub(scoreManager, 'create').returns(this.fakeScoreManager)

this.req = { body: {} }
this.res = { send: this.stub(), json: this.stub() }
},

'should use scoreManager\'s create function': function () {
postApiScore(this.req, this.res)
buster.assert.called(this.create)
},

'should pass default config when request has no config': function () {
postApiScore(this.req, this.res)
buster.assert.calledWith(this.create, config.scoreManager)
},

'should pass request config when available': function () {
this.req.body.config = {}
postApiScore(this.req, this.res)
buster.assert.calledWith(this.create, {})
},

'should wrap result in object with success flag set to true': function () {
this.fakeScoreManager.score.returns('my result')
postApiScore(this.req, this.res)
buster.assert.calledWith(this.res.json, { success: true, result: 'my result' })
},

'should pass along error message on input error': function () {
this.fakeScoreManager.score.throws(new InvalidInputError('There was a problem'))
postApiScore(this.req, this.res)
buster.assert.calledWith(this.res.json, { success: false, error: 'There was a problem' })
},

'should pass along error message on wrapped input error': function () {
this.fakeScoreManager.score.throws(
new VError(
new InvalidInputError('There was a specific problem'),
'There was a general problem'))
postApiScore(this.req, this.res)
buster.assert.calledWith(this.res.json, { success: false, error: 'There was a general problem: There was a specific problem' })
},

'should signal an internal server error on error conditions other than input and rethrow exception': function () {
this.fakeScoreManager.score.throws(new Error('There was an internal problem'))
buster.assert.exception(() => postApiScore(this.req, this.res))
buster.assert.calledWith(this.res.json, { success: false, error: 'Internal Server Error' })
}
})

0 comments on commit fc2eb59

Please sign in to comment.