From e875676da68e6ac28d7491b34d2cb8b048c07e06 Mon Sep 17 00:00:00 2001 From: Perreal Guillaume <guillaume.perreal@irstea.fr> Date: Tue, 17 Sep 2019 12:28:57 +0200 Subject: [PATCH] Fixed coding style. --- src/lib/error-handler.service.ts | 9 +++++-- src/lib/types/app.error.spec.ts | 6 ++++- src/lib/types/app.error.ts | 17 ++++++++++--- src/lib/types/clear-errors.function.spec.ts | 6 ++++- src/lib/types/clear-errors.function.ts | 5 +++- .../types/constraint-violations.class.spec.ts | 10 ++++++-- src/lib/types/constraint-violations.class.ts | 13 +++++++--- src/lib/types/http.error.ts | 7 +++++- src/lib/types/interfaces.ts | 13 ++++++++-- .../types/normalize-error.function.spec.ts | 25 ++++++++++++++----- src/lib/types/normalize-error.function.ts | 18 ++++++++++--- src/lib/types/null.error.spec.ts | 4 ++- src/lib/types/type-guards.function.ts | 13 +++++++--- src/lib/types/unhandled.error.ts | 7 +++++- src/lib/types/validation.error.ts | 5 +++- 15 files changed, 126 insertions(+), 32 deletions(-) diff --git a/src/lib/error-handler.service.ts b/src/lib/error-handler.service.ts index 561cb67..a9759a7 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 b9ccd9c..35890f6 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 807a228..e45aac0 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 be540de..6ba8c92 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 56f958c..bc77a53 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 7e96edc..c6f9886 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 aae1464..53364ea 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 7ba3666..0fba15f 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 7834cde..9175d23 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 1e3c07a..693e18c 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 5cab9a3..18c204f 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 be8d862..ee15dba 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 bb5e4a5..724ef2d 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 684d65a..3479c68 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 8a0cb72..7825c51 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) + ); } } -- GitLab