Add metadata Pinia store for ExifTool queue preview reads.
Introduce preview entry types, resolveErrorMessage, and nullcart-style resolveAxiosErrorMessage for upcoming upload API errors.
This commit is contained in:
@@ -1,23 +1,40 @@
|
||||
import axios from 'axios';
|
||||
import { isAxiosError } from 'axios';
|
||||
import { resolveErrorMessage } from '@/utils/resolveErrorMessage';
|
||||
|
||||
export function resolveAxiosErrorMessage(error: unknown, fallback = 'Request failed'): string {
|
||||
if (axios.isAxiosError(error)) {
|
||||
const data = error.response?.data;
|
||||
if (typeof data === 'object' && data !== null && 'message' in data) {
|
||||
const message = (data as { message: unknown }).message;
|
||||
if (typeof message === 'string') {
|
||||
return message;
|
||||
}
|
||||
if (Array.isArray(message)) {
|
||||
return message.map(String).join(', ');
|
||||
}
|
||||
const getAxiosErrorResponseMessage = (data: unknown): string | null => {
|
||||
if (typeof data === 'string') {
|
||||
return data;
|
||||
}
|
||||
|
||||
if (typeof data !== 'object' || data === null || !('message' in data)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const { message } = data;
|
||||
|
||||
if (typeof message === 'string') {
|
||||
return message;
|
||||
}
|
||||
|
||||
if (Array.isArray(message)) {
|
||||
const texts = message.filter((item): item is string => typeof item === 'string');
|
||||
|
||||
if (texts.length === 1) {
|
||||
return texts[0];
|
||||
}
|
||||
if (error.message) {
|
||||
return error.message;
|
||||
|
||||
if (texts.length > 1) {
|
||||
return texts.join(', ');
|
||||
}
|
||||
}
|
||||
if (error instanceof Error) {
|
||||
return error.message;
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
export const resolveAxiosErrorMessage = (error: unknown, fallback: string): string => {
|
||||
if (!isAxiosError(error) || !error.response) {
|
||||
return resolveErrorMessage(error, fallback);
|
||||
}
|
||||
return fallback;
|
||||
}
|
||||
|
||||
return getAxiosErrorResponseMessage(error.response.data) ?? fallback;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user