The type of a successful value.
Used for performing side effects on failures: the given function will be invoked if and only if this Try is a failure, in which case it will be invoked with the encapsualted error.
Note that any errors thrown by the consumer will not be handled.
A function that will consume the error, if there is one.
returns the same Try.
Turns a Failure into a Success and vice-versa. A Failure is turned into a Success encapsulating the error as it's value. A Success is turned into a new Failure.
You can kind of think of this as an assertion that the try is a failure: so if it is, the assertion passes, so it results in a Success. If it's not a Failure, then the assertion fails, so it results in a Failure.
If the Try is a success and its value passes the given predicate, the Try is returned. If it does not pass the predicate, or if the predicate throws, a Failure is returned. If the Try is already a Failure, it is returned.
The predicate function to test this against.
A Try acts like a collection of 0 or 1 values, and this applies the given consumer to each item in the collection. This is used for performing side effects.
Note that any errors thrown by the consumer will not be handled.
A function that will consume the value, if there is one.
returns the same Try.
Gets the encapsulated value if it's successful, otherwise throws the encapsulated error.
the encapsulated value
Get the encpauslated value from a Success, or else return the given default value for a Failure.
The default value to return if this is a Failure.
Maps the encapsulated value of a success through the given mapper and returns a success encapsulating the result. If the mapper throws an error, it's encapsulated in a failure. If this try is already a failure, a new failure (of the appropriate type) encapsulating the same error is returned.
Somewhat like getOrElse
, but this doesn't do the get
portion of it, meaning it
doesn't give you the encapsulated value, it gives you a Try. If this is a success, returns
itself. If this is a failure, returns the given value, as is.
The encapsulated value (or failure) to use if this is a failure.
Returns a permissive Try which encapsulates both successes and failures as successes. For successes, returns a Try with the same encapsulated value. For failures, returns a success whose encapsulated value is the encapsulated error.
Recovers a Failure Try by mapping the encapsulated error into a valid value with the given mapper. If the given mapper throws an error, it's returned wrapped inside a new Failure. If this Try is a Success, it is returned unchanged.
Function that turns an error into a value, or throws an error if the given error is not recoverable.
Possibly recovers a Failure Try by mapping the encapsulated error into a Try. This is similar to recover
,
but the error mapper's returned value is assumed to already be a Try, which is returned. If the mapper
throws an error, it's returned in a new Failure. If this Try is already a Success, it is returned as is.
Function that turns an error into a Try, with a failure for unreocverable errors (or errors that occurreed attemptig to recover), or with a success encapsulating a recovered value/
Similar to transform
, except that any error thrown by the selected mapper function is captured and returned as
a Failure.
The function applied to the encapsulated value of a success, to get the new Try.
The function applied to the encapsulated error of a failure, to get the new Try.
Used to perform side effects on success or failure.
Note that any errors thrown by the selected consumer will not be handled.
The function that will be called to consume the encapsulated value if this Try is a success.
The function that will be called to consume the encapsulated error if this Try is a failure.
return this same Try.
Returns a new array containing the encapsulated value for a Success, or no values (an empty array) for a Failure.
Converts this Try to an Observable stream that works the same as a supressed observable stream returned
by toSuppressingObservable
, except the stream never completes (for either the failure or success case).
Converts this to a Maybe using the provided factory. A success is converted to a Maybe of the encapsulated value using
the provided Just
function. A failure returns the Nothing
value.
An object that provides the
Just
factory function for creating a Maybe, and the Nothing
singleton Maybe instance.
Returns the encapsulated value of a success, or null
for a failure.
Note that the encapsulated value of a success may be a null
.
Converts this Try to an Observable stream: A success returns an Observable that emits the encapsulated value and then completes, a failure turns an Observable that err's.
a factory function that is called to create
the returned observable by providing it with the "subscribe" function. Note that this is not called with new
,
so if your Observable
is a constructor, you'll need to encapsulate it in a factory function.
Converts this to an Option using the provided factory object. A success is converted to an Option of the encapsulated value, a failure is converted to a None.
An object that provides the
Some
and None
factory function for creating an Option.
Converts this to an Optional, as long as you can provide it with an appropriate factory. A success is returned as an Optional of the encapsulated value, a failure is returned as an empty.
an object that provides the
of
and empty
factory functions for creating an Optional (denoted by type parameter O
).
Converts to this a settled promise. A success is converted to a promise that fulfills with the encapsulated value, a failure is converted to a promise that rejects with the encapsulated error.
Converts this Try to an Observable stream that supresses the encapsulated error of a failure. Same was
toObservable
, but the failure case just completes immediately.
Transforms this Try into another Try by transforming the encapsulated value of a success, or the encapsulated error of a failure through the given functions.
Note: if the applied function throws an error, it is not captured, it is thrown. If you want to capture
it in a Failure, use safeTransform
instead.
The function applied to the encapsulated value of a success, to get the new Try.
The function applied to the encapsulated error of a failure, to get the new Try.
Unpacks the Try into a value by applying one function for successes, and one for failures. Similar to transform
except the mappers aren't assumed to return a Try.
Not recommended, you should generally prefer using Try.apply
instead
of instantiating a new Success or Failure directly.
Not recommended, you should generally prefer using Try.apply
instead
of instantiating a new Success or Failure directly.
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.
The function to invoke.
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.
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.
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.
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.
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.
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.
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.
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.
A Maybe object
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.
An Option object.
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.
An Optional object.
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.
The promise to convert to a Try.
Generated using TypeDoc
A Try represents the results of an operation that might have failed, enapsulating either the successful result value, or the error that occurred.