All files / src/api TwitarrError.ts

75% Statements 21/28
60% Branches 6/10
75% Functions 3/4
75% Lines 21/28

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 9912x             12x                                               30x                                             24x 24x 24x 24x 24x 24x     2x   2x   2x           24x         24x             20x   5x 5x 5x 5x     7x   13x        
import { ErrorMessage } from './ErrorMessage';
import { IErrorParameters } from './IErrorParameters';
 
/**
 * Represents a Twitarr error.
 * @module TwitarrError
 */
export class TwitarrError extends Error {
  /**
   * The response status code, if any.
   * @hidden
   */
  private statusCode: number;
 
  /**
   * The options provided as part of the request that resulted in this error.
   */
  private options: any;
 
  /**
   * Any error messages that were returned from Twit-arr.
   */
  private errors: ErrorMessage | IErrorParameters;
 
  /**
   * Any other useful data.
   */
  private data: any;
 
  /** The error code associated with this error. */
  public get code() {
    return this.statusCode;
  }
 
  /** The error message */
  public get simpleErrorMessage() {
    if (this.message) {
      return this.message;
    }
    if (this.errors instanceof ErrorMessage && this.errors.messages.length > 0) {
      return this.errors.messages[0];
    } else if (this.errors && Object.keys(this.errors).length > 0) {
      const key = Object.keys(this.errors)[0];
      return this.errors[key][0];
    }
    return undefined;
  }
  /**
   * Create a new error.
   * @constructor
   * @param message - The error message.
   * @param code - An optional error code to associate with the error.
   */
  public constructor(message?: string, code?: number, errors?: any, options?: any, data?: any) {
    super(message);
    const self = this;
    self.name = self.constructor.name;
    self.statusCode = code;
    self.options = options;
    self.data = data;
 
    if (typeof errors === 'string' || errors instanceof String) {
      self.errors = new ErrorMessage(errors as string);
    } else if (Array.isArray(errors)) {
      self.errors = new ErrorMessage(errors as string[]);
    } else if (errors && Object.keys(errors).length > 0) {
      self.errors = errors as IErrorParameters;
    } else if (errors) {
      console.warn('Unsure how to decode error response:', errors);
    }
 
    if (typeof Error.captureStackTrace === 'function') {
      Error.captureStackTrace(this, this.constructor);
    } else {
      this.stack = new Error(message).stack;
    }
    // workaround, see http://bit.ly/2vllGdD
    Object.setPrototypeOf(this, TwitarrError.prototype);
  }
 
  /**
   * Returns a string representation of this error.
   */
  public toString() {
    let message = this.message;
    if (this.errors) {
      let stringified = JSON.stringify(this.errors);
      stringified = stringified.replace(/\\"/g, '\uFFFF');
      stringified = stringified.replace(/\"([^"]+)\"/g, '$1').replace(/\uFFFF/g, '\\"');
      message = (message ? message + ': ' : '') + stringified;
    }
    if (this.code) {
      return 'Error ' + this.code + ': ' + message;
    } else {
      return 'Error: ' + message;
    }
  }
}