feat(core): HttpRequestError
MODIFY
packages/core/src/http/error/http.error.effect.ts
packages/core/src/http/error/http.error.effect.ts
@@ -18,7 +18,7 @@ const errorFactory = (status: HttpStatus, error: Error) =>
? { error: { status, message: error.message, data: error.data, context: error.context } }
: { error: { status, message: error.message } };
-export const defaultError$: HttpErrorEffect<Error> = req$ =>
+export const defaultError$: HttpErrorEffect = req$ =>
req$.pipe(
map(({ error = defaultHttpError }) => {
const status = getStatusCode(error);

MODIFY
packages/core/src/http/error/http.error.model.ts
packages/core/src/http/error/http.error.model.ts
@@ -6,6 +6,7 @@ import { coreErrorFactory } from '../../error/error.factory';
export enum HttpErrorType {
HTTP_ERROR = 'HttpError',
+ HTTP_REQUEST_ERROR = 'HttpRequestError',
}
export class HttpError extends ExtendableError {
@@ -20,9 +21,21 @@ export class HttpError extends ExtendableError {
}
}
+export class HttpRequestError extends ExtendableError {
+ constructor(
+ public readonly request: HttpRequest,
+ public readonly error: Error,
+ ) {
+ super(HttpErrorType.HTTP_REQUEST_ERROR, 'An error occured while processing a request');
+ }
+}
+
export const isHttpError = (error: Error): error is HttpError =>
error.name === HttpErrorType.HTTP_ERROR;
+export const isHttpRequestError = (error: Error): error is HttpRequestError =>
+ error.name === HttpErrorType.HTTP_REQUEST_ERROR;
+
export const unexpectedErrorWhileSendingErrorFactory = (error: Error) => {
const message = `An unexpected error ${chalk.red(`"${error.message}"`)} occured while sending an error response. Please check your error effect.`;
return coreErrorFactory(message, { printStacktrace: false });

MODIFY
packages/core/src/http/router/http.router.helpers.ts
packages/core/src/http/router/http.router.helpers.ts
import { Observable, pipe, throwError, of } from 'rxjs';
import { pipeFromArray } from 'rxjs/internal/util/pipe';
import { mergeMap, catchError, map } from 'rxjs/operators';
-import { HttpRequest, HttpStatus } from '../http.interface';
+import { HttpRequest } from '../http.interface';
import { HttpEffectResponse } from '../effects/http.effects.interface';
-import { isHttpError, HttpError } from '../error/http.error.model';
+import { isHttpError, HttpError, HttpRequestError } from '../error/http.error.model';
import { RouteCombinerConfig, RouteEffectGroup } from './http.router.interface';
export const isRouteEffectGroup = (item: any): item is RouteEffectGroup =>
@@ -22,7 +22,7 @@ export const decorateEffect = (stream: Observable<HttpRequest>) => {
catchError((error: Error) =>
isHttpError(error)
? throwError(new HttpError(error.message, error.status, error.data, request, error.context))
- : throwError(new HttpError(error.message, HttpStatus.INTERNAL_SERVER_ERROR, undefined, request))
+ : throwError(new HttpRequestError(request, error))
),
])(of(request)) as Observable<HttpEffectResponse>),
)(stream);
@@ -39,7 +39,7 @@ export const decorateMiddleware = (stream: Observable<HttpRequest>) => {
catchError((error: Error) =>
isHttpError(error)
? throwError(new HttpError(error.message, error.status, error.data, request, error.context))
- : throwError(new HttpError(error.message, HttpStatus.INTERNAL_SERVER_ERROR, undefined, request))
+ : throwError(new HttpRequestError(request, error))
),
])(of(request)) as Observable<HttpRequest>),
)(stream);

MODIFY
packages/core/src/index.ts
packages/core/src/index.ts
// http
export { EffectFactory } from './http/effects/http.effects.factory';
export { defaultError$ } from './http/error/http.error.effect';
-export { HttpError, isHttpError } from './http/error/http.error.model';
+export { HttpError, HttpRequestError, isHttpError, isHttpRequestError } from './http/error/http.error.model';
export { combineRoutes } from './http/router/http.router.combiner';
export { r } from './http/router/http.router.ixbuilder';
export * from './http/router/http.router.interface';

feat(core): HttpRequestError