Skip to content

Commit a34963c

Browse files
committed
Use throws syntax for errors in SchemasRouter.
1 parent 2afebf9 commit a34963c

File tree

3 files changed

+22
-40
lines changed

3 files changed

+22
-40
lines changed

spec/schemas.spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ describe('schemas', () => {
224224
expect(response.statusCode).toEqual(400);
225225
expect(body).toEqual({
226226
code: Parse.Error.INVALID_CLASS_NAME,
227-
error: 'class name mismatch between B and A',
227+
error: 'Class name mismatch between B and A.',
228228
});
229229
done();
230230
});
@@ -240,7 +240,7 @@ describe('schemas', () => {
240240
expect(response.statusCode).toEqual(400);
241241
expect(body).toEqual({
242242
code: 135,
243-
error: 'POST /schemas needs class name',
243+
error: 'POST /schemas needs a class name.',
244244
});
245245
done();
246246
})
@@ -267,7 +267,7 @@ describe('schemas', () => {
267267
expect(response.statusCode).toEqual(400);
268268
expect(body).toEqual({
269269
code: Parse.Error.INVALID_CLASS_NAME,
270-
error: 'class A already exists',
270+
error: 'Class A already exists.'
271271
});
272272
done();
273273
});
@@ -353,7 +353,7 @@ describe('schemas', () => {
353353
}, (error, response, body) => {
354354
expect(response.statusCode).toEqual(400);
355355
expect(body.code).toEqual(Parse.Error.INVALID_CLASS_NAME);
356-
expect(body.error).toEqual('class name mismatch between WrongClassName and NewClass');
356+
expect(body.error).toEqual('Class name mismatch between WrongClassName and NewClass.');
357357
done();
358358
});
359359
});

src/Routers/SchemasRouter.js

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,10 @@ import PromiseRouter from '../PromiseRouter';
88
import * as middleware from "../middlewares";
99

1010
function classNameMismatchResponse(bodyClass, pathClass) {
11-
return Promise.resolve({
12-
status: 400,
13-
response: {
14-
code: Parse.Error.INVALID_CLASS_NAME,
15-
error: 'class name mismatch between ' + bodyClass + ' and ' + pathClass,
16-
}
17-
});
11+
throw new Parse.Error(
12+
Parse.Error.INVALID_CLASS_NAME,
13+
`Class name mismatch between ${bodyClass} and ${pathClass}.`
14+
);
1815
}
1916

2017
function mongoSchemaAPIResponseFields(schema) {
@@ -63,23 +60,15 @@ function createSchema(req) {
6360
return classNameMismatchResponse(req.body.className, req.params.className);
6461
}
6562
}
66-
var className = req.params.className || req.body.className;
63+
64+
const className = req.params.className || req.body.className;
6765
if (!className) {
68-
return Promise.resolve({
69-
status: 400,
70-
response: {
71-
code: 135,
72-
error: 'POST ' + req.path + ' needs class name',
73-
}
74-
});
66+
throw new Parse.Error(135, `POST ${req.path} needs a class name.`);
7567
}
68+
7669
return req.config.database.loadSchema()
7770
.then(schema => schema.addClassIfNotExists(className, req.body.fields))
78-
.then(result => ({ response: mongoSchemaToSchemaAPIResponse(result) }))
79-
.catch(error => ({
80-
status: 400,
81-
response: error,
82-
}));
71+
.then(result => ({ response: mongoSchemaToSchemaAPIResponse(result) }));
8372
}
8473

8574
function modifySchema(req) {

src/Schema.js

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -333,29 +333,22 @@ function buildMergedSchemaObject(mongoObject, putRequest) {
333333
// enabled) before calling this function.
334334
Schema.prototype.addClassIfNotExists = function(className, fields) {
335335
if (this.data[className]) {
336-
return Promise.reject({
337-
code: Parse.Error.INVALID_CLASS_NAME,
338-
error: 'class ' + className + ' already exists',
339-
});
336+
throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME, `Class ${className} already exists.`);
340337
}
341338

342-
var mongoObject = mongoSchemaFromFieldsAndClassName(fields, className);
343-
339+
let mongoObject = mongoSchemaFromFieldsAndClassName(fields, className);
344340
if (!mongoObject.result) {
345341
return Promise.reject(mongoObject);
346342
}
347343

348344
return this.collection.insertOne(mongoObject.result)
349-
.then(result => result.ops[0])
350-
.catch(error => {
351-
if (error.code === 11000) { //Mongo's duplicate key error
352-
return Promise.reject({
353-
code: Parse.Error.INVALID_CLASS_NAME,
354-
error: 'class ' + className + ' already exists',
355-
});
356-
}
357-
return Promise.reject(error);
358-
});
345+
.then(result => result.ops[0])
346+
.catch(error => {
347+
if (error.code === 11000) { //Mongo's duplicate key error
348+
throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME, `Class ${className} already exists.`);
349+
}
350+
return Promise.reject(error);
351+
});
359352
};
360353

361354
// Returns a promise that resolves successfully to the new schema

0 commit comments

Comments
 (0)