@pradip1995/commerce-core 1.1.5 → 1.1.6
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/package.json +1 -1
- package/src/util/medusa-error.ts +46 -11
package/package.json
CHANGED
package/src/util/medusa-error.ts
CHANGED
|
@@ -1,18 +1,53 @@
|
|
|
1
|
+
type FetchLikeError = {
|
|
2
|
+
message: string
|
|
3
|
+
status?: number
|
|
4
|
+
statusText?: string
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function isFetchLikeError(error: unknown): error is FetchLikeError {
|
|
8
|
+
return (
|
|
9
|
+
typeof error === "object" &&
|
|
10
|
+
error !== null &&
|
|
11
|
+
"message" in error &&
|
|
12
|
+
typeof (error as FetchLikeError).message === "string" &&
|
|
13
|
+
"status" in error &&
|
|
14
|
+
typeof (error as FetchLikeError).status === "number"
|
|
15
|
+
)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function formatErrorMessage(message: unknown): string {
|
|
19
|
+
if (typeof message === "string") {
|
|
20
|
+
const normalized = message.trim()
|
|
21
|
+
return normalized.charAt(0).toUpperCase() + normalized.slice(1) + "."
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (message && typeof message === "object") {
|
|
25
|
+
return JSON.stringify(message)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return "Request failed."
|
|
29
|
+
}
|
|
30
|
+
|
|
1
31
|
export default function medusaError(error: any): never {
|
|
2
32
|
if (error.response) {
|
|
3
|
-
//
|
|
4
|
-
// that falls out of the range of 2xx
|
|
5
|
-
const u = new URL(error.config.url, error.config.baseURL)
|
|
6
|
-
|
|
7
|
-
// Extracting the error message from the response data
|
|
33
|
+
// Axios-style errors (legacy)
|
|
8
34
|
const message = error.response.data.message || error.response.data
|
|
9
35
|
|
|
10
|
-
throw new Error(
|
|
11
|
-
}
|
|
12
|
-
|
|
36
|
+
throw new Error(formatErrorMessage(message))
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (isFetchLikeError(error)) {
|
|
40
|
+
throw new Error(formatErrorMessage(error.message))
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (error.request) {
|
|
44
|
+
// Axios-style: request sent but no response
|
|
13
45
|
throw new Error("No response received: " + error.request)
|
|
14
|
-
} else {
|
|
15
|
-
// Something happened in setting up the request that triggered an Error
|
|
16
|
-
throw new Error("Error setting up the request: " + error.message)
|
|
17
46
|
}
|
|
47
|
+
|
|
48
|
+
if (error instanceof Error) {
|
|
49
|
+
throw error
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
throw new Error("Error setting up the request: " + String(error?.message ?? error))
|
|
18
53
|
}
|