Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use 500 as default status code for errors, instead of 400 #140

Merged
merged 1 commit into from
Apr 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ fetcher.updateOptions({

## Error Handling

When an error occurs in your Fetchr CRUD method, you should return an error object to the callback. The error object should contain a `statusCode` (default 400) and `output` property that contains a JSON serializable object which will be sent to the client.
When an error occurs in your Fetchr CRUD method, you should return an error object to the callback. The error object should contain a `statusCode` (default 500) and `output` property that contains a JSON serializable object which will be sent to the client.

```js
module.exports = {
Expand Down
4 changes: 2 additions & 2 deletions libs/fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function sanitizeResourceName(resource) {
* @return {Object} object with resolved statusCode & output
*/
function getErrorResponse(err) {
var statusCode = err.statusCode || 400;
var statusCode = err.statusCode || 500;
var output = {
message: 'request failed'
};
Expand Down Expand Up @@ -571,7 +571,7 @@ module.exports = Fetcher;
/**
* @callback Fetcher~fetcherCallback
* @param {Object} err The request error, pass null if there was no error. The data and meta parameters will be ignored if this parameter is not null.
* @param {number} [err.statusCode=400] http status code to return
* @param {number} [err.statusCode=500] http status code to return
* @param {string} [err.message=request failed] http response body
* @param {Object} data request result
* @param {Object} [meta] request meta-data
Expand Down
28 changes: 14 additions & 14 deletions tests/unit/libs/fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,27 +245,27 @@ describe('Server Fetcher', function () {
};

it('should respond to POST api request with default error details',
makePostApiErrorTest({}, 400, {message: 'request failed'}));
makePostApiErrorTest({}, 500, {message: 'request failed'}));

it('should respond to POST api request with custom error status code',
makePostApiErrorTest({statusCode: 500}, 500, {message: 'request failed'}));
makePostApiErrorTest({statusCode: 400}, 400, {message: 'request failed'}));

it('should respond to POST api request with custom error message',
makePostApiErrorTest({message: 'Error message...'}, 400, {message: 'Error message...'}));
makePostApiErrorTest({message: 'Error message...'}, 500, {message: 'Error message...'}));

it('should respond to POST api request with no leaked error information',
makePostApiErrorTest({statusCode: 500, danger: 'zone'}, 500, {message: 'request failed'}));
makePostApiErrorTest({statusCode: 400, danger: 'zone'}, 400, {message: 'request failed'}));


describe('should respond to POST api request with custom output', function() {
it('using json object',
makePostApiErrorTest({statusCode: 500, output: {
makePostApiErrorTest({statusCode: 400, output: {
message: 'custom message',
foo : 'bar',
}}, 500, {message: 'custom message', 'foo': 'bar'}));
}}, 400, {message: 'custom message', 'foo': 'bar'}));

it('using json array',
makePostApiErrorTest({statusCode: 500, output: [1, 2]}, 500, [1, 2]));
makePostApiErrorTest({statusCode: 400, output: [1, 2]}, 400, [1, 2]));
});
});

Expand Down Expand Up @@ -508,26 +508,26 @@ describe('Server Fetcher', function () {
};

it('should respond to GET api request with default error details',
makeGetApiErrorTest({}, 400, {message: 'request failed'}));
makeGetApiErrorTest({}, 500, {message: 'request failed'}));

it('should respond to GET api request with custom error status code',
makeGetApiErrorTest({statusCode: 500}, 500, {message: 'request failed'}));
makeGetApiErrorTest({statusCode: 400}, 400, {message: 'request failed'}));

it('should respond to GET api request with no leaked error information',
makeGetApiErrorTest({statusCode: 500, danger: 'zone'}, 500, {message: 'request failed'}));
makeGetApiErrorTest({statusCode: 400, danger: 'zone'}, 400, {message: 'request failed'}));

it('should respond to GET api request with custom error message',
makeGetApiErrorTest({message: 'Error message...'}, 400, {message: 'Error message...'}));
makeGetApiErrorTest({message: 'Error message...'}, 500, {message: 'Error message...'}));

describe('should respond to GET api request with custom output', function() {
it('using json object',
makeGetApiErrorTest({statusCode: 500, output: {
makeGetApiErrorTest({statusCode: 400, output: {
message: 'custom message',
foo : 'bar',
}}, 500, {message: 'custom message', 'foo': 'bar'}));
}}, 400, {message: 'custom message', 'foo': 'bar'}));

it('using json array',
makeGetApiErrorTest({statusCode: 500, output: [1, 2]}, 500, [1, 2]));
makeGetApiErrorTest({statusCode: 400, output: [1, 2]}, 400, [1, 2]));
});
});

Expand Down