Skip to content

Commit

Permalink
add FailAsProcessed
Browse files Browse the repository at this point in the history
  • Loading branch information
vladbasin committed Jul 2, 2024
1 parent 98caaa3 commit 3c873f1
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 10 deletions.
30 changes: 20 additions & 10 deletions src/Result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ export class Result<T> {
}

/**
* Processes error
* Processes error. Ensures that subsequent errors are not overridden
* @param factory Function which accepts error and transforms into a new one
* @returns New failed Result object, which contains processed error
*/
Expand All @@ -338,7 +338,7 @@ export class Result<T> {
}

/**
* Processes error
* Processes error. Ensures that subsequent errors are not overridden
* @param factory Function which accepts error and transforms into a new one
* @returns New failed Result object, which contains processed error
*/
Expand Down Expand Up @@ -387,7 +387,6 @@ export class Result<T> {

/**
* Creates Result with optional context
* @param context Context to execute actions in
* @returns New Promise with true value
*/
static Start(): Result<boolean> {
Expand All @@ -397,7 +396,6 @@ export class Result<T> {
/**
* Creates Result with value and optional context
* @param value Value to store in Result
* @param context Context to execute actions in
* @returns New Promise with stored value
*/
static Ok<T>(value: T): Result<T> {
Expand All @@ -424,7 +422,6 @@ export class Result<T> {
/**
* Creates Result with failure
* @param error Error message of the failure
* @param context Context to execute actions in
* @returns New Result object with failure
*/
static Fail<T>(error: string): Result<T> {
Expand All @@ -434,17 +431,33 @@ export class Result<T> {
/**
* Creates Result with failure
* @param error Error of the failure
* @param context Context to execute actions in
* @returns New Result object with failure
*/
static FailWithError<T>(error: Error): Result<T> {
return new Result(Promise.reject(error));
}

/**
* Creates Result with failure. Ensures that processing methods don't override this error
* @param error Error of the failure
* @returns New Result object with failure
*/
static FailAsProcessed<T>(error: string): Result<T> {
return Result.FailAsProcessedWithError(new Error(error));
}

/**
* Creates Result with failure. Ensures that processing methods don't override this error
* @param error Error of the failure
* @returns New Result object with failure
*/
static FailAsProcessedWithError<T>(error: Error): Result<T> {
return new Result(Promise.reject(new ProcessedError(error.message, error)));
}

/**
* Creates Result from promise
* @param promise Promise to track
* @param context Context to execute actions in
* @returns New Result object from Promise
*/
static FromPromise<T>(promise: Promise<T>): Result<T> {
Expand Down Expand Up @@ -482,7 +495,6 @@ export class Result<T> {
* Creates Result
* @param isSuccess Indicates whether the Result is failure or success
* @param error Error in for new Result in case it is failure
* @param context Context to execute actions in
* @returns New Result with true or false value
*/
static Create(isSuccess: boolean, error: string): Result<boolean> {
Expand All @@ -493,7 +505,6 @@ export class Result<T> {
* Creates Result
* @param isSuccess Indicates whether the Result is failure or success
* @param error Error in for new Result in case it is failure
* @param context Context to execute actions in
* @returns New Result with true or false value
*/
static CreateWithError(isSuccess: boolean, error: Error): Result<boolean> {
Expand All @@ -505,7 +516,6 @@ export class Result<T> {
* @param times Number of attempts
* @param delay Delay between attempts
* @param retryResultAction Action to execute and retry
* @param context Context to execute actions in
* @returns New Result which represents either failure or success
*/
static Retry<T>(times: number, delay: number, retryResultAction: () => Result<T>): Result<T> {
Expand Down
16 changes: 16 additions & 0 deletions tests/Result.FailAsProcessed.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Result } from '../src/Result';
import { executeResult } from './executeResult';

describe('.FailAsProcessed()', () => {
test('produces fail that is not overridden', done => {
executeResult(
done,
Result.FailAsProcessed('original')
.onSuccess(_ => done('No success expected'))
.withProcessedFail(() => 'overridden')
.onFailure(error => {
expect(error).toBe('original');
})
);
});
});
16 changes: 16 additions & 0 deletions tests/Result.FailAsProcessedWithError.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Result } from '../src/Result';
import { executeResult } from './executeResult';

describe('.FailAsProcessedWithError()', () => {
test('produces fail that is not overridden', done => {
executeResult(
done,
Result.FailAsProcessedWithError(new Error('original'))
.onSuccess(_ => done('No success expected'))
.withProcessedFail(() => 'overridden')
.onFailure(error => {
expect(error).toBe('original');
})
);
});
});

0 comments on commit 3c873f1

Please sign in to comment.