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.:
// Might be either a string or a decoded address: constdecoded = decodeCashAddress(address); // Now guaranteed to be a decoded address (error messages are thrown): consttokenAddress = assertSuccess(decoded); // The result can be used immediately: console.log(binToHex(tokenAddress.payload));
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 newError
, 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.: