2020-02-08 22:23:33 +00:00
|
|
|
// Copyright IBM Corp. 2017,2019. All Rights Reserved.
|
2017-05-30 18:18:40 +00:00
|
|
|
// Node module: loopback-connector-mysql
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2020-08-28 06:43:09 +00:00
|
|
|
const setHttpCode = require('../lib/set-http-code');
|
|
|
|
const should = require('./init.js');
|
2017-05-30 18:18:40 +00:00
|
|
|
|
|
|
|
describe('setHttpCode', function() {
|
|
|
|
describe('should set statusCode', function() {
|
|
|
|
testErrorCode('404 on db not found', 'ER_DB_DROP_EXISTS: Nope', 404);
|
|
|
|
testErrorCode('404 on table not found', 'ER_BAD_TABLE_ERROR: Nope', 404);
|
|
|
|
|
|
|
|
testErrorCode('422 on duplicates', 'ER_DUP_ENTRY: Duplicate entry', 422);
|
|
|
|
testErrorCode('422 on cannot create db', 'ER_CANT_CREATE_DB: Oops', 422);
|
|
|
|
testErrorCode('422 on db already exists', 'ER_DB_CREATE_EXISTS: Oops', 422);
|
|
|
|
testErrorCode('422 on db not found', 'ER_TABLE_EXISTS_ERROR: Nope', 422);
|
|
|
|
|
|
|
|
testErrorCode('500 on unknown errors', 'FATAL: Sadness happened', 500);
|
|
|
|
|
|
|
|
function testErrorCode(name, msg, expected) {
|
|
|
|
it(name, function() {
|
2020-08-28 06:43:09 +00:00
|
|
|
let err = new Error(msg);
|
2017-05-30 18:18:40 +00:00
|
|
|
err = setHttpCode(err);
|
|
|
|
should.exist(err.statusCode);
|
|
|
|
should.equal(err.statusCode, expected);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2024-04-14 11:49:51 +00:00
|
|
|
|
|
|
|
it('should set statusCode from code', function() {
|
|
|
|
let err = {
|
|
|
|
message: 'Duplicate entry \'value\' for key \'key_name\'',
|
|
|
|
code: 'ER_DUP_ENTRY',
|
|
|
|
};
|
|
|
|
err = setHttpCode(err);
|
|
|
|
should.exist(err.statusCode);
|
|
|
|
should(err instanceof Error);
|
|
|
|
should.equal(err.statusCode, 422);
|
|
|
|
});
|
|
|
|
|
2017-05-30 18:18:40 +00:00
|
|
|
it('should do nothing without error', function() {
|
|
|
|
should.doesNotThrow(setHttpCode);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should convert strings to errors', function() {
|
2020-08-28 06:43:09 +00:00
|
|
|
let err = 'REALLY_BAD: Something truly awful occurred.';
|
2017-05-30 18:18:40 +00:00
|
|
|
err = setHttpCode(err);
|
|
|
|
should.exist(err.statusCode);
|
|
|
|
should(err instanceof Error);
|
|
|
|
should.equal(err.statusCode, 500);
|
|
|
|
});
|
|
|
|
});
|