Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

previewAutomationErrors.ts

169 lines5.8 KB

1import {
2 EnvironmentId,
3 type PreviewAutomationOwner,
4 PreviewAutomationOperation,
5 type PreviewAutomationRequest,
6 type PreviewAutomationResponse,
7 PreviewTabId,
8 ThreadId,
9 TrimmedNonEmptyString,
10} from "@t3tools/contracts";
11import * as Schema from "effect/Schema";
12​
13export interface PreviewAutomationOperationContext {
14 readonly requestId: PreviewAutomationRequest["requestId"];
15 readonly operation: PreviewAutomationRequest["operation"];
16 readonly environmentId: PreviewAutomationOwner["environmentId"];
17 readonly threadId: PreviewAutomationRequest["threadId"];
18 readonly tabId: Exclude<PreviewAutomationRequest["tabId"], undefined> | null;
20​
21export class PreviewAutomationOverlayTimeoutError extends Schema.TaggedErrorClass<PreviewAutomationOverlayTimeoutError>()(
22 "PreviewAutomationOverlayTimeoutError",
23 {
24 requestId: TrimmedNonEmptyString,
25 environmentId: EnvironmentId,
26 threadId: ThreadId,
27 timeoutMs: Schema.Int,
28 },
29) {
30 get responseTag() {
31 return "PreviewAutomationTimeoutError" as const;
32 }
33​
34 override get message(): string {
35 return `Preview webview for request ${this.requestId} on environment ${this.environmentId} thread ${this.threadId} did not register within ${this.timeoutMs}ms.`;
36 }
38​
39export class PreviewAutomationNavigationTimeoutError extends Schema.TaggedErrorClass<PreviewAutomationNavigationTimeoutError>()(
40 "PreviewAutomationNavigationTimeoutError",
41 {
42 requestId: TrimmedNonEmptyString,
43 environmentId: EnvironmentId,
44 threadId: ThreadId,
45 tabId: PreviewTabId,
46 readiness: Schema.Literals(["domContentLoaded", "load"]),
47 timeoutMs: Schema.Int,
48 },
49) {
50 get responseTag() {
51 return "PreviewAutomationTimeoutError" as const;
52 }
53​
54 override get message(): string {
55 return `Preview navigation for request ${this.requestId} on environment ${this.environmentId} thread ${this.threadId} tab ${this.tabId} did not reach ${this.readiness} readiness within ${this.timeoutMs}ms.`;
56 }
58​
59export class PreviewAutomationStaleOwnerError extends Schema.TaggedErrorClass<PreviewAutomationStaleOwnerError>()(
60 "PreviewAutomationStaleOwnerError",
61 {
62 requestId: TrimmedNonEmptyString,
63 environmentId: EnvironmentId,
64 expectedThreadId: ThreadId,
65 requestedThreadId: ThreadId,
66 },
67) {
68 get responseTag() {
69 return "PreviewAutomationUnavailableError" as const;
70 }
71​
72 override get message(): string {
73 return `Preview automation request ${this.requestId} targeted thread ${this.requestedThreadId}, but the owner for environment ${this.environmentId} is attached to thread ${this.expectedThreadId}.`;
74 }
76​
77export class PreviewAutomationTargetUnavailableError extends Schema.TaggedErrorClass<PreviewAutomationTargetUnavailableError>()(
78 "PreviewAutomationTargetUnavailableError",
79 {
80 requestId: TrimmedNonEmptyString,
81 operation: PreviewAutomationOperation,
82 environmentId: EnvironmentId,
83 threadId: ThreadId,
84 tabId: Schema.NullOr(PreviewTabId),
85 bridgeAvailable: Schema.Boolean,
86 },
87) {
88 get responseTag() {
89 return "PreviewAutomationTabNotFoundError" as const;
90 }
91​
92 override get message(): string {
93 return `Preview automation target for ${this.operation} request ${this.requestId} is unavailable on environment ${this.environmentId} thread ${this.threadId} (tab ${this.tabId ?? "unassigned"}, bridge ${this.bridgeAvailable ? "available" : "unavailable"}).`;
94 }
96​
97export class PreviewAutomationRecordingNotActiveError extends Schema.TaggedErrorClass<PreviewAutomationRecordingNotActiveError>()(
98 "PreviewAutomationRecordingNotActiveError",
99 {
100 requestId: TrimmedNonEmptyString,
101 environmentId: EnvironmentId,
102 threadId: ThreadId,
103 tabId: PreviewTabId,
104 },
105) {
106 get responseTag() {
107 return "PreviewAutomationExecutionError" as const;
108 }
109​
110 override get message(): string {
111 return `Preview automation request ${this.requestId} found no active recording for tab ${this.tabId} on environment ${this.environmentId} thread ${this.threadId}.`;
112 }
114​
115export class PreviewAutomationOperationError extends Schema.TaggedErrorClass<PreviewAutomationOperationError>()(
116 "PreviewAutomationOperationError",
117 {
118 requestId: TrimmedNonEmptyString,
119 operation: PreviewAutomationOperation,
120 environmentId: EnvironmentId,
121 threadId: ThreadId,
122 tabId: Schema.NullOr(PreviewTabId),
123 cause: Schema.Defect(),
124 },
125) {
126 static fromCause(
127 input: PreviewAutomationOperationContext & { readonly cause: unknown },
128 ): PreviewAutomationOwnerError {
129 return isPreviewAutomationOwnerError(input.cause)
130 ? input.cause
131 : new PreviewAutomationOperationError(input);
132 }
133​
134 get responseTag() {
135 return "PreviewAutomationExecutionError" as const;
136 }
137​
138 override get message(): string {
139 return `Preview automation ${this.operation} request ${this.requestId} failed on environment ${this.environmentId} thread ${this.threadId} (tab ${this.tabId ?? "unassigned"}).`;
140 }
142​
143export const PreviewAutomationOwnerError = Schema.Union([
144 PreviewAutomationOverlayTimeoutError,
145 PreviewAutomationNavigationTimeoutError,
146 PreviewAutomationStaleOwnerError,
147 PreviewAutomationTargetUnavailableError,
148 PreviewAutomationRecordingNotActiveError,
149 PreviewAutomationOperationError,
150]);
151export type PreviewAutomationOwnerError = typeof PreviewAutomationOwnerError.Type;
152​
153export const isPreviewAutomationOwnerError = Schema.is(PreviewAutomationOwnerError);
154​
155export function serializePreviewAutomationOwnerError(
156 error: PreviewAutomationOwnerError,
157): NonNullable<PreviewAutomationResponse["error"]> {
158 const detail = Object.fromEntries(
159 Object.entries(error).filter(
160 ([key]) =>
161 key !== "_tag" && key !== "cause" && key !== "name" && key !== "message" && key !== "stack",
162 ),
163 );
164 return {
165 _tag: error.responseTag,
166 message: error.message,
167 ...(Object.keys(detail).length === 0 ? {} : { detail }),
168 };