Class: Err<E>
A type used to express computations that can fail, it can be used for returning and propagating errors. This is a
type union with the variants Ok(T)
, representing success and containing a value, and Err(E)
, representing error
and containing an error value.
Typeparam
T The result's type.
Typeparam
E The error's type.
See
https://doc.rust-lang.org/std/result/index.html
Type parameters
Name |
---|
E |
Implements
IResult
<any
,E
>
Constructors
constructor
• new Err<E
>(error
): Err
<E
>
Type parameters
Name |
---|
E |
Parameters
Name | Type |
---|---|
error | E |
Returns
Err
<E
>
Defined in
node_modules/@sapphire/result/dist/esm/index.d.mts:1706
Properties
error
• Private
Readonly
error: any
Defined in
node_modules/@sapphire/result/dist/esm/index.d.mts:1705
Methods
[iterator]
▸ [iterator](): Generator
<never
, any
, unknown
>
Returns an iterator over the possibly contained value.
The iterator yields one value if the result is Ok
, otherwise none.
Returns
Generator
<never
, any
, unknown
>
Example
const x = ok(7);
for (const value of x) {
console.log(value);
}
// Logs 7
Example
const x = err('Nothing!');
for (const value of x) {
console.log(value);
}
// Doesn't log
See
Implementation of
Defined in
node_modules/@sapphire/result/dist/esm/index.d.mts:1749
and
▸ and(result?
): this
Returns result
if the result is Ok
, otherwise returns the Err
value of itself.
Parameters
Name | Type | Description |
---|---|---|
result? | Result <any , E > | The result to check. |
Returns
this
Example
const x: Result<number, string> = ok(2);
const y: Result<string, string> = err('Late error');
assert.equal(x.and(y), err('Late error'));
Example
const x: Result<number, string> = err('Early error');
const y: Result<string, string> = err('Late error');
assert.equal(x.and(y), err('Early error'));
Example
const x: Result<number, string> = ok(2);
const y: Result<string, string> = ok('Hello');
assert.equal(x.and(y), ok('Hello'));
See
https://doc.rust-lang.org/std/result/enum.Result.html#method.and
Implementation of
Defined in
node_modules/@sapphire/result/dist/esm/index.d.mts:1731
andThen
▸ andThen(cb?
): this
Calls cb
if the result is Ok
, otherwise returns the Err
value of self.
This function can be used for control flow based on Result
values.
Parameters
Name | Type | Description |
---|---|---|
cb? | (value : never ) => Result <any , E > | The predicate. |
Returns
this
Example
function fractionOf4(value: number) {
return value === 0 ? err('overflowed') : ok(4 / value);
}
assert.equal(ok(2).andThen(fractionOf4), ok(4));
assert.equal(ok(0).andThen(fractionOf4), err('overflowed'));
assert.equal(err('not a number').andThen(fractionOf4), err('not a number'));
See
https://doc.rust-lang.org/std/result/enum.Result.html#method.and_then
Implementation of
Defined in
node_modules/@sapphire/result/dist/esm/index.d.mts:1732
contains
▸ contains(value?
): false
Returns true
if the result is an Ok
and the given value strict equals it.
Parameters
Name | Type | Description |
---|---|---|
value? | any | The value to compare. |
Returns
false
Example
const x: Result<number, string> = ok(2);
assert.equal(x.contains(2), true);
Example
const x: Result<number, string> = ok(3);
assert.equal(x.contains(2), false);
Example
const x: Result<number, string> = err('Some error message');
assert.equal(x.contains(2), false);
See
https://doc.rust-lang.org/std/result/enum.Result.html#method.contains
Implementation of
Defined in
node_modules/@sapphire/result/dist/esm/index.d.mts:1735
containsErr
▸ containsErr(error
): boolean
Returns true
if the result is an Err
and the given error strict equals it.
Parameters
Name | Type | Description |
---|---|---|
error | E | The error to compare. |
Returns
boolean
Example
const x: Result<number, string> = ok(2);
assert.equal(x.containsErr('Some error message'), false);
Example
const x: Result<number, string> = err('Some error message');
assert.equal(x.containsErr('Some error message'), true);
Example
const x: Result<number, string> = err('Some other error message');
assert.equal(x.containsErr('Some error message'), false);
See
https://doc.rust-lang.org/std/result/enum.Result.html#method.contains_err
Implementation of
Defined in
node_modules/@sapphire/result/dist/esm/index.d.mts:1736
eq
▸ eq(other
): false
Checks whether or not other
equals with self.
Parameters
Name | Type | Description |
---|---|---|
other | Ok <any > | The other result to compare. |
Returns
false
See
https://doc.rust-lang.org/std/cmp/trait.PartialEq.html#tymethod.eq
Implementation of
Defined in
node_modules/@sapphire/result/dist/esm/index.d.mts:1741
▸ eq(other
): boolean
Parameters
Name | Type |
---|---|
other | Result <any , E > |
Returns
boolean
Implementation of
IResult.eq
Defined in
node_modules/@sapphire/result/dist/esm/index.d.mts:1742
err
▸ err(): Some
<E
>
Converts from Result<T, E>
to Option<E>
.
Converts itself into an Option<E>
, and discarding the successful value, if any.
Returns
Some
<E
>
Example
const x: Result<number, string> = ok(2);
assert.equal(x.err(), none);
Example
const x: Result<number, string> = err('Some error message');
assert.equal(x.err(), 'Some error message');
See
https://doc.rust-lang.org/std/result/enum.Result.html#method.err
Implementation of
Defined in
node_modules/@sapphire/result/dist/esm/index.d.mts:1712
expect
▸ expect(message
): never
Returns the contained Ok
value.
If the value is an Err
, it throws a ResultError with the given message and the content of the Err
.
Parameters
Name | Type | Description |
---|---|---|
message | string | The message for the error. |
Returns
never
Example
const x = ok(2);
assert.equal(x.expect('Whoops!'), 2);
Example
const x = err('Emergency failure');
assert.throws(() => x.expect('Whoops!'), {
name: 'ResultError',
message: 'Whoops',
value: 'Emergency failure'
});
See
https://doc.rust-lang.org/std/result/enum.Result.html#method.expect
Implementation of
Defined in
node_modules/@sapphire/result/dist/esm/index.d.mts:1724
expectErr
▸ expectErr(message?
): E
Returns the contained Err
value.
If the value is an Ok
, it throws a ResultError with the given message and the content of the Ok
.
Parameters
Name | Type | Description |
---|---|---|
message? | string | The message for the error. |
Returns
E
Example
const x = ok(2);
assert.throws(() => x.expectErr('Whoops!'), {
name: 'ResultError',
message: 'Whoops',
value: 2
});
Example
const x = err('Emergency failure');
assert.equal(x.expectErr('Whoops!'), 'Emergency failure');
See
https://doc.rust-lang.org/std/result/enum.Result.html#method.expect_err
Implementation of
Defined in
node_modules/@sapphire/result/dist/esm/index.d.mts:1725
flatten
▸ flatten(): this
Converts from Result<Result<T, E>, E>
to Result<T, E>
.
Returns
this
Example
const x: Result<Result<string, number>, number> = ok(ok('Hello'));
assert.equal(x.flatten(), ok('Hello'));
Example
const x: Result<Result<string, number>, number> = ok(err(6));
assert.equal(x.flatten(), err(6));
Example
const x: Result<Result<string, number>, number> = err(6);
assert.equal(x.flatten(), err(6));
See
https://doc.rust-lang.org/std/result/enum.Result.html#method.flatten
Implementation of
Defined in
node_modules/@sapphire/result/dist/esm/index.d.mts:1738
inspect
▸ inspect(cb?
): this
Calls the provided closure with a reference to the contained value (if Ok
).
Parameters
Name | Type | Description |
---|---|---|
cb? | (value : never ) => void | The predicate. |
Returns
this
Seealso
inspectAsync for the awaitable version.
Example
ok(2).inspect(console.log);
// Logs: 2
Example
err('Some error message').inspect(console.log);
// Doesn't log
See
https://doc.rust-lang.org/std/result/enum.Result.html#method.inspect
Implementation of
Defined in
node_modules/@sapphire/result/dist/esm/index.d.mts:1719
inspectAsync
▸ inspectAsync(cb?
): Promise
<Err
<E
>>
Calls the provided closure with a reference to the contained value (if Ok
) and awaits it.
Parameters
Name | Type | Description |
---|---|---|
cb? | (value : never ) => unknown | The predicate. |
Returns
Seealso
inspect for the sync version.
Example
await ok(2).inspectAsync(console.log);
// Logs: 2
Example
await err('Some error message').inspectAsync(console.log);
// Doesn't log
Note
This is an extension not supported in Rust
Implementation of
Defined in
node_modules/@sapphire/result/dist/esm/index.d.mts:1720
inspectErr
▸ inspectErr(cb
): this
Calls the provided closure with a reference to the contained error (if Err
).
Parameters
Name | Type | Description |
---|---|---|
cb | (error : E ) => void | The predicate. |
Returns
this
Seealso
inspectErrAsync for the awaitable version.
Example
ok(2).inspectErr(console.log);
// Doesn't log
Example
err('Some error message').inspectErr(console.log);
// Logs: Some error message
See
https://doc.rust-lang.org/std/result/enum.Result.html#method.inspect_err
Implementation of
Defined in
node_modules/@sapphire/result/dist/esm/index.d.mts:1721
inspectErrAsync
▸ inspectErrAsync(cb
): Promise
<Err
<E
>>
Calls the provided closure with a reference to the contained error (if Err
) and awaits it.
Parameters
Name | Type | Description |
---|---|---|
cb | (error : E ) => unknown | The predicate. |
Returns
Seealso
inspectErr for the sync version.
Example
await ok(2).inspectErrAsync(console.log);
// Doesn't log
Example
await err('Some error message').inspectErrAsync(console.log);
// Logs: Some error message
Note
This is an extension not supported in Rust
Implementation of
Defined in
node_modules/@sapphire/result/dist/esm/index.d.mts:1722
intoOkOrErr
▸ intoOkOrErr(): E
Returns the Ok
value if self is Ok
, and the Err
value if self is Err
.
Returns
E
Example
let x: Result<number, number> = ok(3);
assert.equal(x.intoOkOrErr(), 3);
Example
let x: Result<number, number> = err(4);
assert.equal(x.intoOkOrErr(), 4);
See
https://doc.rust-lang.org/std/result/enum.Result.html#method.into_ok_or_err
Implementation of
Defined in
node_modules/@sapphire/result/dist/esm/index.d.mts:1739
intoPromise
▸ intoPromise(): Promise
<Err
<Awaited
<E
>>>
Returns a Promise
object with the awaited value (if Ok
) or the awaited error (if Err
).
Returns
Example
let x = ok(Promise.resolve(3));
assert.equal(await x.intoPromise(), ok(3));
Note
This is an extension not supported in Rust
Implementation of
Defined in
node_modules/@sapphire/result/dist/esm/index.d.mts:1740
isErr
▸ isErr(): this is Err<E>
Returns true
if the result is Err
.
Returns
this is Err<E>
Example
const x = ok(-3);
assert.equal(x.isErr(), false);
Example
const x = err('Some error message');
assert.equal(x.isErr(), true);
See
https://doc.rust-lang.org/std/result/enum.Result.html#method.is_err
Implementation of
Defined in
node_modules/@sapphire/result/dist/esm/index.d.mts:1709
isErrAnd
▸ isErrAnd(cb
): boolean
Returns true
if the result is Err
and the value inside of it matches a predicate.
Parameters
Name | Type | Description |
---|---|---|
cb | (error : E ) => boolean | The predicate. |
Returns
boolean
Example
const x = ok(2);
assert.equal(x.isErrAnd((error) => error instanceof TypeError), false);
Example
const x = err(new Error('Some error message'));
assert.equal(x.isErrAnd((error) => error instanceof TypeError), false);
Example
const x = err(new TypeError('Some error message'));
assert.equal(x.isErrAnd((error) => error instanceof TypeError), true);
See
https://doc.rust-lang.org/std/result/enum.Result.html#method.is_err_and
Implementation of
Defined in
node_modules/@sapphire/result/dist/esm/index.d.mts:1710
isOk
▸ isOk(): false
Returns true
if the result is Ok
.
Returns
false
Example
const x = ok(-3);
assert.equal(x.isOk(), true);
Example
const x = err('Some error message');
assert.equal(x.isOk(), false);
See
https://doc.rust-lang.org/std/result/enum.Result.html#method.is_ok
Implementation of
Defined in
node_modules/@sapphire/result/dist/esm/index.d.mts:1707
isOkAnd
▸ isOkAnd(cb?
): false
Returns true
if the result is Ok
and the value inside of it matches a predicate.
Parameters
Name | Type |
---|---|
cb? | (value : never ) => boolean |
Returns
false
Example
const x = ok(2);
assert.equal(x.isOkAnd((value) => value > 1), true);
Example
const x = ok(0);
assert.equal(x.isOkAnd((value) => value > 1), false);
Example
const x = err('Some error message');
assert.equal(x.isOkAnd((value) => value > 1), false);
See
https://doc.rust-lang.org/std/result/enum.Result.html#method.is_ok_and
Implementation of
Defined in
node_modules/@sapphire/result/dist/esm/index.d.mts:1708