Use throws syntax for errors in SchemasRouter.
This commit is contained in:
@@ -188,8 +188,8 @@ describe('Schema', () => {
|
|||||||
foo: {type: 'String'}
|
foo: {type: 'String'}
|
||||||
}))
|
}))
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
expect(error.code).toEqual(Parse.Error.INVALID_CLASS_NAME)
|
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();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -216,7 +216,7 @@ describe('Schema', () => {
|
|||||||
Promise.all([p1,p2])
|
Promise.all([p1,p2])
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
expect(error.code).toEqual(Parse.Error.INVALID_CLASS_NAME);
|
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();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -524,7 +524,8 @@ describe('Schema', () => {
|
|||||||
.then(() => config.database.collectionExists('_Join:aRelation:HasPointersAndRelations'))
|
.then(() => config.database.collectionExists('_Join:aRelation:HasPointersAndRelations'))
|
||||||
.then(exists => {
|
.then(exists => {
|
||||||
if (!exists) {
|
if (!exists) {
|
||||||
fail('Relation collection should exist after save.');
|
fail('Relation collection ' +
|
||||||
|
'should exist after save.');
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.then(() => config.database.loadSchema())
|
.then(() => config.database.loadSchema())
|
||||||
|
|||||||
@@ -224,7 +224,7 @@ describe('schemas', () => {
|
|||||||
expect(response.statusCode).toEqual(400);
|
expect(response.statusCode).toEqual(400);
|
||||||
expect(body).toEqual({
|
expect(body).toEqual({
|
||||||
code: Parse.Error.INVALID_CLASS_NAME,
|
code: Parse.Error.INVALID_CLASS_NAME,
|
||||||
error: 'class name mismatch between B and A',
|
error: 'Class name mismatch between B and A.',
|
||||||
});
|
});
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
@@ -240,7 +240,7 @@ describe('schemas', () => {
|
|||||||
expect(response.statusCode).toEqual(400);
|
expect(response.statusCode).toEqual(400);
|
||||||
expect(body).toEqual({
|
expect(body).toEqual({
|
||||||
code: 135,
|
code: 135,
|
||||||
error: 'POST /schemas needs class name',
|
error: 'POST /schemas needs a class name.',
|
||||||
});
|
});
|
||||||
done();
|
done();
|
||||||
})
|
})
|
||||||
@@ -267,7 +267,7 @@ describe('schemas', () => {
|
|||||||
expect(response.statusCode).toEqual(400);
|
expect(response.statusCode).toEqual(400);
|
||||||
expect(body).toEqual({
|
expect(body).toEqual({
|
||||||
code: Parse.Error.INVALID_CLASS_NAME,
|
code: Parse.Error.INVALID_CLASS_NAME,
|
||||||
error: 'class A already exists',
|
error: 'Class A already exists.'
|
||||||
});
|
});
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
@@ -353,7 +353,7 @@ describe('schemas', () => {
|
|||||||
}, (error, response, body) => {
|
}, (error, response, body) => {
|
||||||
expect(response.statusCode).toEqual(400);
|
expect(response.statusCode).toEqual(400);
|
||||||
expect(body.code).toEqual(Parse.Error.INVALID_CLASS_NAME);
|
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();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -8,13 +8,10 @@ import PromiseRouter from '../PromiseRouter';
|
|||||||
import * as middleware from "../middlewares";
|
import * as middleware from "../middlewares";
|
||||||
|
|
||||||
function classNameMismatchResponse(bodyClass, pathClass) {
|
function classNameMismatchResponse(bodyClass, pathClass) {
|
||||||
return Promise.resolve({
|
throw new Parse.Error(
|
||||||
status: 400,
|
Parse.Error.INVALID_CLASS_NAME,
|
||||||
response: {
|
`Class name mismatch between ${bodyClass} and ${pathClass}.`
|
||||||
code: Parse.Error.INVALID_CLASS_NAME,
|
);
|
||||||
error: 'class name mismatch between ' + bodyClass + ' and ' + pathClass,
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function mongoSchemaAPIResponseFields(schema) {
|
function mongoSchemaAPIResponseFields(schema) {
|
||||||
@@ -63,23 +60,15 @@ function createSchema(req) {
|
|||||||
return classNameMismatchResponse(req.body.className, req.params.className);
|
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) {
|
if (!className) {
|
||||||
return Promise.resolve({
|
throw new Parse.Error(135, `POST ${req.path} needs a class name.`);
|
||||||
status: 400,
|
|
||||||
response: {
|
|
||||||
code: 135,
|
|
||||||
error: 'POST ' + req.path + ' needs class name',
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return req.config.database.loadSchema()
|
return req.config.database.loadSchema()
|
||||||
.then(schema => schema.addClassIfNotExists(className, req.body.fields))
|
.then(schema => schema.addClassIfNotExists(className, req.body.fields))
|
||||||
.then(result => ({ response: mongoSchemaToSchemaAPIResponse(result) }))
|
.then(result => ({ response: mongoSchemaToSchemaAPIResponse(result) }));
|
||||||
.catch(error => ({
|
|
||||||
status: 400,
|
|
||||||
response: error,
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function modifySchema(req) {
|
function modifySchema(req) {
|
||||||
|
|||||||
@@ -333,14 +333,10 @@ function buildMergedSchemaObject(mongoObject, putRequest) {
|
|||||||
// enabled) before calling this function.
|
// enabled) before calling this function.
|
||||||
Schema.prototype.addClassIfNotExists = function(className, fields) {
|
Schema.prototype.addClassIfNotExists = function(className, fields) {
|
||||||
if (this.data[className]) {
|
if (this.data[className]) {
|
||||||
return Promise.reject({
|
throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME, `Class ${className} already exists.`);
|
||||||
code: Parse.Error.INVALID_CLASS_NAME,
|
|
||||||
error: 'class ' + className + ' already exists',
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var mongoObject = mongoSchemaFromFieldsAndClassName(fields, className);
|
let mongoObject = mongoSchemaFromFieldsAndClassName(fields, className);
|
||||||
|
|
||||||
if (!mongoObject.result) {
|
if (!mongoObject.result) {
|
||||||
return Promise.reject(mongoObject);
|
return Promise.reject(mongoObject);
|
||||||
}
|
}
|
||||||
@@ -349,10 +345,7 @@ Schema.prototype.addClassIfNotExists = function(className, fields) {
|
|||||||
.then(result => result.ops[0])
|
.then(result => result.ops[0])
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
if (error.code === 11000) { //Mongo's duplicate key error
|
if (error.code === 11000) { //Mongo's duplicate key error
|
||||||
return Promise.reject({
|
throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME, `Class ${className} already exists.`);
|
||||||
code: Parse.Error.INVALID_CLASS_NAME,
|
|
||||||
error: 'class ' + className + ' already exists',
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
return Promise.reject(error);
|
return Promise.reject(error);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user