diff --git a/src/lib/error-handler.service.ts b/src/lib/error-handler.service.ts index 561cb67d63f45796c7149b0e28151242daba0178..a9759a7db2f0cb59957a2dd5b5b63e2663ee143e 100644 --- a/src/lib/error-handler.service.ts +++ b/src/lib/error-handler.service.ts @@ -32,7 +32,10 @@ export class ErrorHandlerService implements ErrorHandler { } private getDebugContext(error?: Error): any { - return error ? (error as any).ngDebugContext || this.getDebugContext(this.getOriginalError(error)) : null; + return error + ? (error as any).ngDebugContext || + this.getDebugContext(this.getOriginalError(error)) + : null; } private getOriginalError(error: Error): Error { @@ -43,7 +46,9 @@ export class ErrorHandlerService implements ErrorHandler { return original; } - private getErrorLogger(error: Error): (console: Console, ...values: any[]) => void { + private getErrorLogger( + error: Error + ): (console: Console, ...values: any[]) => void { return (error as any).ngErrorLogger || this.defaultErrorLogger; } diff --git a/src/lib/types/app.error.spec.ts b/src/lib/types/app.error.spec.ts index b9ccd9c0f6e0b59356a0256a6ea398cf8a52105f..35890f67cf05478259c13a35577b0f7d2286b4ff 100644 --- a/src/lib/types/app.error.spec.ts +++ b/src/lib/types/app.error.spec.ts @@ -38,7 +38,11 @@ describe('AppError', () => { expect(err.dispatch(handler)).toBeTruthy(); - expect(handler.logError).toHaveBeenCalledWith('Bam !', 'Error', baseErr.stack); + expect(handler.logError).toHaveBeenCalledWith( + 'Bam !', + 'Error', + baseErr.stack + ); }); }); }); diff --git a/src/lib/types/app.error.ts b/src/lib/types/app.error.ts index 807a228faea6040ddfe67b8170e871357d2c5dff..e45aac0552b5eb0dc4617718d6e1c82e74ffb416 100644 --- a/src/lib/types/app.error.ts +++ b/src/lib/types/app.error.ts @@ -1,5 +1,9 @@ import { DispatchableError } from './interfaces'; -import { isErrorLogger, isErrorMessageHandler, isValidationErrorHandler } from './type-guards.function'; +import { + isErrorLogger, + isErrorMessageHandler, + isValidationErrorHandler, +} from './type-guards.function'; /** * Erreur "enveloppant" une autre erreur. @@ -20,13 +24,18 @@ export class AppError<T> implements DispatchableError { public dispatch(handler: any): boolean { return ( - (isErrorLogger(handler) && handler.logError(this.message, this.name, this.stack)) || + (isErrorLogger(handler) && + handler.logError(this.message, this.name, this.stack)) || (isErrorMessageHandler(handler) && handler.setError(this.message)) || - (isValidationErrorHandler(handler) && handler.setErrors({ message: this.message })) + (isValidationErrorHandler(handler) && + handler.setErrors({ message: this.message })) ); } protected getName(): string { - return (this.original instanceof Error && this.original.name) || this.constructor.name; + return ( + (this.original instanceof Error && this.original.name) || + this.constructor.name + ); } } diff --git a/src/lib/types/clear-errors.function.spec.ts b/src/lib/types/clear-errors.function.spec.ts index be540deacdee8d25e0dce230af2703e86cdaabfc..6ba8c92b4fb9ab6b123e882cbd15a74d84a755b2 100644 --- a/src/lib/types/clear-errors.function.spec.ts +++ b/src/lib/types/clear-errors.function.spec.ts @@ -2,7 +2,11 @@ import { clearErrors } from './clear-errors.function'; describe('clearErrors', () => { it('should clear errors using all ErrorHandler methods', () => { - const handler = jasmine.createSpyObj('handler', ['setError', 'setConstraintViolations', 'setErrors']); + const handler = jasmine.createSpyObj('handler', [ + 'setError', + 'setConstraintViolations', + 'setErrors', + ]); handler.setError.and.returnValue(true); handler.setErrors.and.returnValue(true); diff --git a/src/lib/types/clear-errors.function.ts b/src/lib/types/clear-errors.function.ts index 56f958c5ffd70ffd5fd10f5656390e9e4d4a586d..bc77a531a8fda9fed08087834d10061111b6a0c6 100644 --- a/src/lib/types/clear-errors.function.ts +++ b/src/lib/types/clear-errors.function.ts @@ -20,7 +20,10 @@ export function clearErrors(handler: any): boolean { if (isValidationErrorHandler(handler) && handler.setErrors(null)) { handled = true; } - if (isConstraintViolationHandler(handler) && handler.setConstraintViolations(null)) { + if ( + isConstraintViolationHandler(handler) && + handler.setConstraintViolations(null) + ) { handled = true; } return handled; diff --git a/src/lib/types/constraint-violations.class.spec.ts b/src/lib/types/constraint-violations.class.spec.ts index 7e96edc482a2fa09071a4c64d2e42ba51ffb5926..c6f9886aa236c230a438609f6b8e97cdf474f837 100644 --- a/src/lib/types/constraint-violations.class.spec.ts +++ b/src/lib/types/constraint-violations.class.spec.ts @@ -18,7 +18,11 @@ describe('ConstraintViolations', () => { status: 400, statusText: 'Constraint violations', }); - err = new ConstraintViolations(orig, orig.statusText, orig.error.violations); + err = new ConstraintViolations( + orig, + orig.statusText, + orig.error.violations + ); }); it('.isNullError should be false', () => { @@ -36,7 +40,9 @@ describe('ConstraintViolations', () => { }); it('should set constraint violations', () => { - const handler = jasmine.createSpyObj('handler', ['setConstraintViolations']); + const handler = jasmine.createSpyObj('handler', [ + 'setConstraintViolations', + ]); handler.setConstraintViolations.and.returnValue(true); expect(err.dispatch(handler)).toBeTruthy(); diff --git a/src/lib/types/constraint-violations.class.ts b/src/lib/types/constraint-violations.class.ts index aae146473fc3e96b1ab985644a78d6166e513d5b..53364ea9c94643e10ae523b433f56af0264b5f20 100644 --- a/src/lib/types/constraint-violations.class.ts +++ b/src/lib/types/constraint-violations.class.ts @@ -8,18 +8,25 @@ import { isConstraintViolationHandler } from './type-guards.function'; * Erreur de validation retournée par api-platform. */ export class ConstraintViolations extends BadRequestError { - public constructor(original: HttpErrorResponse, message: string, public readonly violations: ConstraintViolation[]) { + public constructor( + original: HttpErrorResponse, + message: string, + public readonly violations: ConstraintViolation[] + ) { super(original, message); } public dispatch(handler: any): boolean { return ( - (isConstraintViolationHandler(handler) && handler.setConstraintViolations(this.violations)) || + (isConstraintViolationHandler(handler) && + handler.setConstraintViolations(this.violations)) || super.dispatch(handler) ); } - public static create(violations: ConstraintViolation[]): ConstraintViolations { + public static create( + violations: ConstraintViolation[] + ): ConstraintViolations { return new ConstraintViolations( new HttpErrorResponse({ status: 400, diff --git a/src/lib/types/http.error.ts b/src/lib/types/http.error.ts index 7ba36660ae819b6b46c8dda6abeb6bc27e4e0041..0fba15fcb388dce2a22e1c9f7eac0ef614fdc3ce 100644 --- a/src/lib/types/http.error.ts +++ b/src/lib/types/http.error.ts @@ -7,7 +7,12 @@ import { AppError } from './app.error'; */ export class HTTPError extends AppError<HttpErrorResponse> { public constructor(original: HttpErrorResponse, message?: string) { - super(original, message || original.message || original.statusText, undefined, original.status >= 500); + super( + original, + message || original.message || original.statusText, + undefined, + original.status >= 500 + ); } public get status(): number { diff --git a/src/lib/types/interfaces.ts b/src/lib/types/interfaces.ts index 7834cde6a4b3aab769b29db4a00343b353b72822..9175d237a2936cd6710b6f22bcb21ddf97d70985 100644 --- a/src/lib/types/interfaces.ts +++ b/src/lib/types/interfaces.ts @@ -25,14 +25,23 @@ export interface ValidationErrorHandler { } export interface ErrorLogger { - logError(message: string, name: string, stack?: string, context?: any): boolean; + logError( + message: string, + name: string, + stack?: string, + context?: any + ): boolean; } export interface ErrorClearer { clearErrors(): boolean; } -export type ErrorHandler = ErrorMessageHandler | ConstraintViolationHandler | ValidationErrorHandler | ErrorLogger; +export type ErrorHandler = + | ErrorMessageHandler + | ConstraintViolationHandler + | ValidationErrorHandler + | ErrorLogger; export interface DispatchableError extends Error { readonly isNullError: boolean; diff --git a/src/lib/types/normalize-error.function.spec.ts b/src/lib/types/normalize-error.function.spec.ts index 1e3c07a610d151dadcd9eecbd260abda9265f089..693e18ca250d1a10fb6463e5bdc426cc1b0f26b3 100644 --- a/src/lib/types/normalize-error.function.spec.ts +++ b/src/lib/types/normalize-error.function.spec.ts @@ -2,25 +2,36 @@ import { HttpErrorResponse } from '@angular/common/http'; import { AppError } from './app.error'; import { ConstraintViolations } from './constraint-violations.class'; -import { AccessDeniedError, AuthenticationError, BadRequestError, HTTPError } from './http.error'; +import { + AccessDeniedError, + AuthenticationError, + BadRequestError, + HTTPError, +} from './http.error'; import { normalizeError } from './normalize-error.function'; describe('normalizeError', () => { it('should use string as message', () => { const err = 'Error'; - expect(normalizeError(err)).toEqual(new AppError(err, err, undefined, false)); + expect(normalizeError(err)).toEqual( + new AppError(err, err, undefined, false) + ); }); describe('should return NullError on empty values', () => { const falsies = [null, false, {}, [], '', 0]; for (const falsy of falsies) { - it(JSON.stringify(falsy), () => expect(normalizeError(falsy).isNullError).toBeTruthy()); + it(JSON.stringify(falsy), () => + expect(normalizeError(falsy).isNullError).toBeTruthy() + ); } }); it('should reuse Error properties', () => { const err = new Error('BAM!'); - expect(normalizeError(err)).toEqual(new AppError(err, err.message, err.stack, false)); + expect(normalizeError(err)).toEqual( + new AppError(err, err.message, err.stack, false) + ); }); it('should return AppErrors as is', () => { @@ -76,7 +87,8 @@ describe('normalizeError', () => { const violations = [ { propertyPath: 'properties', - message: 'The product must have the minimal properties required ("description", "price")', + message: + 'The product must have the minimal properties required ("description", "price")', }, ]; const err = new HttpErrorResponse({ @@ -85,7 +97,8 @@ describe('normalizeError', () => { '@type': 'ConstraintViolationList', 'hydra:title': 'An error occurred', 'hydra:description': - 'properties: The product must have the minimal properties required ("description",' + ' "price")', + 'properties: The product must have the minimal properties required ("description",' + + ' "price")', violations, }, status: 400, diff --git a/src/lib/types/normalize-error.function.ts b/src/lib/types/normalize-error.function.ts index 5cab9a3b809a3e552747ee94485ea76190d227d9..18c204f1b6e99bd629db19e0b640d9d202b24649 100644 --- a/src/lib/types/normalize-error.function.ts +++ b/src/lib/types/normalize-error.function.ts @@ -3,7 +3,12 @@ import * as _ from 'lodash'; import { AppError } from './app.error'; import { ConstraintViolations } from './constraint-violations.class'; -import { AccessDeniedError, AuthenticationError, BadRequestError, HTTPError } from './http.error'; +import { + AccessDeniedError, + AuthenticationError, + BadRequestError, + HTTPError, +} from './http.error'; import { DispatchableError } from './interfaces'; import { NullError } from './null.error'; @@ -12,7 +17,9 @@ import { NullError } from './null.error'; * * Les informations intéressantes d'HttpErrorResponse et Error sont conservés. */ -export function normalizeError(err: DispatchableError | HttpErrorResponse | Error | any): DispatchableError { +export function normalizeError( + err: DispatchableError | HttpErrorResponse | Error | any +): DispatchableError { if (err instanceof AppError || err === NullError) { return err; } @@ -26,7 +33,12 @@ export function normalizeError(err: DispatchableError | HttpErrorResponse | Erro return NullError; } if (typeof err === 'object') { - return new AppError(err, extractMessage(err), err.stack, err.transient || false); + return new AppError( + err, + extractMessage(err), + err.stack, + err.transient || false + ); } return new AppError(err, extractMessage(err)); } diff --git a/src/lib/types/null.error.spec.ts b/src/lib/types/null.error.spec.ts index be8d862e8e76ebdc7459824cad3e146609fd41bd..ee15dba39b789e76373798ca2f006c835a927a1a 100644 --- a/src/lib/types/null.error.spec.ts +++ b/src/lib/types/null.error.spec.ts @@ -16,7 +16,9 @@ describe('NullError', () => { }); it('should clear constraint violations', () => { - const handler = jasmine.createSpyObj('handler', ['setConstraintViolations']); + const handler = jasmine.createSpyObj('handler', [ + 'setConstraintViolations', + ]); handler.setConstraintViolations.and.returnValue(true); expect(NullError.dispatch(handler)).toBeTruthy(); diff --git a/src/lib/types/type-guards.function.ts b/src/lib/types/type-guards.function.ts index bb5e4a5eb58815925187aa22f72b5bf6f0cb9a32..724ef2da023c66054e4f5a07793bfe35a13e8408 100644 --- a/src/lib/types/type-guards.function.ts +++ b/src/lib/types/type-guards.function.ts @@ -14,11 +14,18 @@ export function isErrorMessageHandler(that: any): that is ErrorMessageHandler { return _.hasIn(that, 'setError') && _.isFunction(that.setError); } -export function isConstraintViolationHandler(that: any): that is ConstraintViolationHandler { - return _.hasIn(that, 'setConstraintViolations') && _.isFunction(that.setConstraintViolations); +export function isConstraintViolationHandler( + that: any +): that is ConstraintViolationHandler { + return ( + _.hasIn(that, 'setConstraintViolations') && + _.isFunction(that.setConstraintViolations) + ); } -export function isValidationErrorHandler(that: any): that is ValidationErrorHandler { +export function isValidationErrorHandler( + that: any +): that is ValidationErrorHandler { return _.hasIn(that, 'setErrors') && _.isFunction(that.setErrors); } diff --git a/src/lib/types/unhandled.error.ts b/src/lib/types/unhandled.error.ts index 684d65a5fb7a9c6286802c8fa829bf4fb02c15b9..3479c68c7a40a76cabaa20346cf05dcd65d22315 100644 --- a/src/lib/types/unhandled.error.ts +++ b/src/lib/types/unhandled.error.ts @@ -6,6 +6,11 @@ import { DispatchableError } from './interfaces'; */ export class UnhandledError extends AppError<DispatchableError> { public constructor(original: DispatchableError) { - super(original, `Unhandled error: ${original.message}`, original.stack, false); + super( + original, + `Unhandled error: ${original.message}`, + original.stack, + false + ); } } diff --git a/src/lib/types/validation.error.ts b/src/lib/types/validation.error.ts index 8a0cb72401b1669e75d3155eca625783f74d6146..7825c5147128137d325dd80548261c9ccd57c38f 100644 --- a/src/lib/types/validation.error.ts +++ b/src/lib/types/validation.error.ts @@ -9,6 +9,9 @@ export class ValidationError extends AppError<ValidationErrors> { } public dispatch(handler: any): boolean { - return (isValidationErrorHandler(handler) && handler.setErrors(this.errors)) || super.dispatch(handler); + return ( + (isValidationErrorHandler(handler) && handler.setErrors(this.errors)) || + super.dispatch(handler) + ); } }