apemanerror
Version:
Error parser for apeman.
50 lines (43 loc) • 1.2 kB
JavaScript
/**
* Convert errors from sequelize.
* @function fromSequelize
*/
;
var ErrorCodes = require('./error_codes');
var create = require('./create');
/** @lends fromSequelize */
function fromSequelize(error, options) {
options = options || {};
if (!error) {
return null;
}
if (Array.isArray(error)) {
return error.map(function (data) {
return fromSequelize(data, options);
});
}
var code = fromSequelize.parseCode(error.type);
var pointer = String(error.path).replace(/\./g, '/');
return create(code, pointer, {
trigger: 'Database',
context: { value: error.value },
namespace: options.namespace
});
}
Object.assign(fromSequelize, {
parseCode: function parseCode(type) {
var a = ErrorCodes;
switch (String(type).toLowerCase()) {
case 'unique violation':
return a.RESOURCE_DATA_CONFLICT_ERROR;
case 'notnull violation':
return a.VALUE_MISSING_ERROR;
case 'string violation':
return a.VALUE_STRING_ERROR;
default:
console.warn('[apemanerror] Unknown sequelize error type: ' + type);
return a.SOMETHING_WRONG_ERROR;
}
}
});
module.exports = fromSequelize;