refactor: use RFC 7807 standard for error handling
All checks were successful
Build and Deploy Angular App / build (push) Successful in 1m30s

This commit is contained in:
Polianin Nikita 2024-12-22 07:17:21 +03:00
parent 10bf53adec
commit eda6ca4b1a

View File

@ -158,7 +158,7 @@ export default abstract class ApiService {
private handleError(error: HttpErrorResponse): void { private handleError(error: HttpErrorResponse): void {
// todo: change to Retry-After condition // todo: change to Retry-After condition
if (error.error && error.error.toString().includes("setup")) { if (error.error && error.error.detail.includes("setup")) {
this.router.navigate(['/setup/']).then(); this.router.navigate(['/setup/']).then();
return; return;
} }
@ -167,6 +167,10 @@ export default abstract class ApiService {
let message: string | undefined = undefined; let message: string | undefined = undefined;
if (error.error instanceof ErrorEvent) { if (error.error instanceof ErrorEvent) {
title = `Произошла ошибка: ${error.error.message}`; title = `Произошла ошибка: ${error.error.message}`;
} else {
if (error.error && error.error.type && error.error.title) {
title = error.error.title || `Ошибка с кодом ${error.status}`;
message = error.error.detail || 'Неизвестная ошибка';
} else { } else {
switch (error.status) { switch (error.status) {
case 0: case 0:
@ -195,11 +199,9 @@ export default abstract class ApiService {
title = `Сервер вернул код ошибки: ${error.status}`; title = `Сервер вернул код ошибки: ${error.status}`;
break; break;
} }
if (error.error?.Error) }
message = error.error.Error;
else if (error.error instanceof String) if (!message)
message = error.error.toString();
else
message = error.error.statusMessage; message = error.error.statusMessage;
} }
this.notify.error(message == '' ? undefined : message, title); this.notify.error(message == '' ? undefined : message, title);