Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Failure<T>

An implementation of Try for representing failures.

Type parameters

  • T

Hierarchy

  • Try<T>
    • Failure

Implements

Index

Constructors

constructor

  • new Failure(error: Error): Failure

Properties

Private error

error: Error

Methods

catch

  • catch(consumer: (e: Error) => void): Failure<T>
  • Parameters

    • consumer: (e: Error) => void
        • (e: Error): void
        • Parameters

          • e: Error

          Returns void

    Returns Failure<T>

failed

filter

  • filter(predicate: (val: T) => boolean): Try<T>
  • Parameters

    • predicate: (val: T) => boolean
        • (val: T): boolean
        • Parameters

          • val: T

          Returns boolean

    Returns Try<T>

flatMap

  • flatMap<U>(mapper: (t: T) => Try<U>): Try<U>

forEach

  • forEach(consumer: (val: T) => void): Failure<T>
  • Parameters

    • consumer: (val: T) => void
        • (val: T): void
        • Parameters

          • val: T

          Returns void

    Returns Failure<T>

get

  • get(): never

getOrElse

  • getOrElse(defaultValue: T): T

isFailure

  • isFailure(): boolean

isSuccess

  • isSuccess(): boolean

map

  • map<U>(mapper: (t: T) => U): Try<U>
  • Type parameters

    • U

    Parameters

    • mapper: (t: T) => U
        • (t: T): U
        • Parameters

          • t: T

          Returns U

    Returns Try<U>

orElse

  • orElse<D>(defaultTryValue: D): D

permissive

  • permissive(): Try<T | Error>

recover

  • recover(errorMapper: (e: Error) => T): Try<T>
  • Parameters

    • errorMapper: (e: Error) => T
        • (e: Error): T
        • Parameters

          • e: Error

          Returns T

    Returns Try<T>

recoverWith

  • recoverWith(recoverer: (e: Error) => Try<T>): Try<T>

safeTransform

  • safeTransform<U>(mapSuccess: (val: T) => Try<U>, mapFailure: (e: Error) => Try<U>): Try<U>
  • Type parameters

    • U

    Parameters

    • mapSuccess: (val: T) => Try<U>
        • (val: T): Try<U>
        • Parameters

          • val: T

          Returns Try<U>

    • mapFailure: (e: Error) => Try<U>
        • (e: Error): Try<U>
        • Parameters

          • e: Error

          Returns Try<U>

    Returns Try<U>

tap

  • tap(valueConsumer: (val: T) => void, errorConsumer: (e: Error) => void): Failure<T>
  • Parameters

    • valueConsumer: (val: T) => void
        • (val: T): void
        • Parameters

          • val: T

          Returns void

    • errorConsumer: (e: Error) => void
        • (e: Error): void
        • Parameters

          • e: Error

          Returns void

    Returns Failure<T>

toArray

  • toArray(): Array<T>

toHungObservable

toMaybe

toNullable

  • toNullable(): null

toObservable

toOption

toOptional

toPromise

  • toPromise(): Promise<T>

toSuppressingObservable

transform

  • transform<U>(mapSuccess: (val: T) => Try<U>, mapFailure: (e: Error) => Try<U>): Try<U>
  • Type parameters

    • U

    Parameters

    • mapSuccess: (val: T) => Try<U>
        • (val: T): Try<U>
        • Parameters

          • val: T

          Returns Try<U>

    • mapFailure: (e: Error) => Try<U>
        • (e: Error): Try<U>
        • Parameters

          • e: Error

          Returns Try<U>

    Returns Try<U>

transmute

  • transmute<U>(mapSuccess: (val: T) => U, mapFailure: (e: Error) => U): U
  • Type parameters

    • U

    Parameters

    • mapSuccess: (val: T) => U
        • (val: T): U
        • Parameters

          • val: T

          Returns U

    • mapFailure: (e: Error) => U
        • (e: Error): U
        • Parameters

          • e: Error

          Returns U

    Returns U

Static Failure

  • Failure<T>(error: Error): Try<T>
  • Not recommended, you should generally prefer using Try.apply instead of instantiating a new Success or Failure directly.

    Type parameters

    • T

    Parameters

    • error: Error

    Returns Try<T>

Static Success

  • Success<T>(value: T): Try<T>
  • Not recommended, you should generally prefer using Try.apply instead of instantiating a new Success or Failure directly.

    Type parameters

    • T

    Parameters

    • value: T

    Returns Try<T>

