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

previewAutomationErrors.ts

235 lines8.0 KB

1import {
2 EnvironmentId,
3 type PreviewAutomationHost,
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: PreviewAutomationHost["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 PreviewAutomationViewportTimeoutError extends Schema.TaggedErrorClass<PreviewAutomationViewportTimeoutError>()(
60 "PreviewAutomationViewportTimeoutError",
61 {
62 requestId: TrimmedNonEmptyString,
63 environmentId: EnvironmentId,
64 threadId: ThreadId,
65 tabId: PreviewTabId,
66 timeoutMs: Schema.Int,
67 },
68) {
69 get responseTag() {
70 return "PreviewAutomationTimeoutError" as const;
71 }
72​
73 override get message(): string {
74 return `Preview viewport for request ${this.requestId} on environment ${this.environmentId} thread ${this.threadId} tab ${this.tabId} was not rendered within ${this.timeoutMs}ms.`;
75 }
77​
78export class PreviewAutomationTargetUnavailableError extends Schema.TaggedErrorClass<PreviewAutomationTargetUnavailableError>()(
79 "PreviewAutomationTargetUnavailableError",
80 {
81 requestId: TrimmedNonEmptyString,
82 operation: PreviewAutomationOperation,
83 environmentId: EnvironmentId,
84 threadId: ThreadId,
85 tabId: Schema.NullOr(PreviewTabId),
86 bridgeAvailable: Schema.Boolean,
87 },
88) {
89 get responseTag() {
90 return "PreviewAutomationTabNotFoundError" as const;
91 }
92​
93 override get message(): string {
94 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"}).`;
95 }
97​
98export class PreviewAutomationRecordingNotActiveError extends Schema.TaggedErrorClass<PreviewAutomationRecordingNotActiveError>()(
99 "PreviewAutomationRecordingNotActiveError",
100 {
101 requestId: TrimmedNonEmptyString,
102 environmentId: EnvironmentId,
103 threadId: ThreadId,
104 tabId: Schema.NullOr(PreviewTabId),
105 },
106) {
107 get responseTag() {
108 return "PreviewAutomationExecutionError" as const;
109 }
110​
111 override get message(): string {
112 return `Preview automation request ${this.requestId} found no active recording for tab ${this.tabId ?? "unassigned"} on environment ${this.environmentId} thread ${this.threadId}.`;
113 }
115​
116export class PreviewAutomationTargetNotEditableHostError extends Schema.TaggedErrorClass<PreviewAutomationTargetNotEditableHostError>()(
117 "PreviewAutomationTargetNotEditableHostError",
118 {
119 requestId: TrimmedNonEmptyString,
120 operation: PreviewAutomationOperation,
121 environmentId: EnvironmentId,
122 threadId: ThreadId,
123 tabId: Schema.NullOr(PreviewTabId),
124 selectorKind: Schema.optional(Schema.Literals(["focused-element", "locator", "selector"])),
125 selectorLength: Schema.optional(Schema.Int.check(Schema.isGreaterThanOrEqualTo(0))),
126 },
127) {
128 get responseTag() {
129 return "PreviewAutomationTargetNotEditableError" as const;
130 }
131​
132 override get message(): string {
133 return `Preview automation ${this.operation} request ${this.requestId} requires an editable target in tab ${this.tabId ?? "unassigned"}.`;
134 }
136​
137const targetNotEditableDiagnostics = (
138 cause: unknown,
139): {
140 readonly selectorKind?: "focused-element" | "locator" | "selector";
141 readonly selectorLength?: number;
142} | null => {
143 if (
144 typeof cause !== "object" ||
145 cause === null ||
146 !("_tag" in cause) ||
147 cause._tag !== "PreviewAutomationTargetNotEditableError"
148 ) {
149 return null;
150 }
151 const selectorKind =
152 "selectorKind" in cause &&
153 (cause.selectorKind === "focused-element" ||
154 cause.selectorKind === "locator" ||
155 cause.selectorKind === "selector")
156 ? cause.selectorKind
157 : undefined;
158 const selectorLength =
159 "selectorLength" in cause &&
160 typeof cause.selectorLength === "number" &&
161 Number.isInteger(cause.selectorLength) &&
162 cause.selectorLength >= 0
163 ? cause.selectorLength
164 : undefined;
165 return {
166 ...(selectorKind === undefined ? {} : { selectorKind }),
167 ...(selectorLength === undefined ? {} : { selectorLength }),
168 };
169};
170​
171export class PreviewAutomationOperationError extends Schema.TaggedErrorClass<PreviewAutomationOperationError>()(
172 "PreviewAutomationOperationError",
173 {
174 requestId: TrimmedNonEmptyString,
175 operation: PreviewAutomationOperation,
176 environmentId: EnvironmentId,
177 threadId: ThreadId,
178 tabId: Schema.NullOr(PreviewTabId),
179 cause: Schema.Defect(),
180 },
181) {
182 static fromCause(
183 input: PreviewAutomationOperationContext & { readonly cause: unknown },
184 ): PreviewAutomationHostError {
185 if (isPreviewAutomationHostError(input.cause)) return input.cause;
186 const diagnostics = targetNotEditableDiagnostics(input.cause);
187 return diagnostics
188 ? new PreviewAutomationTargetNotEditableHostError({
189 requestId: input.requestId,
190 operation: input.operation,
191 environmentId: input.environmentId,
192 threadId: input.threadId,
193 tabId: input.tabId,
194 ...diagnostics,
195 })
196 : new PreviewAutomationOperationError(input);
197 }
198​
199 get responseTag() {
200 return "PreviewAutomationExecutionError" as const;
201 }
202​
203 override get message(): string {
204 return `Preview automation ${this.operation} request ${this.requestId} failed on environment ${this.environmentId} thread ${this.threadId} (tab ${this.tabId ?? "unassigned"}).`;
205 }
207​
208export const PreviewAutomationHostError = Schema.Union([
209 PreviewAutomationOverlayTimeoutError,
210 PreviewAutomationNavigationTimeoutError,
211 PreviewAutomationViewportTimeoutError,
212 PreviewAutomationTargetUnavailableError,
213 PreviewAutomationRecordingNotActiveError,
214 PreviewAutomationTargetNotEditableHostError,
215 PreviewAutomationOperationError,
216]);
217export type PreviewAutomationHostError = typeof PreviewAutomationHostError.Type;
218​
219const isPreviewAutomationHostError = Schema.is(PreviewAutomationHostError);
220​
221export function serializePreviewAutomationHostError(
222 error: PreviewAutomationHostError,
223): NonNullable<PreviewAutomationResponse["error"]> {
224 const detail = Object.fromEntries(
225 Object.entries(error).filter(
226 ([key]) =>
227 key !== "_tag" && key !== "cause" && key !== "name" && key !== "message" && key !== "stack",
228 ),
229 );
230 return {
231 _tag: error.responseTag,
232 message: error.message,
233 ...(Object.keys(detail).length === 0 ? {} : { detail }),
234 };