Skip to content

Commit

Permalink
Merge pull request #20 from Jyrno42/error-handling-bugfix
Browse files Browse the repository at this point in the history
BugFixes: Error handling
  • Loading branch information
simonschmidt authored Aug 30, 2017
2 parents 28f3bf4 + 5cee77d commit 15211a0
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ tmp
coverage
.nyc_output
npm-debug.log
es/
es
.vscode
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
5 changes: 1 addition & 4 deletions src/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defaultParseErrors, defaultPrepareError } from './validationError';
import { parseErrors, prepareError } from './validationError';


const DEFAULTS = {
Expand All @@ -9,8 +9,8 @@ const DEFAULTS = {
headers: null,
cookies: null,

parseErrors: defaultParseErrors,
prepareError: defaultPrepareError,
parseErrors,
prepareError,

statusSuccess: [200, 201, 204],
statusValidationError: [400],
Expand Down
8 changes: 8 additions & 0 deletions test-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
27 changes: 26 additions & 1 deletion test/functional.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';


Expand Down Expand Up @@ -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);
},
},
};

0 comments on commit 15211a0

Please sign in to comment.