refactor: simplify handling axios errors

This commit is contained in:
cv
2023-09-17 20:49:02 +02:00
parent 29fd145bd9
commit 99cd9bf0b7
+7 -4
View File
@@ -5,15 +5,18 @@ import { generateId, millisToString } from 'ontime-utils';
import { addLog } from '../stores/logger';
import { nowInMillis } from '../utils/time';
export function logAxiosError(prepend: string, error: unknown) {
let message;
export function maybeAxiosError(error: unknown) {
if (axios.isAxiosError(error)) {
const statusText = (error as AxiosError).response?.statusText ?? '';
const data = (error as AxiosError).response?.data ?? '';
message = `${prepend} ${statusText}: ${data}`;
return `${statusText}: ${data}`;
} else {
message = `${prepend}: ${error}`;
return error as string;
}
}
export function logAxiosError(prepend: string, error: unknown) {
const message = `${prepend}: ${maybeAxiosError(error)}`;
addLog({
id: generateId(),