import * as chalk from 'chalk' ; import { CoreError , coreErrorFactory } from '@marblejs/core' ; import { NamedError } from '@marblejs/core/dist/+internal/utils' ; import { HttpStatus , HttpRequest } from '../http.interface' ; import { HttpEffectResponse } from '../effects/http.effects.interface' ; export enum HttpErrorType { HTTP_ERROR = 'HttpError' , HTTP_REQUEST_ERROR = 'HttpRequestError' } export class HttpError extends NamedError { constructor ( public readonly message , public readonly status , public readonly data ? , public readonly request ? , public readonly context ? ) { super ( HttpErrorType . HTTP_ERROR , message ) ; } } export class HttpRequestError extends NamedError { constructor ( public readonly request , public readonly error ) { super ( HttpErrorType . HTTP_REQUEST_ERROR , 'An error occured while processing a request' ) ; } } export const isHttpError = ( error ) : error is HttpError => error ?. name === HttpErrorType . HTTP_ERROR ; export const isHttpRequestError = ( error ) : error is HttpRequestError => error ?. name === HttpErrorType . HTTP_REQUEST_ERROR ; export const isURIError = ( error ) : error is URIError => error ?. name === 'URIError' ; export const unexpectedErrorWhileSendingResponseFactory = ( error ) : CoreError => { const message = `An unexpected error ${ chalk . red ( `"${ error . message }"` ) } occured while sending a response. Please check your output/error effect.` ; return coreErrorFactory ( message , { printStacktrace : false } ) ; } ; export const responseNotBoundToRequestErrorFactory = ( response ) : CoreError => { const message = `An effect returned a response: "${ chalk . yellow ( JSON . stringify ( response ) ) }" without bound request` ; return coreErrorFactory ( message , { printStacktrace : false } ) ; } ; export const errorNotBoundToRequestErrorFactory = ( error ) : CoreError => { const message = `An effect or middleware thrown an error ${ chalk . red ( `"${ error . message }"` ) } without bound request.` ; return coreErrorFactory ( message , { printStacktrace : false } ) ; } ;