-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add
catchAndArrayifyErrors
pipe (#2556)
- Loading branch information
Showing
3 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
libs/core/src/operators/catch-and-arrayify-errors.pipe.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { | ||
Observable, | ||
of, | ||
throwError, | ||
} from 'rxjs'; | ||
|
||
import { | ||
DaffError, | ||
DaffInheritableError, | ||
} from '@daffodil/core'; | ||
|
||
import { catchAndArrayifyErrors } from './catch-and-arrayify-errors.pipe'; | ||
|
||
class TestError extends DaffInheritableError implements DaffError { | ||
public readonly code = 'test'; | ||
|
||
constructor(message?: string) { | ||
super(message); | ||
} | ||
} | ||
|
||
describe('@daffodil/core | catchAndArrayifyErrors', () => { | ||
let result: Observable<any>; | ||
let cb: jasmine.Spy; | ||
|
||
beforeEach(() => { | ||
cb = jasmine.createSpy(); | ||
cb.and.returnValue(of(null)); | ||
}); | ||
|
||
describe('when an error is thrown into the stream', () => { | ||
let error: DaffError; | ||
|
||
beforeEach(() => { | ||
error = new TestError('message'); | ||
result = throwError(() => error).pipe( | ||
catchAndArrayifyErrors(cb), | ||
); | ||
}); | ||
|
||
it('should invoke the callback with an array of that error', (done) => { | ||
result.subscribe(() => { | ||
expect(cb).toHaveBeenCalledWith([error]); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('when an array of errors is thrown into the stream', () => { | ||
let errors: DaffError[]; | ||
|
||
beforeEach(() => { | ||
errors = [new TestError('message1'), new TestError('message2')]; | ||
result = throwError(() => errors).pipe( | ||
catchAndArrayifyErrors(cb), | ||
); | ||
}); | ||
|
||
it('should invoke the callback with that array', (done) => { | ||
result.subscribe(() => { | ||
expect(cb).toHaveBeenCalledWith(errors); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('when nothing is thrown into the stream', () => { | ||
beforeEach(() => { | ||
result = of('test').pipe( | ||
catchAndArrayifyErrors(cb), | ||
); | ||
}); | ||
|
||
it('should not invoke the callback ', (done) => { | ||
result.subscribe(() => { | ||
expect(cb).not.toHaveBeenCalled(); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { | ||
Observable, | ||
pipe, | ||
OperatorFunction, | ||
catchError, | ||
} from 'rxjs'; | ||
|
||
import { DaffError } from '../errors/public_api'; | ||
|
||
/** | ||
* Catches errors and invokes the callback with an array of errors. | ||
*/ | ||
export const catchAndArrayifyErrors = <TReturn, TError extends DaffError = DaffError>(transformError: (errors: TError[]) => Observable<TReturn>) => pipe<Observable<unknown>, Observable<TReturn>>( | ||
<OperatorFunction<unknown, TReturn>>catchError((err: TError | TError[]) => transformError(Array.isArray(err) ? err : [err])), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './backoff.pipe'; | ||
export * from './any-of.pipe'; | ||
export * from './all-of.pipe'; | ||
export * from './catch-and-arrayify-errors.pipe'; |