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 { addLog } from '../stores/logger';
import { nowInMillis } from '../utils/time'; import { nowInMillis } from '../utils/time';
export function logAxiosError(prepend: string, error: unknown) { export function maybeAxiosError(error: unknown) {
let message;
if (axios.isAxiosError(error)) { if (axios.isAxiosError(error)) {
const statusText = (error as AxiosError).response?.statusText ?? ''; const statusText = (error as AxiosError).response?.statusText ?? '';
const data = (error as AxiosError).response?.data ?? ''; const data = (error as AxiosError).response?.data ?? '';
message = `${prepend} ${statusText}: ${data}`; return `${statusText}: ${data}`;
} else { } else {
message = `${prepend}: ${error}`; return error as string;
} }
}
export function logAxiosError(prepend: string, error: unknown) {
const message = `${prepend}: ${maybeAxiosError(error)}`;
addLog({ addLog({
id: generateId(), id: generateId(),