Commit e875676d authored by Guillaume Perréal's avatar Guillaume Perréal
Browse files

Fixed coding style.

No related merge requests found
Showing with 126 additions and 32 deletions
+126 -32
......@@ -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;
}
......
......@@ -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
);
});
});
});
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
);
}
}
......@@ -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);
......
......@@ -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;
......
......@@ -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();
......
......@@ -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,
......
......@@ -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 {
......
......@@ -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;
......
......@@ -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,
......
......@@ -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));
}
......
......@@ -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();
......
......@@ -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);
}
......
......@@ -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
);
}
}
......@@ -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)
);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment