Merge pull request #2754 from sebgie/issue#2187

Improve status codes
This commit is contained in:
Hannah Wolfe 2014-05-24 09:14:23 +03:00
commit b6528c0494
6 changed files with 25 additions and 5 deletions

View File

@ -43,7 +43,7 @@ db = {
* - If there is no path
* - If the name doesn't have json in it
*/
return when.reject(new errors.InternalServerError('Please select a .json file to import.'));
return when.reject(new errors.UnsupportedMediaTypeError('Please select a .json file to import.'));
}
return api.settings.read({key: 'databaseVersion', context: { internal: true }}).then(function (response) {

View File

@ -208,6 +208,9 @@ http = function (apiMethod) {
}).then(function addLocationHeader(header) {
if (header) {
res.set({'Location': header});
// The location header indicates that a new object was created.
// In this case the status code should be 201 Created
res.status(201);
}
// Add Content-Disposition Header

View File

@ -12,6 +12,7 @@ var _ = require('lodash'),
RequestEntityTooLargeError = require('./requesttoolargeerror'),
UnauthorizedError = require('./unauthorizederror'),
ValidationError = require('./validationerror'),
UnsupportedMediaTypeError = require('./unsupportedmediaerror'),
EmailError = require('./emailerror'),
errors,
@ -258,4 +259,5 @@ module.exports.NoPermissionError = NoPermissionError;
module.exports.UnauthorizedError = UnauthorizedError;
module.exports.ValidationError = ValidationError;
module.exports.RequestEntityTooLargeError = RequestEntityTooLargeError;
module.exports.UnsupportedMediaTypeError = UnsupportedMediaTypeError;
module.exports.EmailError = EmailError;

View File

@ -0,0 +1,15 @@
// # Unsupported Media Type
// Custom error class with status code and type prefilled.
function UnsupportedMediaTypeError(message) {
this.message = message;
this.stack = new Error().stack;
this.code = 415;
this.type = this.name;
}
UnsupportedMediaTypeError.prototype = Object.create(Error.prototype);
UnsupportedMediaTypeError.prototype.name = "UnsupportedMediaTypeError";
module.exports = UnsupportedMediaTypeError;

View File

@ -86,7 +86,7 @@ describe('Notifications API', function () {
request.post(testUtils.API.getApiQuery('notifications/'))
.set('X-CSRF-Token', csrfToken)
.send(newNotification)
.expect(200)
.expect(201)
.end(function (err, res) {
if (err) {
return done(err);
@ -119,7 +119,7 @@ describe('Notifications API', function () {
request.post(testUtils.API.getApiQuery('notifications/'))
.set('X-CSRF-Token', csrfToken)
.send(newNotification)
.expect(200)
.expect(201)
.end(function (err, res) {
if (err) {
return done(err);

View File

@ -355,7 +355,7 @@ describe('Post API', function () {
request.post(testUtils.API.getApiQuery('posts/?include=tags'))
.set('X-CSRF-Token', csrfToken)
.send(newPost)
.expect(200)
.expect(201)
.end(function (err, res) {
if (err) {
return done(err);
@ -713,7 +713,7 @@ describe('Post API', function () {
request.post(testUtils.API.getApiQuery('posts/'))
.set('X-CSRF-Token', csrfToken)
.send(newPost)
.expect(200)
.expect(201)
.end(function (err ,res) {
if (err) {
return done(err);