Skip to content

Commit 04decd9

Browse files
committed
feat: simplify error handling and remove unused helper functions
1 parent 72eca6b commit 04decd9

File tree

4 files changed

+214
-193
lines changed

4 files changed

+214
-193
lines changed

src/result/error.ts

Lines changed: 39 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,84 @@
1-
/* eslint-disable max-params */
1+
import { getErrorMessage } from "@/common";
2+
import { isError } from "@/remeda";
23

3-
import { stringify } from "@/common";
4-
import { isBigInt, isBoolean, isError, isNumber, isString, isSymbol } from "@/remeda";
5-
6-
const prepare = (message: string | undefined, contexts: string[], error: unknown) => {
7-
let e: unknown;
8-
let emsg: string;
4+
import type { Result } from "./result";
95

6+
const prepare = (result: Result, msg?: string) => {
107
// 1. Determine error message and cause from the provided error
11-
if (isError(error)) {
12-
e = error;
13-
emsg = error.message;
14-
} else if (
15-
isString(error) ||
16-
isNumber(error) ||
17-
isBigInt(error) ||
18-
isBoolean(error) ||
19-
isSymbol(error)
20-
) {
21-
emsg = error.toString();
22-
} else if (error === undefined) {
23-
emsg = "";
24-
} else if (error === null) {
25-
emsg = "null";
26-
} else {
27-
emsg = stringify(error);
28-
}
8+
const cause = result["error"];
9+
const reason = cause === undefined ? "" : getErrorMessage(cause);
2910

3011
// 2. Concat error message to contexts as the innermost context
31-
const ctxs = contexts.reverse().concat(emsg || []);
12+
const contexts = result["contexts"].reverse().concat(reason || []);
3213

33-
let msg = "";
14+
let message = "";
3415
// 3. Use provided message as the main message if available
35-
if (message) {
36-
msg = message;
16+
if (msg) {
17+
message = msg;
3718
} else {
3819
// 4. Otherwise, use the first non-empty context as the main message
39-
while (ctxs.length > 0) {
20+
while (contexts.length > 0) {
4021
// 5. The main message is removed from contexts
41-
msg = ctxs.shift()!;
42-
if (msg) break;
22+
message = contexts.shift()!;
23+
if (message) break;
4324
}
44-
// 6. Them main message may still be empty, that's okay
25+
// 6. The main message may still be empty, that's okay
4526
}
4627

47-
// 7. Format the display string
48-
const ctx = ctxs.map((line, index) => ` ${index}: ${line}`).join("\n");
49-
const display = `
28+
// 7. Format into a string with message and contexts
29+
const ctx = contexts.map((line, index) => ` ${index}: ${line}`).join("\n");
30+
const formatted = `
5031
Message:
51-
${msg || "<empty message>"}
52-
32+
${message || "<empty message>"}
5333
Context:
5434
${ctx.trim() || "<empty context>"}
5535
`.trim();
5636

5737
// 8. Prepare the error constructor options
58-
const options: ErrorOptions | undefined = e ? { cause: e } : undefined;
38+
const options: ErrorOptions | undefined = isError(cause) ? { cause } : undefined;
5939

60-
return { message: msg, contexts: ctxs, error: e, display, options };
40+
return { message, contexts, formatted, options };
6141
};
6242

6343
export class ResultError extends Error {
64-
static isResultError(value: unknown): value is ResultError {
44+
static is(value: unknown): value is ResultError {
6545
return value instanceof ResultError;
6646
}
6747

68-
#message: string;
69-
#contexts: string[];
70-
#display: string;
48+
static fmt(result: Result, message?: string): string {
49+
return prepare(result, message).formatted;
50+
}
51+
52+
#msg: string;
53+
#ctx: string[];
54+
#formatted: string;
7155

72-
constructor(
73-
message: string | undefined,
74-
error: unknown,
75-
contexts: string[],
76-
caller: Function = ResultError,
77-
) {
78-
const prepared = prepare(message, contexts, error);
56+
constructor(result: Result, message?: string, caller: Function = ResultError) {
57+
const prepared = prepare(result, message);
7958

8059
super(
8160
`
82-
83-
${prepared.display}
84-
61+
--------------------
62+
${prepared.formatted}
8563
Stack trace:`,
8664
prepared.options,
8765
);
8866
Error.captureStackTrace(this, caller || this.constructor);
8967

90-
this.#message = prepared.message;
91-
this.#contexts = prepared.contexts;
92-
this.#display = prepared.display;
68+
this.#msg = prepared.message;
69+
this.#ctx = prepared.contexts;
70+
this.#formatted = prepared.formatted;
9371
}
9472

9573
get msg(): string {
96-
return this.#message;
74+
return this.#msg;
9775
}
9876

99-
get ctx(): string[] {
100-
return this.#contexts.slice();
77+
get contexts(): string[] {
78+
return this.#ctx;
10179
}
10280

10381
override toString(): string {
104-
return this.#display;
82+
return this.#formatted;
10583
}
10684
}

src/result/helper.ts

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/result/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
export { ResultError } from "./error";
2-
export { safeTry } from "./helper";
32
export { err, ok, Result } from "./result";
43

54
export type { Err, Ok } from "./result";

0 commit comments

Comments
 (0)