File size: 497 Bytes
83a59ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
 * Shape of the error object thrown by ofetch/$fetch for failed API requests.
 */
export interface ApiError {
  status?: number
  data?: {
    message?: string
    data?: Record<string, string[]>
  }
}

export function toApiError(error: unknown): ApiError {
  if (error !== null && typeof error === 'object') {
    return error as ApiError
  }
  return {}
}

export function getApiErrorMessage(error: unknown, fallback: string): string {
  return toApiError(error).data?.message ?? fallback
}