@pistonite/pure 0.26.0 → 0.26.1
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/result/index.ts +27 -8
package/package.json
CHANGED
package/src/result/index.ts
CHANGED
|
@@ -197,19 +197,38 @@ export async function tryAsync<T, E = unknown>(
|
|
|
197
197
|
}
|
|
198
198
|
|
|
199
199
|
/** Try best effort converting an error to a string */
|
|
200
|
-
export function errstr(e: unknown): string {
|
|
200
|
+
export function errstr(e: unknown, recursing?: boolean): string {
|
|
201
201
|
if (typeof e === "string") {
|
|
202
202
|
return e;
|
|
203
203
|
}
|
|
204
|
-
if (e) {
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
204
|
+
if (!e) {
|
|
205
|
+
return `${e}`;
|
|
206
|
+
}
|
|
207
|
+
if (typeof e === "object" && "message" in e) {
|
|
208
|
+
if (!recursing) {
|
|
209
|
+
return errstr(e.message, true);
|
|
210
|
+
}
|
|
211
|
+
return `${e.message}`;
|
|
212
|
+
}
|
|
213
|
+
if (typeof e === "object" && "toString" in e) {
|
|
214
|
+
const s = e.toString();
|
|
215
|
+
if (!recursing) {
|
|
216
|
+
return errstr(s, true);
|
|
209
217
|
}
|
|
210
|
-
|
|
211
|
-
|
|
218
|
+
return `${s}`;
|
|
219
|
+
}
|
|
220
|
+
// try less-likely fields
|
|
221
|
+
if (typeof e === "object" && "msg" in e) {
|
|
222
|
+
if (!recursing) {
|
|
223
|
+
return errstr(e.msg, true);
|
|
224
|
+
}
|
|
225
|
+
return `${e.msg}`;
|
|
226
|
+
}
|
|
227
|
+
if (typeof e === "object" && "code" in e) {
|
|
228
|
+
if (!recursing) {
|
|
229
|
+
return `error code: ${errstr(e.code, true)}`;
|
|
212
230
|
}
|
|
231
|
+
return `${e.code}`;
|
|
213
232
|
}
|
|
214
233
|
return `${e}`;
|
|
215
234
|
}
|