1import * as Predicate from "effect/Predicate";
2import * as Schema from "effect/Schema";
3
4/**
5 * `ConfigError` indicates a problem with the user's worker config or script.
6 * The user can typically fix the problem by changing their input.
7 *
8 * Examples: an unsupported binding type, a missing hyperdrive origin, a
9 * syntax error in the user's script.
10 */
11export class ConfigError extends Schema.TaggedError<ConfigError>()(
12 "ConfigError",
13 {
14 subtag: Schema.String,
15 message: Schema.String,
16 hint: Schema.optional(Schema.String),
17 detail: Schema.optional(Schema.Unknown),
18 cause: Schema.optional(Schema.Defect()),
19 },
20) {}
21
22/**
23 * `SystemError` indicates a problem with the user's environment, the local
24 * machine, or a process this package depends on (workerd, cloudflared,
25 * filesystem, network sockets). The user may be able to fix the problem,
26 * but it is not a bug in their worker.
27 */
28export class SystemError extends Schema.TaggedError<SystemError>()(
29 "SystemError",
30 {
31 subtag: Schema.String,
32 message: Schema.String,
33 hint: Schema.optional(Schema.String),
34 detail: Schema.optional(Schema.Unknown),
35 cause: Schema.optional(Schema.Defect()),
36 },
37) {}
38
39/**
40 * `ApiError` indicates a problem returned from the Cloudflare API or its
41 * SDK (e.g. preview-session creation failed, script preview upload was
42 * rejected, the SDK considered the request invalid). Often transient or an
43 * authentication / permissions issue; can also be a bug in this package's
44 * usage of the API.
45 */
46export class ApiError extends Schema.TaggedError<ApiError>()("ApiError", {
47 subtag: Schema.String,
48 message: Schema.String,
49 hint: Schema.optional(Schema.String),
50 detail: Schema.optional(Schema.Unknown),
51 cause: Schema.optional(Schema.Defect()),
52}) {}
53
54/**
55 * Top-level union of every error users can observe through `Server.serve()`
56 * or while accessing remote bindings at runtime.
57 *
58 * Defects (raised via `Effect.die`) are intentionally not part of this
59 * union: they indicate a bug inside this package, not something the user
60 * is expected to handle.
61 */
62export const RuntimeError = Schema.Union([ConfigError, SystemError, ApiError]);
63export type RuntimeError = ConfigError | SystemError | ApiError;
64
65export const isRuntimeError = (error: unknown): error is RuntimeError =>
66 Predicate.isTagged(error, "ConfigError") ||
67 Predicate.isTagged(error, "SystemError") ||
68 Predicate.isTagged(error, "ApiError");