Skip to content

Commit 06c4efd

Browse files
committed
feat(utils): Added as-uncaught-error helper
1 parent 5eb7daf commit 06c4efd

8 files changed

Lines changed: 56 additions & 4 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@acamica/task",
3-
"version": "0.1.1",
3+
"version": "0.1.2",
44
"description": "Promise replacement made with TypeScript more suitable for functional programming and error handling",
55
"keywords": [
66
"async",

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export * from './case-error';
1+
export * from './utils';
22
export * from './task';

src/task.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export class Task <T, E> {
4444
});
4545
}
4646

47-
static fromPromise<TResult> (promise: Promise<TResult>): Task<TResult, any> {
47+
static fromPromise<TResult, EResult = any> (promise: Promise<TResult>): Task<TResult, EResult> {
4848
return new Task((outerResolve, outerReject) => {
4949
promise.then(outerResolve, err => outerReject(err));
5050
});

src/utils/as-uncaught-error.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { Task, UncaughtError } from '../task';
2+
export function asUncaughtError (error: any): Task<never, UncaughtError> {
3+
return Task.reject(new UncaughtError(error));
4+
}

src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './case-error';
2+
export * from './as-uncaught-error';

test/task.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ describe('Task', () => {
124124
);
125125

126126
});
127+
127128
it('Should work with rejected promises', (cb) => {
128129
// GIVEN: A task created from a rejected promise
129130
const resolved = Task.fromPromise(Promise.reject('buu'));
@@ -134,6 +135,32 @@ describe('Task', () => {
134135
jestAssertNever(cb)
135136
);
136137
});
138+
139+
it('Should mantain any error when changed', (cb) => {
140+
// GIVEN: A task created from a rejected promise
141+
const task = Task.fromPromise(Promise.reject('buu'));
142+
143+
// WHEN: we chain it with some value
144+
const result = task.chain(_ => Task.resolve(0));
145+
// THEN: the task type should be Task<number, any> (check manually?)
146+
147+
// and the error should be the same
148+
result.fork(
149+
assertFork(cb, x => expect(x).toBe('buu')),
150+
jestAssertUntypedNeverCalled(cb)
151+
);
152+
});
153+
154+
it('Should be able to set rejected type', (cb) => {
155+
// GIVEN: A task created from a rejected promise
156+
const resolved = Task.fromPromise<never, string>(Promise.reject('buu'));
157+
158+
// WHEN: we fork, THEN: it should call the error function with the rejected value
159+
resolved.fork(
160+
assertFork(cb, x => expect(x).toBe('buu')),
161+
jestAssertNever(cb)
162+
);
163+
});
137164
});
138165
describe('chain', () => {
139166
it('Should handle functions that succeed', (cb) => {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Task, UncaughtError } from '../../src/task';
2+
import { asUncaughtError } from '../../src/utils/as-uncaught-error';
3+
import { assertFork, jestAssertNever, jestAssertUntypedNeverCalled } from '../jest-helper';
4+
5+
describe('asUncaughtError', () => {
6+
it('Should catch any error', (cb) => {
7+
// GIVEN: A task created from a rejected promise (with any error type)
8+
const task = Task.fromPromise(Promise.reject('something'));
9+
10+
// WHEN: We catch it as an UncaughtError
11+
const result = task.catch(asUncaughtError);
12+
13+
// THEN: The Task is rejected with an UncaughtError and the types get more specific
14+
// than any
15+
result.fork(
16+
assertFork(cb, err => expect(err).toBeInstanceOf(UncaughtError)),
17+
jestAssertNever(cb)
18+
);
19+
});
20+
});

test/utils/case-error.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const divideAndRejectPairs = (numerator: number, denominator: number) =>
3939

4040

4141

42-
describe.only('caseError:', () => {
42+
describe('caseError:', () => {
4343
it('Should catch and resolve the cased error', (cb) => {
4444
// GIVEN: A task that fails with DivisionByZeroError
4545
const task = divideAndRejectPairs(2, 0);

0 commit comments

Comments
 (0)