#Function: unwrapError()
Extracts the error value from a Result or ResultAsync.
If the input is a Success, it will throw the success value or return the default value if provided.
unwrapError(result, defaultValue?)Select a case to follow its path#Type Param
R
The input Result or ResultAsync.
#Type Param
T
The default value type (optional).
#Examples
Failure Case (without default)
import { import Result Result } from '@praha/byethrow';
const const result: Result.Result<number, string> result : import Result Result .type Result<T, E> = Result.Success<T> | Result.Failure<E>A union type representing either a success or a failure.
@typeParamT - The type of the Success value.@typeParamE - The type of the Failure value.@exampleimport { Result } from '@praha/byethrow';
const doSomething = (): Result.Result<number, string> => {
return Math.random() > 0.5
? { type: 'Success', value: 10 }
: { type: 'Failure', error: 'Oops' };
};
@categoryCore Types Result <number, string> = import Result Result .function fail<"Oops">(error: "Oops"): Result.Result<never, "Oops"> (+1 overload)Creates a
Failure
result from a given error.
Passing a Promise is allowed, but it is recommended to await it before passing to fail.
@typeParamE - The type of the error to wrap.@returnsA Result containing the given error.@exampleSynchronous Usage
import { Result } from '@praha/byethrow';
const result = Result.fail('Something went wrong');
// Result.Result<never, string>
@exampleWith No Value
import { Result } from '@praha/byethrow';
const result = Result.fail();
// Result.Result<never, void>
@seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators fail ('Oops');
const const error: string error = import Result Result .function unwrapError<Result.Result<number, string>>(result: Result.Result<number, string>): string (+3 overloads)Extracts the error value from a
Result
or
ResultAsync
.
If the input is a
Success
, it will throw the success value or return the default value if provided.
@typeParamR - The input Result or ResultAsync .@typeParamT - The default value type (optional).@exampleFailure Case (without default)
import { Result } from '@praha/byethrow';
const result: Result.Result<number, string> = Result.fail('Oops');
const error = Result.unwrapError(result); // 'Oops'
@exampleFailure Case (with default)
import { Result } from '@praha/byethrow';
const result: Result.Result<number, string> = Result.fail('Oops');
const error = Result.unwrapError(result, 'default'); // 'Oops'
@exampleSuccess Case (without default)
import { Result } from '@praha/byethrow';
const result: Result.Result<number, string> = Result.succeed(100);
const error = Result.unwrapError(result); // throws 100
@exampleSuccess Case (with default)
import { Result } from '@praha/byethrow';
const result: Result.Result<number, string> = Result.succeed(100);
const error = Result.unwrapError(result, 0); // 0
@seeassertFailure - When used with assertFailure , you can safely unwrap the Result .@categoryUnwraps unwrapError (const result: Result.Result<number, string> result ); // 'Oops'Failure Case (with default)
import { import Result Result } from '@praha/byethrow';
const const result: Result.Result<number, string> result : import Result Result .type Result<T, E> = Result.Success<T> | Result.Failure<E>A union type representing either a success or a failure.
@typeParamT - The type of the Success value.@typeParamE - The type of the Failure value.@exampleimport { Result } from '@praha/byethrow';
const doSomething = (): Result.Result<number, string> => {
return Math.random() > 0.5
? { type: 'Success', value: 10 }
: { type: 'Failure', error: 'Oops' };
};
@categoryCore Types Result <number, string> = import Result Result .function fail<"Oops">(error: "Oops"): Result.Result<never, "Oops"> (+1 overload)Creates a
Failure
result from a given error.
Passing a Promise is allowed, but it is recommended to await it before passing to fail.
@typeParamE - The type of the error to wrap.@returnsA Result containing the given error.@exampleSynchronous Usage
import { Result } from '@praha/byethrow';
const result = Result.fail('Something went wrong');
// Result.Result<never, string>
@exampleWith No Value
import { Result } from '@praha/byethrow';
const result = Result.fail();
// Result.Result<never, void>
@seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators fail ('Oops');
const const error: string error = import Result Result .function unwrapError<Result.Result<number, string>, "default">(result: Result.Result<number, string>, defaultValue: "default"): string (+3 overloads)Extracts the error value from a
Result
or
ResultAsync
.
If the input is a
Success
, it will throw the success value or return the default value if provided.
@typeParamR - The input Result or ResultAsync .@typeParamT - The default value type (optional).@exampleFailure Case (without default)
import { Result } from '@praha/byethrow';
const result: Result.Result<number, string> = Result.fail('Oops');
const error = Result.unwrapError(result); // 'Oops'
@exampleFailure Case (with default)
import { Result } from '@praha/byethrow';
const result: Result.Result<number, string> = Result.fail('Oops');
const error = Result.unwrapError(result, 'default'); // 'Oops'
@exampleSuccess Case (without default)
import { Result } from '@praha/byethrow';
const result: Result.Result<number, string> = Result.succeed(100);
const error = Result.unwrapError(result); // throws 100
@exampleSuccess Case (with default)
import { Result } from '@praha/byethrow';
const result: Result.Result<number, string> = Result.succeed(100);
const error = Result.unwrapError(result, 0); // 0
@seeassertFailure - When used with assertFailure , you can safely unwrap the Result .@categoryUnwraps unwrapError (const result: Result.Result<number, string> result , 'default'); // 'Oops'Success Case (without default)
import { import Result Result } from '@praha/byethrow';
const const result: Result.Result<number, string> result : import Result Result .type Result<T, E> = Result.Success<T> | Result.Failure<E>A union type representing either a success or a failure.
@typeParamT - The type of the Success value.@typeParamE - The type of the Failure value.@exampleimport { Result } from '@praha/byethrow';
const doSomething = (): Result.Result<number, string> => {
return Math.random() > 0.5
? { type: 'Success', value: 10 }
: { type: 'Failure', error: 'Oops' };
};
@categoryCore Types Result <number, string> = import Result Result .function succeed<100>(value: 100): Result.Result<100, never> (+1 overload)Creates a
Success
result from a given value.
Passing a Promise is allowed, but it is recommended to await it before passing to succeed.
@typeParamT - The type of the value to wrap.@returnsA Result containing the given value.@exampleSynchronous Usage
import { Result } from '@praha/byethrow';
const result = Result.succeed(42);
// Result.Result<42, never>
@exampleWith No Value
import { Result } from '@praha/byethrow';
const result = Result.succeed();
// Result.Result<void, never>
@seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators succeed (100);
const const error: string error = import Result Result .function unwrapError<Result.Result<number, string>>(result: Result.Result<number, string>): string (+3 overloads)Extracts the error value from a
Result
or
ResultAsync
.
If the input is a
Success
, it will throw the success value or return the default value if provided.
@typeParamR - The input Result or ResultAsync .@typeParamT - The default value type (optional).@exampleFailure Case (without default)
import { Result } from '@praha/byethrow';
const result: Result.Result<number, string> = Result.fail('Oops');
const error = Result.unwrapError(result); // 'Oops'
@exampleFailure Case (with default)
import { Result } from '@praha/byethrow';
const result: Result.Result<number, string> = Result.fail('Oops');
const error = Result.unwrapError(result, 'default'); // 'Oops'
@exampleSuccess Case (without default)
import { Result } from '@praha/byethrow';
const result: Result.Result<number, string> = Result.succeed(100);
const error = Result.unwrapError(result); // throws 100
@exampleSuccess Case (with default)
import { Result } from '@praha/byethrow';
const result: Result.Result<number, string> = Result.succeed(100);
const error = Result.unwrapError(result, 0); // 0
@seeassertFailure - When used with assertFailure , you can safely unwrap the Result .@categoryUnwraps unwrapError (const result: Result.Result<number, string> result ); // throws 100Success Case (with default)
import { import Result Result } from '@praha/byethrow';
const const result: Result.Result<number, string> result : import Result Result .type Result<T, E> = Result.Success<T> | Result.Failure<E>A union type representing either a success or a failure.
@typeParamT - The type of the Success value.@typeParamE - The type of the Failure value.@exampleimport { Result } from '@praha/byethrow';
const doSomething = (): Result.Result<number, string> => {
return Math.random() > 0.5
? { type: 'Success', value: 10 }
: { type: 'Failure', error: 'Oops' };
};
@categoryCore Types Result <number, string> = import Result Result .function succeed<100>(value: 100): Result.Result<100, never> (+1 overload)Creates a
Success
result from a given value.
Passing a Promise is allowed, but it is recommended to await it before passing to succeed.
@typeParamT - The type of the value to wrap.@returnsA Result containing the given value.@exampleSynchronous Usage
import { Result } from '@praha/byethrow';
const result = Result.succeed(42);
// Result.Result<42, never>
@exampleWith No Value
import { Result } from '@praha/byethrow';
const result = Result.succeed();
// Result.Result<void, never>
@seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators succeed (100);
const const error: string | 0 error = import Result Result .function unwrapError<Result.Result<number, string>, 0>(result: Result.Result<number, string>, defaultValue: 0): string | 0 (+3 overloads)Extracts the error value from a
Result
or
ResultAsync
.
If the input is a
Success
, it will throw the success value or return the default value if provided.
@typeParamR - The input Result or ResultAsync .@typeParamT - The default value type (optional).@exampleFailure Case (without default)
import { Result } from '@praha/byethrow';
const result: Result.Result<number, string> = Result.fail('Oops');
const error = Result.unwrapError(result); // 'Oops'
@exampleFailure Case (with default)
import { Result } from '@praha/byethrow';
const result: Result.Result<number, string> = Result.fail('Oops');
const error = Result.unwrapError(result, 'default'); // 'Oops'
@exampleSuccess Case (without default)
import { Result } from '@praha/byethrow';
const result: Result.Result<number, string> = Result.succeed(100);
const error = Result.unwrapError(result); // throws 100
@exampleSuccess Case (with default)
import { Result } from '@praha/byethrow';
const result: Result.Result<number, string> = Result.succeed(100);
const error = Result.unwrapError(result, 0); // 0
@seeassertFailure - When used with assertFailure , you can safely unwrap the Result .@categoryUnwraps unwrapError (const result: Result.Result<number, string> result , 0); // 0#See
assertFailure - When used with assertFailure, you can safely unwrap the Result.
#Call Signature
unwrapError<
R>(result):trueextendsHasPromise<R> ?Promise<InferFailure<R>> :InferFailure<R>
#Type Parameters
#R
R extends ResultMaybeAsync<any, any>
#Parameters
#result
R
#Returns
true extends HasPromise<R> ? Promise<InferFailure<R>> : InferFailure<R>
#Call Signature
unwrapError<
R,T>(result,defaultValue):trueextendsHasPromise<R> ?Promise<T|InferFailure<R>> :T|InferFailure<R>
#Type Parameters
#R
R extends ResultMaybeAsync<any, any>
#T
T
#Parameters
#result
R
#defaultValue
T
#Returns
true extends HasPromise<R> ? Promise<T | InferFailure<R>> : T | InferFailure<R>
#Call Signature
unwrapError<
R>(): (result) =>trueextendsHasPromise<R> ?Promise<InferFailure<R>> :InferFailure<R>
#Type Parameters
#R
R extends ResultMaybeAsync<any, any>
#Returns
(result) => true extends HasPromise<R> ? Promise<InferFailure<R>> : InferFailure<R>
#Call Signature
unwrapError<
R,T>(defaultValue): (result) =>trueextendsHasPromise<R> ?Promise<T|InferFailure<R>> :T|InferFailure<R>
#Type Parameters
#R
R extends ResultMaybeAsync<any, any>
#T
T
#Parameters
#defaultValue
T
#Returns
(result) => true extends HasPromise<R> ? Promise<T | InferFailure<R>> : T | InferFailure<R>
