Function assertSuccess

  • A utility to handle error results by throwing an Error object.

    If the provided value is of type string, the contents of the string are thrown as a new Error, otherwise, the value is returned unmodified.

    This method is useful for eliminating string as a possible type from a resulting value, particularly in places where an error is never expected to occur in practice (i.e. no user or runtime input is involved), e.g.:

    import { assertSuccess, decodeCashAddress, binToHex } from '@bitauth/libauth';
    const address = 'bitcoincash:zq2azmyyv6dtgczexyalqar70q036yund5j2mspghf';

    // Might be either a string or a decoded address:
    const decoded = decodeCashAddress(address);
    // Now guaranteed to be a decoded address (error messages are thrown):
    const tokenAddress = assertSuccess(decoded);
    // The result can be used immediately:
    console.log(binToHex(tokenAddress.payload));

    Type Parameters

    • T

    Parameters

    • result: string | T

      A result which might be a string.

    • expectation: string = 'Expected a successful result, but encountered an error: '

      An optional, descriptive prefix for the error message thrown in failure cases. By default, Expected a successful result, but encountered an error: .

    Returns T

Generated using TypeDoc