All files / src/api ContractException.ts

100% Statements 13/13
100% Branches 9/9
100% Functions 3/3
100% Lines 13/13

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 5139x           39x                   36x   35x   35x 35x                 12x 7x 5x 3x   2x                     39x 12x      
import { messageCheck, used } from "@jonloucks/contracts-ts/auxiliary/Checks";
 
/**
 * Runtime exception thrown for Contract related problems.
 * For example, when claimed contract is not bound to a promisor.
 */
export class ContractException extends Error {
 
  /**
   * Create a new ContractException
   *
   * @param message the message for this exception
   * @param thrown  the cause of this exception, null is allowed
   */
  public constructor(message: string, thrown: Error | null = null) {
    // super(messageCheck(message), thrown || undefined);
    super(messageCheck(message));
 
    used(thrown);
 
    this.name = "ContractException";
    Object.setPrototypeOf(this, ContractException.prototype)
  }
 
  /**
   * Ensure something that was caught is rethrown as a ContractException
   * @param caught the caught value
   * @param message the optional message to use if caught is not an ContractException
   */
  static rethrow(caught: unknown, message?: string): never {
    if (guard(caught)) {
      throw caught;
    } else if (caught instanceof Error) {
      throw new ContractException(message ?? caught.message, caught);
    } else {
      throw new ContractException(message ?? "Unknown type of caught value.");
    }
  }
}
 
/**
 * Type guard for ContractException
 *
 * @param value the value to check
 * @return true if value is ContractException, false otherwise
 */
export function guard(value: unknown): value is ContractException {
  return value instanceof ContractException;
}