Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface ITry<T>

A try represents the result of an operation, that may have succeeded or failed, and contains several methods for handling success and error cases.

const t = T.of(() => JSON.parse(input)) // => Try[success=true/false]
t.orElse({}); // => The parsed JSON object or an empty object.

Several methods such as {@link ITry.map} take an abitrary callback for manipulating the success or error value. If these methods throw an error, the returned ITry is not successful and contains an error with the stacktrace of the original error added.

const t = T.of(() => JSON.parse(input))
t.mapError(() => {throw new Error("Bad handler")})

Type parameters

  • T

    Type of the encapsulated success value.

Hierarchy

  • ITry

Implemented by

Index

Properties

success

success: boolean

Whether this ITry represents a successful or failed operation.

Methods

catch

  • catch(mapper: TypedFunction<Error, T | ITry<T>>): ITry<T>
  • Similar to {@link #orTry}, but works like a thenable-object. The handler is allowed to return either the new value directly, or a ITry with the new value.

    TryFactory.error("bad").catch(e => 0)
    
    TryFactory.error("bad").catch(e => Try.success(0))
    

    Parameters

    • mapper: TypedFunction<Error, T | ITry<T>>

    Returns ITry<T>

    The new value, wrapped in a ITry for encapsulating errors.

convert

  • convert<S>(mapper: TypedFunction<T, S>, backup?: TypedFunction<Error, S>): ITry<S>
  • Computes a new value by using either the success handler, or the backup handler. If the mapper throws, the backup is applied. If the backup throws as well, the returned ITry is not successful. If the backup handler is not given and this try is unsuccessful, return a try with the same error.

    TryFactor.success(9).convert(x => x * 2);
    // => 18
    
    TryFactor.error("bad").convert(x => x * x, e => e.message.length);
    // => 3
    

    Type parameters

    • S

      Type of the new value.

    Parameters

    • mapper: TypedFunction<T, S>

      Handler that maps the successful value to a new value.

    • Optional backup: TypedFunction<Error, S>

      Handler that maps the error to a new value.

    Returns ITry<S>

    The new value, wrapped in a ITry for encapsulating errors.

flatConvert

  • flatConvert<S>(operation: TypedFunction<T, ITry<S>>, backup?: TypedFunction<Error, ITry<S>>): ITry<S>
  • Computes a new value by using either the success handler, or the backup handler. If the mapper throws, the backup is applied. If the backup throws as well, the returned Try is not successful. If the backup handler is not given and this try is unsuccessful, return a try with the same error. Similar to #convert, but the handlers return a Try directly.

    TryFactor.success("9").convert(x => Try.of(() => JSON.parse(x)));
    // => Try[value=9]
    

    Type parameters

    • S

      Type of the new value.

    Parameters

    • operation: TypedFunction<T, ITry<S>>
    • Optional backup: TypedFunction<Error, ITry<S>>

      Handler that maps the error to a new value.

    Returns ITry<S>

    The new value, wrapped in a ITry for encapsulating errors.

ifAbsent

  • ifAbsent(consumer: Consumer<Error>): this
  • Calls the error handler iff this try is not successful, or does nothing otherwise.

    TryFactory.success(9).ifAbsent(console.log)
    // prints nothing
    
    TryFactory.error(new Error).ifAbsent(console.info)
    // Informs about error
    

    Parameters

    • consumer: Consumer<Error>

    Returns this

    This ITry..

ifPresent

  • ifPresent(success: Consumer<T>, error?: Consumer<Error>): this
  • Calls the success handler with the successful value, or the error handler with the error.

    TryFactory.success(9).ifPresent(console.info, console.warn)
    // informs about 9
    
    TryFactory.error(new Error).ifPresent(console.info, console.warn)
    // warns about error
    

    Parameters

    • success: Consumer<T>
    • Optional error: Consumer<Error>

    Returns this

    This ITry.

include

  • include(predicate: Predicate<T>): ITry<T>
  • If operation was succesful and the value matches the predicate, returns a ITry with that value, otherwise a ITry with a No such element error.

    Parameters

    • predicate: Predicate<T>

      Test to perform on the success value.

    Returns ITry<T>

    A ITry with the current success value iff it exists and matches the predicate.

iterate

  • iterate(): Iterable<T | Error>
  • Creates an iterable over either the successful value or the error.

    Returns Iterable<T | Error>

    A stream over either the successful value or the error.

orElse

  • orElse(backup: T): T
  • Returns the successful value or the backup value in case this try represents an error.

    TryFactory.error("bad").erElse(0) // => 0
    

    Parameters

    • backup: T

      The default value in case of an errror.

    Returns T

    The successful value; or the default value.

orFlatTry

  • orFlatTry(backup: TypedFunction<Error, ITry<T>>): ITry<T>
  • If not successful, attempts to compute a value from the error with the given backup handler. Similar to orTry, but the backup handler returns a ITry directly.

    parseIntSafe = string => {
      if (isInt(string)) return parseInt(string);
      throw new Error("invalid number format")
    }
    
    T.of(() => parseIntSafe(input))
      .orTryFlat(e => Try.of(() => parseIntSafe(e.statusCode)))
    
    // => input, status code, or error
    

    Parameters

    • backup: TypedFunction<Error, ITry<T>>

      Backup handler that computes a new value for the error.

    Returns ITry<T>

    A ITry with the current successful value, the new computed backup value, or an error in case the backup handler threw an error.

orThrow

  • orThrow(): T
  • On success, returns the value; otherwise throws the error.

    TryFactory.success(9).orThrow()
    // => 9
    
    TryFactory.error(new Error()).orThrow()
    // => throws
    

    Returns T

    The successful value.

orTry

  • orTry(backup: TypedFunction<Error, T>): ITry<T>
  • If not successful, attempts to compute a value from the error with the given backup handler.

    parseIntSafe = string => {
      if (isInt(string)) return parseInt(string);
      throw new Error("invalid number format")
    }
    
    T.of(() => parseIntSafe(input))
      .orTry(e => parseIntSafe(e.statusCode))
    
    // => input, status code, or error
    

    Parameters

    • backup: TypedFunction<Error, T>

      Backup handler that computes a new value for the error.

    Returns ITry<T>

    A ITry with the current successful value, the new computed backup value, or an error in case the backup handler threw an error.

stream

  • Creates an stream from either the successful value or the error.

    TryFactory.of(() => parseIntSafe("2.5")).stream().filter(x => x instanceof Error).first()
    // Error("invalid integer format")
    

    Parameters

    Returns IStream<T | Error>

    A stream of either the successful value or the error.

then

  • then<S>(success: TypedFunction<T, S | ITry<S>>, error?: TypedFunction<Error, S | ITry<S>>): ITry<S>
  • Similar to [[Try.convert]], but works like a thenable-object. The handlers are allowed to return either the new value directly, or a ITry with the new value.

    TryFactory.of(...).then(x => JSON.parse(x))
    
    TryFactory.of(...).then(x => Try.of(() => JSON.parse(x)))
    

    Type parameters

    • S

      Type of the new value.

    Parameters

    • success: TypedFunction<T, S | ITry<S>>

      Handler that maps the successful value to a new value.

    • Optional error: TypedFunction<Error, S | ITry<S>>

    Returns ITry<S>

    The new value, wrapped in a ITry for encapsulating errors.

toJSON

  • toJSON(): object
  • Hook for JSON.stringify. Returns a JSON representation of itself, containing its value and whether it represents a success or failure of an operation.

    Returns object

    An object with keys result (contained value) and success (whether the operation was successful).

    • result: T | string
    • success: boolean

unwrap

  • unwrap(): T | Error
  • Unwraps the try an returns either the contained successful value or the error.

    Returns T | Error

    Either the contained value or the error.

Generated using TypeDoc