diff --git a/projects/funixproductions-requests/src/lib/services/core/components/requests/crud-http-client.ts b/projects/funixproductions-requests/src/lib/services/core/components/requests/crud-http-client.ts index 3c03ec9..b182d36 100644 --- a/projects/funixproductions-requests/src/lib/services/core/components/requests/crud-http-client.ts +++ b/projects/funixproductions-requests/src/lib/services/core/components/requests/crud-http-client.ts @@ -1,5 +1,5 @@ -import {HttpClient, HttpParams} from "@angular/common/http"; -import {Observable} from "rxjs"; +import {HttpClient, HttpErrorResponse, HttpParams} from "@angular/common/http"; +import {catchError, Observable, throwError} from "rxjs"; import {PageOption, Paginated} from "../../dtos/paginated"; import {QueryBuilder} from "../query.builder"; import {FunixprodHttpClient} from "./funixprod-http-client"; @@ -36,23 +36,48 @@ export abstract class CrudHttpClient extends FunixprodHttpCl search: (queryBuilder === null ? '' : queryBuilder.get()) }; - return this.http.get>(this.domain + this.path, {headers: super.getHeaders(), params: {...params}}); + return this.http.get>(this.domain + this.path, {headers: super.getHeaders(), params: {...params}}) + .pipe( + catchError((error: HttpErrorResponse) => { + return throwError(() => this.buildErrorDto(error)); + }) + ); } getById(id: string): Observable { - return this.http.get(this.domain + this.path + "/" + id, {headers: super.getHeaders()}); + return this.http.get(this.domain + this.path + "/" + id, {headers: super.getHeaders()}) + .pipe( + catchError((error: HttpErrorResponse) => { + return throwError(() => this.buildErrorDto(error)); + }) + ); } create(dto: DTO): Observable { return this.http.post(this.domain + this.path, dto, {headers: super.getHeaders()}) + .pipe( + catchError((error: HttpErrorResponse) => { + return throwError(() => this.buildErrorDto(error)); + }) + ); } patch(dto: DTO): Observable { return this.http.patch(this.domain + this.path, dto, {headers: super.getHeaders()}) + .pipe( + catchError((error: HttpErrorResponse) => { + return throwError(() => this.buildErrorDto(error)); + }) + ); } update(dto: DTO): Observable { return this.http.put(this.domain + this.path, dto, {headers: super.getHeaders()}) + .pipe( + catchError((error: HttpErrorResponse) => { + return throwError(() => this.buildErrorDto(error)); + }) + ); } delete(id: string): Observable { @@ -61,6 +86,10 @@ export abstract class CrudHttpClient extends FunixprodHttpCl return this.http.delete(this.domain + this.path, { params: httpParams, headers: super.getHeaders() - }) + }).pipe( + catchError((error: HttpErrorResponse) => { + return throwError(() => this.buildErrorDto(error)); + }) + ) } } diff --git a/projects/funixproductions-requests/src/lib/services/core/components/requests/funixprod-http-client.ts b/projects/funixproductions-requests/src/lib/services/core/components/requests/funixprod-http-client.ts index d981d7c..1cedab4 100644 --- a/projects/funixproductions-requests/src/lib/services/core/components/requests/funixprod-http-client.ts +++ b/projects/funixproductions-requests/src/lib/services/core/components/requests/funixprod-http-client.ts @@ -1,4 +1,5 @@ -import {HttpHeaders} from "@angular/common/http"; +import {HttpErrorResponse, HttpHeaders} from "@angular/common/http"; +import {ErrorDto} from "../../dtos/error-dto"; export abstract class FunixprodHttpClient { @@ -28,4 +29,32 @@ export abstract class FunixprodHttpClient { return headersToSend; } + public buildErrorDto(error: HttpErrorResponse): ErrorDto { + let customError: ErrorDto = new ErrorDto( + 'Une erreur est survenue : ', + error.status, + Date.now(), + [] + ); + + if (error.error instanceof ErrorEvent) { + customError.error += error.error.message; + } else { + if (error.error.error) { + customError.error += error.error.error; + } + if (error.error.timestamp) { + customError.timestamp = error.error.timestamp; + } + if (error.error.fieldErrors) { + customError.fieldErrors = error.error.fieldErrors.map((fieldError: any) => ({ + field: fieldError.field, + message: fieldError.message + })); + } + } + + return customError; + } + } diff --git a/projects/funixproductions-requests/src/lib/services/core/dtos/api-dto.ts b/projects/funixproductions-requests/src/lib/services/core/dtos/api-dto.ts index 4f028a5..a92c247 100644 --- a/projects/funixproductions-requests/src/lib/services/core/dtos/api-dto.ts +++ b/projects/funixproductions-requests/src/lib/services/core/dtos/api-dto.ts @@ -3,6 +3,9 @@ */ export abstract class ApiDTO { public id?: string; + public createdAt?: Date; + public updatedAt?: Date; + } diff --git a/projects/funixproductions-requests/src/lib/services/core/dtos/error-dto.ts b/projects/funixproductions-requests/src/lib/services/core/dtos/error-dto.ts new file mode 100644 index 0000000..8dccb46 --- /dev/null +++ b/projects/funixproductions-requests/src/lib/services/core/dtos/error-dto.ts @@ -0,0 +1,23 @@ +export class ErrorDto { + error: string; + status: number; + timestamp: number; + fieldErrors: FieldErrorDto[] = []; + + constructor(error: string, status: number, timestamp: number, fieldErrors: FieldErrorDto[]) { + this.error = error; + this.status = status; + this.timestamp = timestamp; + this.fieldErrors = fieldErrors; + } +} + +export class FieldErrorDto { + field: string; + message: string; + + constructor(field: string, message: string) { + this.field = field; + this.message = message; + } +} diff --git a/projects/funixproductions-requests/src/public-api.ts b/projects/funixproductions-requests/src/public-api.ts index dc2a0e4..77d47db 100644 --- a/projects/funixproductions-requests/src/public-api.ts +++ b/projects/funixproductions-requests/src/public-api.ts @@ -6,6 +6,7 @@ * Core packages */ export * from './lib/services/core/dtos/api-dto'; +export * from './lib/services/core/dtos/error-dto'; export * from './lib/services/core/dtos/paginated'; export * from './lib/services/core/components/requests/crud-http-client'; export * from './lib/services/core/components/requests/funixprod-http-client';