error-handling-utils
Version:
Simple utility library for error handling
152 lines (126 loc) • 4.69 kB
JavaScript
;
var isString = require('lodash/isString');
var isArray = require('lodash/isArray');
var isObject = require('lodash/isObject');
var isFunction = require('lodash/isFunction');
/**
* @namespace ErrorHandling
* @summary error handling utilities.
* @example 'errors' object example:
* const errors = {
* email: ['Email is required!', 'Please, provide a valid email address!', ...],
* password: ['Please, at least 6 characters long!', ...],
* ...
* }
*/
var ErrorHandling = {};
//------------------------------------------------------------------------------
/**
* @summary Traverses errors object keys and fires callback when condition is met
* or when the end of the errors object is reached.
*/
ErrorHandling.traverseErrors = function (errors, cond) {
if (!isObject(errors)) {
throw new Error('Check your errors argument, it must be a valid object');
}
if (!isFunction(cond)) {
throw new Error('Check your cond argument, it must be a valid function');
}
var keys = Object.keys(errors);
var length = keys.length;
for (var i = 0; i < length; i += 1) {
// If condition is met, interrupt the for loop and return relevant data
if (cond(errors, keys[i])) {
return { index: i, key: keys[i] };
}
}
return { index: -1, key: null };
};
//------------------------------------------------------------------------------
/**
* @summary Returns the first not empty error.
*/
ErrorHandling.getFirstError = function (errors) {
if (!isObject(errors)) {
throw new Error('Check your errors argument, it must be a valid object');
}
// Condition: at least one of the error fields is not empty
var cond = function cond(err, key) {
return err[key].length > 0;
};
// Traverse the errors object and apply the condition above until it's met or
// the end of the object is reached.
var _ErrorHandling$traver = ErrorHandling.traverseErrors(errors, cond),
index = _ErrorHandling$traver.index,
key = _ErrorHandling$traver.key;
// Handle no errors found
if (index === -1) {
return { index: index, key: key, value: '' };
}
// Return first error data
return { index: index, key: key, value: errors[key][0] };
};
//------------------------------------------------------------------------------
/**
* @summary Returns 'true' if the errors object contains at least one non-empty
* error field.
*/
ErrorHandling.hasErrors = function (errors) {
if (!isObject(errors)) {
throw new Error('Check your errors argument, it must be a valid object');
}
return ErrorHandling.getFirstError(errors).index !== -1;
};
//------------------------------------------------------------------------------
/**
* @summary Returns all errors for the given field. A mutation (i18n for instance)
* can be applied to the array of errors before concatenation.
*/
ErrorHandling.getFieldErrors = function (errors, field, mutation) {
if (!isObject(errors)) {
throw new Error('Check your errors argument, it must be a valid object');
}
if (!isString(field)) {
throw new Error('Check your field argument, it must be a valid string');
}
var keys = Object.keys(errors);
if (keys.indexOf(field) === -1) {
throw new Error('Check your errors object, the field is not a valid key');
}
// Get array of errors for the given field
var array = errors[field];
if (!isArray(array)) {
throw new Error('Check your errors object, the value is not a valid array');
}
if (mutation && isFunction(mutation)) {
return array.map(function (item) {
return mutation(item);
}).join(' ');
}
return array.join(' ');
};
//------------------------------------------------------------------------------
/**
* @summary Clear error messages for the given field -or array of fields-
* leaving the remaining errors keys untouched.
*/
ErrorHandling.clearErrors = function (errors, fields) {
if (!isObject(errors)) {
throw new Error('Check your errors argument, it must be a valid object');
}
if (!isString(fields) && !isArray(fields)) {
throw new Error('Check your fields argument, it must be a valid string or array of strings');
}
var keys = Object.keys(errors);
var res = {}; // new errors object
// Remove errors if key matches the given field(s), preserve the original
// value otherwise.
keys.forEach(function (key) {
var isStringMatch = isString(fields) && fields === key;
var isArrayMatch = isArray(fields) && fields.indexOf(key) !== -1;
res[key] = isStringMatch || isArrayMatch ? [] : errors[key];
});
return res;
};
//------------------------------------------------------------------------------
module.exports = ErrorHandling;