Skip to content

Commit

Permalink
Use throws syntax for errors in SchemasRouter.
Browse files Browse the repository at this point in the history
  • Loading branch information
nlutsenko committed Mar 3, 2016
1 parent 2afebf9 commit 99cb05e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 44 deletions.
9 changes: 5 additions & 4 deletions spec/Schema.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ describe('Schema', () => {
foo: {type: 'String'}
}))
.catch(error => {
expect(error.code).toEqual(Parse.Error.INVALID_CLASS_NAME)
expect(error.error).toEqual('class NewClass already exists');
expect(error.code).toEqual(Parse.Error.INVALID_CLASS_NAME);
expect(error.message).toEqual('Class NewClass already exists.');
done();
});
});
Expand All @@ -216,7 +216,7 @@ describe('Schema', () => {
Promise.all([p1,p2])
.catch(error => {
expect(error.code).toEqual(Parse.Error.INVALID_CLASS_NAME);
expect(error.error).toEqual('class NewClass already exists');
expect(error.message).toEqual('Class NewClass already exists.');
done();
});
});
Expand Down Expand Up @@ -524,7 +524,8 @@ describe('Schema', () => {
.then(() => config.database.collectionExists('_Join:aRelation:HasPointersAndRelations'))
.then(exists => {
if (!exists) {
fail('Relation collection should exist after save.');
fail('Relation collection ' +
'should exist after save.');
}
})
.then(() => config.database.loadSchema())
Expand Down
8 changes: 4 additions & 4 deletions spec/schemas.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ describe('schemas', () => {
expect(response.statusCode).toEqual(400);
expect(body).toEqual({
code: Parse.Error.INVALID_CLASS_NAME,
error: 'class name mismatch between B and A',
error: 'Class name mismatch between B and A.',
});
done();
});
Expand All @@ -240,7 +240,7 @@ describe('schemas', () => {
expect(response.statusCode).toEqual(400);
expect(body).toEqual({
code: 135,
error: 'POST /schemas needs class name',
error: 'POST /schemas needs a class name.',
});
done();
})
Expand All @@ -267,7 +267,7 @@ describe('schemas', () => {
expect(response.statusCode).toEqual(400);
expect(body).toEqual({
code: Parse.Error.INVALID_CLASS_NAME,
error: 'class A already exists',
error: 'Class A already exists.'
});
done();
});
Expand Down Expand Up @@ -353,7 +353,7 @@ describe('schemas', () => {
}, (error, response, body) => {
expect(response.statusCode).toEqual(400);
expect(body.code).toEqual(Parse.Error.INVALID_CLASS_NAME);
expect(body.error).toEqual('class name mismatch between WrongClassName and NewClass');
expect(body.error).toEqual('Class name mismatch between WrongClassName and NewClass.');
done();
});
});
Expand Down
29 changes: 9 additions & 20 deletions src/Routers/SchemasRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@ import PromiseRouter from '../PromiseRouter';
import * as middleware from "../middlewares";

function classNameMismatchResponse(bodyClass, pathClass) {
return Promise.resolve({
status: 400,
response: {
code: Parse.Error.INVALID_CLASS_NAME,
error: 'class name mismatch between ' + bodyClass + ' and ' + pathClass,
}
});
throw new Parse.Error(
Parse.Error.INVALID_CLASS_NAME,
`Class name mismatch between ${bodyClass} and ${pathClass}.`
);
}

function mongoSchemaAPIResponseFields(schema) {
Expand Down Expand Up @@ -63,23 +60,15 @@ function createSchema(req) {
return classNameMismatchResponse(req.body.className, req.params.className);
}
}
var className = req.params.className || req.body.className;

const className = req.params.className || req.body.className;
if (!className) {
return Promise.resolve({
status: 400,
response: {
code: 135,
error: 'POST ' + req.path + ' needs class name',
}
});
throw new Parse.Error(135, `POST ${req.path} needs a class name.`);
}

return req.config.database.loadSchema()
.then(schema => schema.addClassIfNotExists(className, req.body.fields))
.then(result => ({ response: mongoSchemaToSchemaAPIResponse(result) }))
.catch(error => ({
status: 400,
response: error,
}));
.then(result => ({ response: mongoSchemaToSchemaAPIResponse(result) }));
}

function modifySchema(req) {
Expand Down
25 changes: 9 additions & 16 deletions src/Schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,29 +333,22 @@ function buildMergedSchemaObject(mongoObject, putRequest) {
// enabled) before calling this function.
Schema.prototype.addClassIfNotExists = function(className, fields) {
if (this.data[className]) {
return Promise.reject({
code: Parse.Error.INVALID_CLASS_NAME,
error: 'class ' + className + ' already exists',
});
throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME, `Class ${className} already exists.`);
}

var mongoObject = mongoSchemaFromFieldsAndClassName(fields, className);

let mongoObject = mongoSchemaFromFieldsAndClassName(fields, className);
if (!mongoObject.result) {
return Promise.reject(mongoObject);
}

return this.collection.insertOne(mongoObject.result)
.then(result => result.ops[0])
.catch(error => {
if (error.code === 11000) { //Mongo's duplicate key error
return Promise.reject({
code: Parse.Error.INVALID_CLASS_NAME,
error: 'class ' + className + ' already exists',
});
}
return Promise.reject(error);
});
.then(result => result.ops[0])
.catch(error => {
if (error.code === 11000) { //Mongo's duplicate key error
throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME, `Class ${className} already exists.`);
}
return Promise.reject(error);
});
};

// Returns a promise that resolves successfully to the new schema
Expand Down

0 comments on commit 99cb05e

Please sign in to comment.