UNPKG

apemanerror

Version:
50 lines (42 loc) 1.15 kB
/** * Convert errors from sequelize. * @function fromSequelize */ 'use strict' const ErrorCodes = require('./error_codes') const create = require('./create') /** @lends fromSequelize */ function fromSequelize (error, options) { options = options || {} if (!error) { return null } if (Array.isArray(error)) { return error.map((data) => fromSequelize(data, options)) } let code = fromSequelize.parseCode(error.type) let pointer = String(error.path).replace(/\./g, '/') return create(code, pointer, { trigger: 'Database', context: { value: error.value }, namespace: options.namespace }) } Object.assign(fromSequelize, { parseCode (type) { const 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