Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions apps/server/src/device/DeviceHubOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const HubActionResult = Schema.Struct({
Schema.Array(Schema.Struct({ message: Schema.String, error: Schema.optional(Schema.String) })),
),
});
const HubRejection = Schema.Struct({ error: Schema.String });

// Hub diagnostics can carry a whole stderr dump.
const boundDiagnostics = (text: string) =>
Expand Down Expand Up @@ -98,13 +99,38 @@ export const makeDeviceHubOperations = (options: {
schema: Schema.Codec<A, I>,
operation: string,
timeout: Duration.Input = Duration.seconds(15),
) =>
fetchJson(request, schema).pipe(
Effect.timeout(timeout),
Effect.mapError(
(cause) => new DeviceOperationError({ operation, reason: "request_failed", cause }),
) => {
const requestFailed = (cause: unknown) =>
Effect.fail(new DeviceOperationError({ operation, reason: "request_failed", cause }));
return httpClient.execute(request).pipe(
Effect.flatMap(HttpClientResponse.filterStatusOk),
Effect.flatMap(HttpClientResponse.schemaBodyJson(schema)),
Effect.catchReason("HttpClientError", "StatusCodeError", ({ response }, cause) =>
// Only the hub's `error` string is surfaced; other bodies can be private hub output.
HttpClientResponse.schemaBodyJson(HubRejection)(response).pipe(
Effect.map(({ error }) => errorHead(error)),
Effect.orElseSucceed(() => ""),
Effect.flatMap((detail) =>
Effect.fail(
new DeviceOperationError({
operation,
reason: "hub_rejected",
...(detail ? { diagnostics: boundDiagnostics(detail) } : {}),
cause,
}),
),
),
),
),
Effect.scoped,
Effect.timeout(timeout),
Effect.catchTags({
HttpClientError: requestFailed,
SchemaError: requestFailed,
TimeoutError: requestFailed,
}),
);
};

const postJson = (path: string, body: Record<string, string>, operation: string) =>
HttpClientRequest.post(`${origin}${path}`).pipe(
Expand Down
36 changes: 36 additions & 0 deletions apps/server/src/device/DeviceService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,42 @@ it.effect.each([400, 500])(
}),
);

it.effect("reports the hub's error when it rejects attaching a simulator stream", () =>
Effect.gen(function* () {
const hubError =
"Could not release launch state for phone: /tmp/serve-sim/launch-phone.lock is still locked. Retry cleanup after the active command finishes.";
const operations = makeDeviceHubOperations({
hostId: LOCAL_DEVICE_HOST_ID,
origin: "http://device.test",
http: HttpClient.make((request) =>
Effect.succeed(
HttpClientResponse.fromWeb(
request,
Response.json({ ok: false, error: hubError }, { status: 500 }),
),
),
),
run: () => Effect.die("Attaching does not run a command"),
platformAvailable: () => Effect.succeed(false),
});
const error = yield* operations
.attach({
id: "phone",
name: "Phone",
version: "iOS 26.3",
platform: "ios",
booted: true,
physical: false,
})
.pipe(Effect.flip);
expect({ _tag: error._tag, reason: error.reason, message: error.message }).toEqual({
_tag: "DeviceOperationError",
reason: "hub_rejected",
message: expect.stringContaining("is still locked"),
});
}),
);

const fixture = Effect.fn("fixture")(function* (
onBoot: Effect.Effect<void> = Effect.void,
bootError?: string | { error?: string; errors?: Array<{ message: string; error?: string }> },
Expand Down
4 changes: 3 additions & 1 deletion packages/contracts/src/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,7 @@ export class DeviceOperationError extends Schema.TaggedError<DeviceOperationErro
"hub_rejected",
]),
exitCode: Schema.optional(Schema.Number),
diagnostics: Schema.optional(Schema.String.check(Schema.isMaxLength(2_000))),
cause: Schema.Defect(),
},
) {
Expand All @@ -601,7 +602,8 @@ export class DeviceOperationError extends Schema.TaggedError<DeviceOperationErro
settings_failed: "Could not read or save device settings.",
hub_rejected: "The device hub could not complete the request.",
}[this.reason];
return `Device ${this.operation} failed: ${explanation}`;
const summary = `Device ${this.operation} failed: ${explanation}`;
return this.diagnostics ? `${summary}\n\n${this.diagnostics}` : summary;
}
}

Expand Down
Loading