Merge pull request #1245 from strongloop/duplicate-code
Add proper statusCode for duplicate
This commit is contained in:
commit
22cf48bd21
|
@ -220,8 +220,11 @@ Memory.prototype._createSync = function(model, data, fn) {
|
||||||
this.collection(model, {});
|
this.collection(model, {});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.collection(model)[id])
|
if (this.collection(model)[id]) {
|
||||||
return fn(new Error(g.f('Duplicate entry for %s.%s', model, idName)));
|
var error = new Error(g.f('Duplicate entry for %s.%s', model, idName));
|
||||||
|
error.statusCode = error.status = 409;
|
||||||
|
return fn(error);
|
||||||
|
}
|
||||||
|
|
||||||
this.collection(model)[id] = serialize(data);
|
this.collection(model)[id] = serialize(data);
|
||||||
fn(null, id);
|
fn(null, id);
|
||||||
|
|
|
@ -660,6 +660,26 @@ describe('Memory connector', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should refuse to create object with duplicate id', function(done) {
|
||||||
|
var ds = new DataSource({connector: 'memory'});
|
||||||
|
var Product = ds.define('ProductTest', {name: String}, {forceId: false});
|
||||||
|
ds.automigrate('ProductTest', function(err) {
|
||||||
|
if (err) return done(err);
|
||||||
|
|
||||||
|
Product.create({name: 'a-name'}, function(err, p) {
|
||||||
|
if (err) return done(err);
|
||||||
|
Product.create({id: p.id, name: 'duplicate'}, function(err) {
|
||||||
|
if (!err) {
|
||||||
|
return done(new Error('Create should have rejected duplicate id.'));
|
||||||
|
}
|
||||||
|
err.message.should.match(/duplicate/i);
|
||||||
|
err.statusCode.should.equal(409);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('automigrate', function() {
|
describe('automigrate', function() {
|
||||||
var ds;
|
var ds;
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
|
|
Loading…
Reference in New Issue