@orsetra/shared-ui 1.0.38 → 1.0.39
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/components/ui/alert-banner.tsx +4 -4
- package/index.ts +2 -1
- package/lib/error-utils.ts +127 -0
- package/package.json +1 -1
|
@@ -10,10 +10,10 @@ const alertBannerVariants = cva(
|
|
|
10
10
|
{
|
|
11
11
|
variants: {
|
|
12
12
|
variant: {
|
|
13
|
-
success: "bg-green-50 text-green-800
|
|
14
|
-
error: "bg-red-50 text-red-800
|
|
15
|
-
warning: "bg-amber-50 text-amber-800
|
|
16
|
-
info: "bg-blue-50 text-blue-800
|
|
13
|
+
success: "bg-green-50 text-green-800",
|
|
14
|
+
error: "bg-red-50 text-red-800",
|
|
15
|
+
warning: "bg-amber-50 text-amber-800",
|
|
16
|
+
info: "bg-blue-50 text-blue-800",
|
|
17
17
|
},
|
|
18
18
|
},
|
|
19
19
|
defaultVariants: {
|
package/index.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
// Utilities
|
|
2
2
|
export * from './lib/utils'
|
|
3
3
|
export * from './lib/menu-utils'
|
|
4
|
+
export * from './lib/error-utils'
|
|
4
5
|
export { BaseService } from './lib/base-service'
|
|
5
|
-
export { default as HttpClient, useHttpClient } from './lib/http-client'
|
|
6
|
+
export { default as HttpClient, useHttpClient, ApiError } from './lib/http-client'
|
|
6
7
|
|
|
7
8
|
// UI Components
|
|
8
9
|
export * from './components/ui'
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility functions for parsing API errors
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { ApiError } from './http-client'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Standard API error response format
|
|
9
|
+
*/
|
|
10
|
+
export interface ApiErrorResponse {
|
|
11
|
+
BusinessCode?: number
|
|
12
|
+
Message?: string
|
|
13
|
+
message?: string
|
|
14
|
+
error?: string
|
|
15
|
+
code?: number
|
|
16
|
+
details?: unknown
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Extracts a user-friendly error message from various error formats
|
|
21
|
+
*
|
|
22
|
+
* Supports:
|
|
23
|
+
* - ApiError from http-client
|
|
24
|
+
* - Standard Error objects
|
|
25
|
+
* - API responses with BusinessCode/Message format
|
|
26
|
+
* - API responses with message/error format
|
|
27
|
+
*
|
|
28
|
+
* @param error - The error object to parse
|
|
29
|
+
* @param fallbackMessage - Default message if no error message can be extracted
|
|
30
|
+
* @returns A user-friendly error message
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```typescript
|
|
34
|
+
* try {
|
|
35
|
+
* await apiCall()
|
|
36
|
+
* } catch (error) {
|
|
37
|
+
* const message = parseApiError(error)
|
|
38
|
+
* showError(message)
|
|
39
|
+
* }
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export function parseApiError(error: unknown, fallbackMessage = "An unexpected error occurred"): string {
|
|
43
|
+
// Handle ApiError from http-client
|
|
44
|
+
if (error instanceof ApiError) {
|
|
45
|
+
// Check raw response for BusinessCode/Message format
|
|
46
|
+
if (error.raw && typeof error.raw === 'object') {
|
|
47
|
+
const rawError = error.raw as ApiErrorResponse
|
|
48
|
+
if (rawError.Message) {
|
|
49
|
+
return rawError.Message
|
|
50
|
+
}
|
|
51
|
+
if (rawError.message) {
|
|
52
|
+
return rawError.message
|
|
53
|
+
}
|
|
54
|
+
if (rawError.error) {
|
|
55
|
+
return rawError.error
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
// Use ApiError message
|
|
59
|
+
return error.message
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Handle standard Error objects
|
|
63
|
+
if (error instanceof Error) {
|
|
64
|
+
return error.message
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Handle plain objects with error properties
|
|
68
|
+
if (error && typeof error === 'object') {
|
|
69
|
+
const errorObj = error as ApiErrorResponse
|
|
70
|
+
|
|
71
|
+
// Check for BusinessCode/Message format (custom API format)
|
|
72
|
+
if (errorObj.Message) {
|
|
73
|
+
return errorObj.Message
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Check for standard message/error properties
|
|
77
|
+
if (errorObj.message) {
|
|
78
|
+
return errorObj.message
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (errorObj.error) {
|
|
82
|
+
return errorObj.error
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Handle string errors
|
|
87
|
+
if (typeof error === 'string') {
|
|
88
|
+
return error
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Fallback
|
|
92
|
+
return fallbackMessage
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Checks if an error is an ApiError with a specific status code
|
|
97
|
+
*
|
|
98
|
+
* @param error - The error to check
|
|
99
|
+
* @param status - The status code to match
|
|
100
|
+
* @returns true if the error is an ApiError with the specified status
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```typescript
|
|
104
|
+
* if (isApiErrorWithStatus(error, 404)) {
|
|
105
|
+
* showError("Resource not found")
|
|
106
|
+
* }
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
export function isApiErrorWithStatus(error: unknown, status: number): boolean {
|
|
110
|
+
return error instanceof ApiError && error.status === status
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Checks if an error is a network error (no response from server)
|
|
115
|
+
*
|
|
116
|
+
* @param error - The error to check
|
|
117
|
+
* @returns true if the error is a network error
|
|
118
|
+
*/
|
|
119
|
+
export function isNetworkError(error: unknown): boolean {
|
|
120
|
+
if (error instanceof Error) {
|
|
121
|
+
const message = error.message.toLowerCase()
|
|
122
|
+
return message.includes('network') ||
|
|
123
|
+
message.includes('fetch') ||
|
|
124
|
+
message.includes('connection')
|
|
125
|
+
}
|
|
126
|
+
return false
|
|
127
|
+
}
|