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 _ = require('lodash');
|
2017-05-30 18:18:40 +00:00
|
|
|
|
2020-08-28 06:43:09 +00:00
|
|
|
const codes = {
|
2017-05-30 18:18:40 +00:00
|
|
|
'404': [
|
|
|
|
'ER_DB_DROP_EXISTS',
|
|
|
|
'ER_BAD_TABLE_ERROR',
|
|
|
|
],
|
|
|
|
'422': [
|
|
|
|
'ER_DUP_ENTRY',
|
|
|
|
'ER_CANT_CREATE_DB',
|
|
|
|
'ER_DB_CREATE_EXISTS',
|
|
|
|
'ER_TABLE_EXISTS_ERROR',
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* Translate MySQL error codes into HTTP Errors.
|
|
|
|
* If an error would be better represented by a code that isn't 500,
|
|
|
|
* add your SQL error to the correct HTTP code array above!
|
|
|
|
*/
|
|
|
|
module.exports = function(err) {
|
|
|
|
if (!err) {
|
|
|
|
return;
|
|
|
|
} else if (!(err instanceof Error)) {
|
|
|
|
err = new Error(err); // Sucks that we weren't given an error object...
|
|
|
|
}
|
|
|
|
// Find error prefix
|
2020-08-28 06:43:09 +00:00
|
|
|
const msg = err.message;
|
|
|
|
const sqlError = msg.substring(0, msg.indexOf(':'));
|
2017-05-30 18:18:40 +00:00
|
|
|
|
2020-08-28 06:43:09 +00:00
|
|
|
for (const code in codes) {
|
2017-05-30 18:18:40 +00:00
|
|
|
if (_.includes(codes[code], sqlError)) {
|
|
|
|
err.statusCode = code;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!err.statusCode) err.statusCode = 500;
|
|
|
|
return err;
|
|
|
|
};
|