@@ -19,8 +19,12 @@ import type {
1919 * message: `Failed to connect: ${cause}`,
2020 * cause,
2121 * }),
22+ * Response: ({ status }: { status: number; bodyMessage?: string }) => ({
23+ * message: `HTTP ${status}`,
24+ * status,
25+ * }),
2226 * Parse: ({ cause }: { cause: string }) => ({
23- * message: `Failed to parse: ${cause}`,
27+ * message: `Failed to parse response body : ${cause}`,
2428 * cause,
2529 * }),
2630 * });
@@ -29,6 +33,30 @@ import type {
2933 *
3034 * const result = HttpError.Connection({ cause: 'timeout' }); // Err<...>
3135 * ```
36+ *
37+ * Inspired by Rust's {@link https://docs.rs/thiserror | thiserror} crate. The
38+ * mapping is nearly 1:1:
39+ *
40+ * - `enum HttpError` → `const HttpError = defineErrors(...)`
41+ * - Variant `Connection { cause: String }` → key `Connection: ({ cause }) => (...)`
42+ * - `#[error("Failed: {cause}")]` → `` message: `Failed: ${cause}` ``
43+ * - `HttpError::Connection { ... }` → `HttpError.Connection({ ... })`
44+ * - `match error { Connection { .. } => }` → `switch (error.name) { case 'Connection': }`
45+ *
46+ * The equivalent Rust `thiserror` enum:
47+ * ```rust
48+ * #[derive(Error, Debug)]
49+ * enum HttpError {
50+ * #[error("Failed to connect: {cause}")]
51+ * Connection { cause: String },
52+ *
53+ * #[error("HTTP {status}")]
54+ * Response { status: u16, body_message: Option<String> },
55+ *
56+ * #[error("Failed to parse response body: {cause}")]
57+ * Parse { cause: String },
58+ * }
59+ * ```
3260 */
3361export function defineErrors < const TConfig extends ErrorsConfig > (
3462 config : TConfig & ValidatedConfig < TConfig > ,
0 commit comments