Static apply

  • apply<T>(provider: () => T): Try<T>
  • Execute the given function and encapsulate the result in a Try, whether successful or not. If the func returns a value, this is returned encapsulated in a Success. If the func throws an error, it is captured in a Failure and returned.

    Type parameters

    • T

    Parameters

    • provider: () => T

      The function to invoke.

        • (): T
        • Returns T

    Returns Try<T>

Static createTryOperator

  • Creates an operator for Observables in the style of rxjs. The operator will map an observable to one that emits tries: values will be mapped to successes encapsulating those values, and an error will be mapped to a failure encapsulating that error. The generated observable will terminate when the source observable terminate or errors.

    Type parameters

    Parameters

    • Observable: ObservableFactory<O2, Try<T>>

      The factory function for creating a new Observable from a subscribe function. Note that this is not called with new, so if your function is a costructor, you'll need to encapsulate that in another function. For instance, for rxjs, this could be (subscribe) => new rxjs.Observable(subscribe). The subscribe function that will be passed to Observable is expected to be called with an Observer object that has next, error, and complete methods.

    Returns (source: O) => O2

    The operator function which will take a source Observable and return a derived Observable that emits Tries as described above. The source Observable passed to the returned function is expected to have a subscribe method which takes three arguments: onNext, onError, and onComplete.

      • (source: O): O2
      • Parameters

        • source: O

        Returns O2

Static createUnTryOperator

  • createUnTryOperator<O, O2, T>(Observable: ObservableFactory<O, T>): (source: O2) => O
  • Similar to createTryOperator, this returns an Observable operator that unpacks Try values emitted by an observable. Encapsulated values of successes are emitted as values, and a failure emitted by the source stream is unpacked and its encapsulated error is put out as an error.

    Type parameters

    Parameters

    • Observable: ObservableFactory<O, T>

      The factory function for creating a new Observable from a subscribe function. Now that this is not called with new, so if your function is costructor, you'll need to encapsulate that in a function. For instance, for rxjs, this could be (subscribe) => new rxjs.Observable(subscribe). The subscribe function that will be passed to Observable is expected to be called with an Observer object that has next, error, and complete methods.

    Returns (source: O2) => O

    The operator function which will take a source Observable and return a derived Observable that emits Tries as described above. The source Observable passed to the returned function is expected to have a subscribe method which takes three arguments: onNext, onError, and onComplete.

      • (source: O2): O
      • Parameters

        • source: O2

        Returns O

Static flatApply

  • flatApply<T>(trySupplier: () => Try<T>): Try<T>
  • Similar to apply, this executes a function and captures any exceptions thrown into a Failure. The difference from apply is that the given function is assumed to already return a Try, which is not wrapped in another Try, but returned as is.

    Type parameters

    • T

    Parameters

    • trySupplier: () => Try<T>

    Returns Try<T>

Static from

Static fromMaybe

  • Converts a Maybe to a Try. Assumes the Maybe implements the map and getOrElse methods; the former returns another Maybe with the encapsulated value (if any) transformed according to the given function; the latter returns the encapsulated value or invokes the provided supplier if the Maybe has no encapsulated value and returns the result.

    Type parameters

    • T

    Parameters

    • maybe: Maybe<T>

      A Maybe object

    Returns Try<T>

Static fromOption

  • Convert a scala-like Option object to a Try. If the option is defined (as defined by it's isDefined method returning a truthy value), it's value (as returned by it's get method) is returned encapsulated in a success. Otherwise a Failure is returned.

    Note that error thrown attempting to invoke the method of the option are not handled, they will be thrown. The get method is only invoked if isDefined returns a truthy value.

    see

    Try.fromOptional

    Type parameters

    • T

    Parameters

    • option: Option<T>

      An Option object.

    Returns Try<T>

Static fromOptional

  • Convert a java-like Optional object to a Try. If the optional is present (as defined by it's isPresent method returning a truthy value), it's value (as returned by it's get method) is returned encapsulated in a success. Otherwise a Failure is returned.

    Note that error thrown attempting to invoke the method of the option are not handled, they will be thrown. The get method is only invoked if isPresent returns a truthy value.

    see

    Try.fromOption

    Type parameters

    • T

    Parameters

    • optional: Optional<T>

      An Optional object.

    Returns Try<T>

Static fromPromise

  • fromPromise<T>(p: Promise<T>): Promise<Try<T>>
  • Given a Promise for a value, returns a Promise for a Try of that value. The returned promise will always fulfill: if the given promise fulfills, the returned promise will fulfill with a success encapsulating the fulfillment value; if the given promise rejects, the returned promise will fulfill with a failure Try encapsulating the rejection error.

    Type parameters

    • T

    Parameters

    • p: Promise<T>

      The promise to convert to a Try.

    Returns Promise<Try<T>>

Generated using TypeDoc