mirror of
https://github.com/lordmathis/llamactl.git
synced 2025-12-24 10:04:26 +00:00
Refactor form components and improve API error handling
This commit is contained in:
32
webui/src/lib/errorUtils.ts
Normal file
32
webui/src/lib/errorUtils.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* Parses error response from API calls and returns a formatted error message
|
||||
*/
|
||||
export async function parseErrorResponse(response: Response): Promise<string> {
|
||||
let errorMessage = `HTTP ${response.status}`
|
||||
|
||||
try {
|
||||
const errorText = await response.text()
|
||||
if (errorText) {
|
||||
errorMessage += `: ${errorText}`
|
||||
}
|
||||
} catch {
|
||||
// If we can't read the error, just use status
|
||||
}
|
||||
|
||||
return errorMessage
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles common API call errors and throws appropriate Error objects
|
||||
*/
|
||||
export async function handleApiError(response: Response): Promise<void> {
|
||||
// Handle authentication errors
|
||||
if (response.status === 401) {
|
||||
throw new Error('Authentication required')
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
const errorMessage = await parseErrorResponse(response)
|
||||
throw new Error(errorMessage)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user