@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.
Files changed (2) hide show
  1. package/README.md +37 -53
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @twinedo/app-error
2
2
 
3
- Framework-agnostic error normalization for fetch, axios-like clients, and runtime failures.
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 { defineErrorPolicy, toAppError } from "@twinedo/app-error";
109
-
110
- const isRecord = (value: unknown): value is Record<string, unknown> =>
111
- typeof value === "object" && value !== null;
112
-
113
- const readString = (value: unknown): string | undefined =>
114
- typeof value === "string" ? value : undefined;
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
- isRecord(data) && isRecord(data.error)
131
- ? readString(data.error.message)
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
- // Project B (Bobby backend): { message | msg, code }, header x-correlation-id
142
- const projectBPolicy = defineErrorPolicy({
123
+ // Bobby backend: { message | msg, code }, x-correlation-id
124
+ const policyBobby = defineErrorPolicy({
143
125
  http: {
144
- message: (data) =>
145
- isRecord(data)
146
- ? readString(data.message) ?? readString(data.msg)
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
- const handleError = (
154
- err: unknown,
155
- policy: ReturnType<typeof defineErrorPolicy>
156
- ) => {
157
- const appError = toAppError(err, policy);
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
- try {
162
- await axios.get("/api/user");
163
- } catch (err) {
164
- handleError(err, projectAPolicy);
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
- try {
168
- await axios.get("/api/user");
169
- } catch (err) {
170
- handleError(err, projectBPolicy);
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
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twinedo/app-error",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "A configurable error normalization layer for fetch, axios-like, and runtime errors.",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.js",