Skip to content

Commit

Permalink
added error dto on error requests
Browse files Browse the repository at this point in the history
  • Loading branch information
FunixG committed Jan 24, 2024
1 parent cc8d7d1 commit 5ccf7cf
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -36,23 +36,48 @@ export abstract class CrudHttpClient<DTO extends ApiDTO> extends FunixprodHttpCl
search: (queryBuilder === null ? '' : queryBuilder.get())
};

return this.http.get<Paginated<DTO>>(this.domain + this.path, {headers: super.getHeaders(), params: {...params}});
return this.http.get<Paginated<DTO>>(this.domain + this.path, {headers: super.getHeaders(), params: {...params}})
.pipe(
catchError((error: HttpErrorResponse) => {
return throwError(() => this.buildErrorDto(error));
})
);
}

getById(id: string): Observable<DTO> {
return this.http.get<DTO>(this.domain + this.path + "/" + id, {headers: super.getHeaders()});
return this.http.get<DTO>(this.domain + this.path + "/" + id, {headers: super.getHeaders()})
.pipe(
catchError((error: HttpErrorResponse) => {
return throwError(() => this.buildErrorDto(error));
})
);
}

create(dto: DTO): Observable<DTO> {
return this.http.post<DTO>(this.domain + this.path, dto, {headers: super.getHeaders()})
.pipe(
catchError((error: HttpErrorResponse) => {
return throwError(() => this.buildErrorDto(error));
})
);
}

patch(dto: DTO): Observable<DTO> {
return this.http.patch<DTO>(this.domain + this.path, dto, {headers: super.getHeaders()})
.pipe(
catchError((error: HttpErrorResponse) => {
return throwError(() => this.buildErrorDto(error));
})
);
}

update(dto: DTO): Observable<DTO> {
return this.http.put<DTO>(this.domain + this.path, dto, {headers: super.getHeaders()})
.pipe(
catchError((error: HttpErrorResponse) => {
return throwError(() => this.buildErrorDto(error));
})
);
}

delete(id: string): Observable<any> {
Expand All @@ -61,6 +86,10 @@ export abstract class CrudHttpClient<DTO extends ApiDTO> extends FunixprodHttpCl
return this.http.delete(this.domain + this.path, {
params: httpParams,
headers: super.getHeaders()
})
}).pipe(
catchError((error: HttpErrorResponse) => {
return throwError(() => this.buildErrorDto(error));
})
)
}
}
Original file line number Diff line number Diff line change
@@ -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 {

Expand Down Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
*/
export abstract class ApiDTO {
public id?: string;

public createdAt?: Date;

public updatedAt?: Date;

}
Original file line number Diff line number Diff line change
@@ -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;
}
}
1 change: 1 addition & 0 deletions projects/funixproductions-requests/src/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 5ccf7cf

Please sign in to comment.