@twinedo/app-error 1.0.1 → 1.0.2
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/README.md +37 -53
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @twinedo/app-error
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A framework-agnostic JavaScript/TypeScript library to normalize fetch, axios, and runtime errors into a predictable AppError model. Open to contributions.
|
|
4
4
|
|
|
5
5
|
## Why this exists
|
|
6
6
|
|
|
@@ -105,69 +105,53 @@ try {
|
|
|
105
105
|
|
|
106
106
|
```ts
|
|
107
107
|
import axios from "axios";
|
|
108
|
-
import {
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
const readHeader = (headers: unknown, name: string): string | undefined => {
|
|
117
|
-
if (!headers) return undefined;
|
|
118
|
-
const getter = (headers as { get?: (key: string) => string | null | undefined })
|
|
119
|
-
.get;
|
|
120
|
-
if (typeof getter === "function") {
|
|
121
|
-
return getter.call(headers, name) ?? undefined;
|
|
122
|
-
}
|
|
123
|
-
return readString((headers as Record<string, unknown>)[name]);
|
|
124
|
-
};
|
|
125
|
-
|
|
126
|
-
// Project A (Tony backend): { error: { message, code } }, header x-request-id
|
|
127
|
-
const projectAPolicy = defineErrorPolicy({
|
|
108
|
+
import {
|
|
109
|
+
defineErrorPolicy,
|
|
110
|
+
toAppError,
|
|
111
|
+
fromFetchResponse,
|
|
112
|
+
} from "@twinedo/app-error";
|
|
113
|
+
|
|
114
|
+
// Tony backend: { error: { message, code } }, x-request-id
|
|
115
|
+
const policyTony = defineErrorPolicy({
|
|
128
116
|
http: {
|
|
129
|
-
message: (data) =>
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
: undefined,
|
|
133
|
-
code: (data) =>
|
|
134
|
-
isRecord(data) && isRecord(data.error)
|
|
135
|
-
? readString(data.error.code)
|
|
136
|
-
: undefined,
|
|
137
|
-
requestId: (headers) => readHeader(headers, "x-request-id"),
|
|
117
|
+
message: (data) => (data as any)?.error?.message,
|
|
118
|
+
code: (data) => (data as any)?.error?.code,
|
|
119
|
+
requestId: (headers) => (headers as any)?.["x-request-id"],
|
|
138
120
|
},
|
|
139
121
|
});
|
|
140
122
|
|
|
141
|
-
//
|
|
142
|
-
const
|
|
123
|
+
// Bobby backend: { message | msg, code }, x-correlation-id
|
|
124
|
+
const policyBobby = defineErrorPolicy({
|
|
143
125
|
http: {
|
|
144
|
-
message: (data) =>
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
: undefined,
|
|
148
|
-
code: (data) => (isRecord(data) ? readString(data.code) : undefined),
|
|
149
|
-
requestId: (headers) => readHeader(headers, "x-correlation-id"),
|
|
126
|
+
message: (data) => (data as any)?.message ?? (data as any)?.msg,
|
|
127
|
+
code: (data) => (data as any)?.code,
|
|
128
|
+
requestId: (headers) => (headers as any)?.["x-correlation-id"],
|
|
150
129
|
},
|
|
151
130
|
});
|
|
152
131
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
console.error(appError.message, appError.code, appError.requestId);
|
|
159
|
-
};
|
|
132
|
+
// One handler for UI/logs
|
|
133
|
+
function handleError(err: unknown, policy = policyTony) {
|
|
134
|
+
const e = toAppError(err, policy);
|
|
135
|
+
console.error(e.message, e.code, e.requestId);
|
|
136
|
+
}
|
|
160
137
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
138
|
+
// Request using axios (Tony backend)
|
|
139
|
+
async function loadUserAxios() {
|
|
140
|
+
try {
|
|
141
|
+
await axios.get("/api/user"); // Tony API
|
|
142
|
+
} catch (err) {
|
|
143
|
+
handleError(err, policyTony);
|
|
144
|
+
}
|
|
165
145
|
}
|
|
166
146
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
147
|
+
// Request using fetch (Bobby backend)
|
|
148
|
+
async function loadUserFetch() {
|
|
149
|
+
try {
|
|
150
|
+
const res = await fetch("/api/user"); // Bobby API
|
|
151
|
+
if (!res.ok) throw await fromFetchResponse(res, policyBobby);
|
|
152
|
+
} catch (err) {
|
|
153
|
+
handleError(err, policyBobby);
|
|
154
|
+
}
|
|
171
155
|
}
|
|
172
156
|
```
|
|
173
157
|
|