mirror of
https://github.com/rustfs/console.git
synced 2026-08-30 17:14:47 +08:00
6f1193f6e4
* chore: replace codebase with Next.js version (console-new) - Migrate from Nuxt/Vue to Next.js/React - Same repo, dev-pr branch has shared history with main for PR * chore: wip * ci: pin pnpm to latest in workflows, packageManager 10.19.0 * ci: use pnpm version from package.json only (fix version mismatch) * fix: CI - add root page redirect, fix TS types and lint (api-client, aws4fetch, upload-task, hooks) * fix(ci): add type-check script for TypeScript Check job * fix(ci): resolve Code Formatting job - ESLint errors (setState-in-effect, refs, static-components, preserve-memoization) * fix(ci): add lint:fix script for quality workflow Auto-fix step * fix(ci): add SVG module type declaration for TypeScript Check
95 lines
2.2 KiB
TypeScript
95 lines
2.2 KiB
TypeScript
export interface ApiError {
|
|
message: string
|
|
code?: string
|
|
statusCode?: number
|
|
originalError?: Error
|
|
}
|
|
|
|
export class ConfigLoadError extends Error {
|
|
code: "INVALID_URL" | "STORAGE_ERROR" | "NETWORK_ERROR" | "UNKNOWN_ERROR"
|
|
originalError?: Error
|
|
|
|
constructor(
|
|
message: string,
|
|
code: ConfigLoadError["code"] = "UNKNOWN_ERROR",
|
|
originalError?: Error
|
|
) {
|
|
super(message)
|
|
this.name = "ConfigLoadError"
|
|
this.code = code
|
|
this.originalError = originalError
|
|
}
|
|
}
|
|
|
|
export const parseApiError = async (response: Response): Promise<string> => {
|
|
try {
|
|
const errorData = await response.clone().json()
|
|
return (
|
|
(errorData as { message?: string }).message ||
|
|
JSON.stringify(errorData) ||
|
|
response.statusText
|
|
)
|
|
} catch {
|
|
try {
|
|
const text = await response.clone().text()
|
|
if (text) {
|
|
if (text.trim().startsWith("<")) {
|
|
const match =
|
|
text.match(/<Message>(.*?)<\/Message>/i) ||
|
|
text.match(/<Error>(.*?)<\/Error>/i)
|
|
return match?.[1] ?? text
|
|
}
|
|
return text
|
|
}
|
|
} catch {
|
|
// keep statusText
|
|
}
|
|
}
|
|
return response.statusText
|
|
}
|
|
|
|
export const handleConfigError = (
|
|
error: unknown,
|
|
context: string
|
|
): ConfigLoadError => {
|
|
if (error instanceof ConfigLoadError) {
|
|
return error
|
|
}
|
|
|
|
if (error instanceof Error) {
|
|
if (error.message.includes("Invalid URL")) {
|
|
return new ConfigLoadError(
|
|
`Invalid URL in ${context}: ${error.message}`,
|
|
"INVALID_URL",
|
|
error
|
|
)
|
|
}
|
|
if (
|
|
error.message.includes("localStorage") ||
|
|
error.message.includes("storage")
|
|
) {
|
|
return new ConfigLoadError(
|
|
`Storage error in ${context}: ${error.message}`,
|
|
"STORAGE_ERROR",
|
|
error
|
|
)
|
|
}
|
|
if (
|
|
error.message.includes("fetch") ||
|
|
error.message.includes("network")
|
|
) {
|
|
return new ConfigLoadError(
|
|
`Network error in ${context}: ${error.message}`,
|
|
"NETWORK_ERROR",
|
|
error
|
|
)
|
|
}
|
|
}
|
|
|
|
return new ConfigLoadError(
|
|
`Unknown error in ${context}: ${error}`,
|
|
"UNKNOWN_ERROR",
|
|
error instanceof Error ? error : new Error(String(error))
|
|
)
|
|
}
|