All files / src/errors OAuthError.js

56.41% Statements 22/39
60.6% Branches 20/33
100% Functions 3/3
56.41% Lines 22/39

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                                21x 3x       18x     18x     18x 18x 18x     18x                                                           18x       18x     18x 18x                 2x   2x 2x     2x 1x     2x 1x     2x               1x                     1x
'use strict';
 
/**
 * Base error class for OAuth related errors
 * @extends Error
 */
class OAuthError extends Error {
  /**
   * Create a new OAuthError
   * @param {string} message - Error message
   * @param {string} [code] - Error code
   * @param {string} [description] - Error description
   * @param {string} [intuitTid] - Intuit transaction ID
   * @throws {TypeError} If message is not a string
   */
  constructor(message, code, description, intuitTid) {
    if (typeof message !== 'string') {
      throw new TypeError('Error message must be a string');
    }
 
    // Call parent constructor
    super(message);
 
    // Set error name
    this.name = 'OAuthError';
 
    // Set error properties
    this.message = message;
    this.description = description || message;
    this.intuitTid = intuitTid || '';
 
    // Set error code
    Iif (typeof code === 'number' || (typeof code === 'string' && /^\d{3}$/.test(code))) {
      const statusCode = parseInt(code, 10);
      switch (statusCode) {
        case 400:
          this.code = '400';
          break;
        case 401:
          this.code = '401';
          break;
        case 403:
          this.code = '403';
          break;
        case 404:
          this.code = '404';
          break;
        case 429:
          this.code = '429';
          break;
        case 500:
          this.code = '500';
          break;
        case 502:
        case 503:
        case 504:
          this.code = statusCode.toString();
          break;
        default:
          this.code = code;
      }
    } else {
      this.code = code || 'OAUTH_ERROR';
    }
 
    // Ensure proper prototype chain for instanceof checks
    Object.setPrototypeOf(this, OAuthError.prototype);
 
    // Capture stack trace
    Eif (Error.captureStackTrace) {
      Error.captureStackTrace(this, OAuthError);
    }
  }
 
  /**
   * Convert error to string representation
   * @returns {string} String representation of the error
   */
  toString() {
    let errorString = `${this.name}: ${this.message}`;
    
    Eif (this.code) {
      errorString += ` (${this.code})`;
    }
    
    if (this.description && this.description !== this.message) {
      errorString += ` - ${this.description}`;
    }
    
    if (this.intuitTid) {
      errorString += ` [TID: ${this.intuitTid}]`;
    }
    
    return errorString;
  }
 
  /**
   * Convert error to JSON representation
   * @returns {Object} JSON representation of the error
   */
  toJSON() {
    return {
      name: this.name,
      message: this.message,
      code: this.code,
      description: this.description,
      intuitTid: this.intuitTid,
      stack: this.stack,
    };
  }
}
 
module.exports = OAuthError;