All files / helpers/error index.js

100% Statements 53/53
88% Branches 44/50
100% Functions 3/3
100% Lines 45/45

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118                      1853x 1853x 1853x 1853x 1853x 1853x 1853x 1853x   1853x                                           1853x 1853x 1853x     1853x 109x 108x   1x   1744x   1853x 1132x 537x 537x 595x 1x 1x 594x 594x         1x   1x 1x 1x 1x   1x 1x 1x     1x     1x 1x       1x       593x 593x       1853x 1700x 1700x 1700x     1853x                            
/* eslint-disable max-classes-per-file */
/* eslint-disable no-param-reassign */
import httpStatus from 'http-status';
import codeErrors from './codes';
import { t } from '../i18n';
 
/**
 * @extends Error
 */
class ExtendableError extends Error {
  constructor(message, code, status, details, stack, isPublic) {
    super(message);
    this.name = this.constructor.name;
    this.code = code;
    this.message = message;
    this.details = details;
    this.status = status;
    this.isPublic = isPublic;
    this.isOperational = true; // This is required since bluebird 4 doesn't append it anymore.
 
    if (stack) this.stack = stack;
  }
}
 
/**
 * Class representing an API error.
 * @extends ExtendableError
 */
class APIError extends ExtendableError {
  /**
   * Creates an API error.
   * @param {String} errorCode - Error code.
   * @param {Object} details - Error details.
   * @param {Number} status - HTTP status code of error.
   * @param {Boolean} isPublic - Whether the message should be visible to user or not.
   */
  constructor(
    errorCode,
    details,
    status = httpStatus.INTERNAL_SERVER_ERROR,
    isPublic = false
  ) {
    const detailsInfo = details;
    let overideMessage = '';
    let errorInfo = codeErrors[errorCode] && { ...codeErrors[errorCode] };
    let stack;
 
    if (!details && errorInfo) {
      if (typeof errorInfo.message === 'function') {
        details = errorInfo.message();
      } else {
        details = errorInfo.message;
      }
    } else if (!details) details = errorCode;
 
    if (details) {
      if (typeof details === 'string') {
        if (errorInfo) errorInfo.message = details;
        details = errorCode;
      } else if (typeof details === 'function') {
        Eif (errorInfo) errorInfo.message = details();
        details = errorCode;
      } else Eif (typeof details === 'object') {
        if (
          details.name &&
          details.name === 'SequelizeUniqueConstraintError' &&
          details.errors
        ) {
          const detailErrors = details.errors;
 
          for (let index = 0; index < detailErrors.length; index++) {
            const errorDb = detailErrors[index];
            const dbAttributes = errorDb.instance.rawAttributes;
            const dbKeys = Object.keys(dbAttributes);
 
            Eif (errorDb.type === 'unique violation') {
              const fieldName = errorDb.path;
              const fieldValue = errorDb.value;
 
              const fieldDbName =
                dbKeys.find((key) => dbAttributes[key].field === fieldName) ||
                fieldName;
 
              Eif (!overideMessage)
                overideMessage = `${t(
                  'BEE3482' /* Registro já existe! */
                )}\n\n`;
 
              overideMessage += `${fieldDbName}: ${fieldValue}\n`;
            }
          }
        } else {
          if (details.stack) ({ stack } = details);
          if (details.message) details = details.message;
        }
      }
    }
    if (!errorInfo) {
      errorInfo = { ...codeErrors.UNKNOWN_ERROR };
      errorInfo.status = status || errorInfo.status;
      errorInfo.message = overideMessage || detailsInfo || errorInfo.message;
    }
 
    super(
      typeof errorInfo.message === 'function'
        ? errorInfo.message()
        : errorInfo.message,
      errorInfo.code,
      errorInfo.status,
      details,
      stack,
      isPublic
    );
  }
}
 
export default APIError;