This commit is contained in:
2026-10-07 12:10:12 +03:00
parent e384bc4872
commit e676717ffb
28 changed files with 722 additions and 236 deletions
+49
View File
@@ -0,0 +1,49 @@
import { HttpErrorResponse } from '@angular/common/http';
export function authErrorMessage(error: unknown): string {
if (error instanceof Error && !(error instanceof HttpErrorResponse) && error.message) {
return error.message;
}
if (!(error instanceof HttpErrorResponse)) {
return 'Не удалось выполнить запрос';
}
if (error.status === 0) {
return 'Нет соединения с сервером';
}
if (error.status === 429) {
return 'Слишком много попыток. Попробуйте чуть позже';
}
return readErrorBody(error.error) ?? 'Не удалось выполнить запрос';
}
function readErrorBody(body: unknown): string | null {
const parsed = typeof body === 'string' ? parseJson(body) : body;
if (!parsed || typeof parsed !== 'object') {
return null;
}
const record = parsed as { error_description?: unknown; message?: unknown };
if (typeof record.error_description === 'string' && record.error_description) {
return record.error_description;
}
if (typeof record.message === 'string' && record.message) {
return record.message;
}
return null;
}
function parseJson(value: string): unknown {
try {
return JSON.parse(value) as unknown;
} catch {
return null;
}
}