test cases - error.middleware
ADD
src/middlewares/error.middleware.spec.ts
+import { mapTo } from 'rxjs/operators';
+import { HttpRequest, HttpResponse } from '../http.interface';
+import { HttpError } from '../util/error.util';
+import { Marbles } from '../util/marbles.spec-util';
+import { error$, getErrorMiddleware } from './error.middleware';
+
+const createMockRes = () => ({} as HttpResponse);
+const createMockReq = (url = '/') => ({ url } as HttpRequest);
+
+describe('Error middleware', () => {
+
+ it('error$ maps HttpError', () => {
+ const error = new HttpError('test-message', 400);
+ const expectedResponse = {
+ status: 400,
+ body: { error: {
+ message: 'test-message',
+ status: 400,
+ }},
+ };
+
+ Marbles.assertEffect(error$, [
+ ['-a-', { a: createMockReq('/') }],
+ ['-a-', { a: expectedResponse }],
+ ], { response: createMockRes(), error });
+ });
+
+ it('error$ maps other errors', () => {
+ const error = new Error('test-message');
+ const expectedResponse = {
+ status: 500,
+ body: { error: {
+ message: 'test-message',
+ status: 500,
+ }},
+ };
+
+ Marbles.assertEffect(error$, [
+ ['-a-', { a: createMockReq('/') }],
+ ['-a-', { a: expectedResponse }],
+ ], { response: createMockRes(), error });
+ });
+
+ it('#getErrorMiddleware provides middleware implementation', () => {
+ const customError$ = req$ => req$.pipe(mapTo({ status: 500, body: 'error' }));
+ const middleware = getErrorMiddleware(customError$);
+ expect(middleware).toBe(customError$);
+ });
+
+ it('#getErrorMiddleware provides default middleware implementation if not passed', () => {
+ const middleware = getErrorMiddleware();
+ expect(middleware).toBe(error$);
+ });
+
+});

MODIFY
src/middlewares/error.middleware.ts
src/middlewares/error.middleware.ts
@@ -3,21 +3,21 @@ import { Effect, EffectResponse } from '../effects/effects.interface';
import { HttpStatus } from '../http.interface';
import { HttpError, isHttpError } from '../util/error.util';
-export type Error = HttpError;
+export type ThrowedError = HttpError | Error;
-export const getErrorMiddleware = (customError$?: Effect<EffectResponse, Error>) => !!customError$
+export const getErrorMiddleware = (customError$?: Effect<EffectResponse, ThrowedError>) => !!customError$
? customError$
: error$;
-export const getStatusCode = (error: Error): HttpStatus => isHttpError(error)
+const getStatusCode = (error: ThrowedError): HttpStatus => isHttpError(error)
? error.status
: HttpStatus.INTERNAL_SERVER_ERROR;
-export const errorFactory = (message: string, status: HttpStatus, data?: any) => ({
+const errorFactory = (message: string, status: HttpStatus, data?: any) => ({
error: { status, message, data }
});
-export const error$: Effect<EffectResponse, Error> = (request$, response, error) => request$
+export const error$: Effect<EffectResponse, ThrowedError> = (request$, response, error) => request$
.pipe(
map(req => {
const { message } = error;

test cases - error.middleware