diff --git a/.gitignore b/.gitignore index b1993f4..ca55c4d 100644 --- a/.gitignore +++ b/.gitignore @@ -8,5 +8,5 @@ tmp coverage .nyc_output npm-debug.log -es/ +es .vscode diff --git a/package.json b/package.json index 80609b8..d976d65 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "tg-resources", - "version": "2.0.0-alpha.2", + "version": "2.0.0-alpha.3", "description": "Abstractions on-top of `superagent` (or other Ajax libaries) for communication with REST.", "main": "./dist/index.js", "jsnext:main": "./es/index.js", diff --git a/src/base.js b/src/base.js index c428921..d59e21c 100644 --- a/src/base.js +++ b/src/base.js @@ -122,10 +122,7 @@ class GenericResource { } else if (this.config.statusValidationError.indexOf(res.status) !== -1) { // Got statusValidationError response code, lets throw RequestValidationError throw this.mutateError( - new RequestValidationError({ - statusCode: res.status, - responseText: res.text, - }, this.config), + new RequestValidationError(res.status, res.text, this.config), res, ); } else { diff --git a/src/constants.js b/src/constants.js index 9cd050e..4435093 100644 --- a/src/constants.js +++ b/src/constants.js @@ -1,4 +1,4 @@ -import { defaultParseErrors, defaultPrepareError } from './validationError'; +import { parseErrors, prepareError } from './validationError'; const DEFAULTS = { @@ -9,8 +9,8 @@ const DEFAULTS = { headers: null, cookies: null, - parseErrors: defaultParseErrors, - prepareError: defaultPrepareError, + parseErrors, + prepareError, statusSuccess: [200, 201, 204], statusValidationError: [400], diff --git a/test-server.js b/test-server.js index 238c66e..5543edb 100644 --- a/test-server.js +++ b/test-server.js @@ -141,4 +141,12 @@ app.delete('/dogs/:id', (req, res) => { } }); +app.post('/error400', (req, res) => { + res.status(400).json({ + 'name': [ + 'This field is required.' + ] + }); +}); + export default () => app.listen(port); diff --git a/test/functional.js b/test/functional.js index 6b91ad9..d98fcce 100644 --- a/test/functional.js +++ b/test/functional.js @@ -3,7 +3,7 @@ import { spy } from 'sinon'; import listen from '../test-server'; -import { Resource, Response } from '../src'; +import { Resource, Response, RequestValidationError } from '../src'; import { isSubClass } from '../src/typeChecks'; @@ -345,5 +345,30 @@ export default { }, done); }); }, + + 'statusValidationError is handled properly': (done) => { + const res = new Resource('/error400', { + apiRoot: 'http://127.0.0.1:3000', + }); + + res.post(null, { name: '' }).then(() => { + done(new Error('Expected request to fail')); + }, (err) => { + // the error must RequestValidationError + expect(err).to.be.an.instanceof(RequestValidationError); + + // statusCode must be correct + expect(err.statusCode).to.equal(400); + + // hasError must be true + expect(err.hasError()).to.be.equal(true); + + // errors should be correct + expect(err.errors.toString()).to.equal('name: This field is required.'); + + // all good + done(); + }).catch(done); + }, }, };