feat(sdk): typed errors propagate to one edge mapping (StorageError, InternalError, Telemetry)
Showing the changes to apps/cloud/src (4 of 75 files). Show all
Unified · Split
| @@ -6,7 +6,11 @@ · 5 unmodified lines | ||
| 6 | 6 | import { HttpApiBuilder, HttpRouter, HttpServer } from "@effect/platform"; |
| 7 | 7 | import { Layer } from "effect"; |
| 8 | 8 | |
| 9 | −import { CoreExecutorApi } from "@executor/api"; | |
| 9 | +import { | |
| 10 | + CoreExecutorApi, | |
| 11 | + InternalError, | |
| 12 | + observabilityMiddleware, | |
| 13 | +} from "@executor/api"; | |
| 10 | 14 | import { CoreHandlers } from "@executor/api/server"; |
| 11 | 15 | import { OpenApiGroup, OpenApiHandlers } from "@executor/plugin-openapi/api"; |
| 12 | 16 | import { McpGroup, McpHandlers } from "@executor/plugin-mcp/api"; |
| @@ -18,12 +22,16 @@ import { UserStoreService } from "../auth/context"; · 5 unmodified lines | ||
| 18 | 22 | import { WorkOSAuth } from "../auth/workos"; |
| 19 | 23 | import { AutumnService } from "../services/autumn"; |
| 20 | 24 | import { DbService } from "../services/db"; |
| 25 | +import { ErrorCaptureLive } from "../observability"; | |
| 21 | 26 | |
| 22 | 27 | export const ProtectedCloudApi = CoreExecutorApi.add(OpenApiGroup) |
| 23 | 28 | .add(McpGroup) |
| 24 | 29 | .add(GraphqlGroup) |
| 30 | + .addError(InternalError) | |
| 25 | 31 | .middleware(OrgAuth); |
| 26 | 32 | |
| 33 | +const ObservabilityLive = observabilityMiddleware(ProtectedCloudApi); | |
| 34 | + | |
| 27 | 35 | const DbLive = DbService.Live; |
| 28 | 36 | const UserStoreLive = UserStoreService.Live.pipe(Layer.provide(DbLive)); |
| 29 | 37 | |
| @@ -47,6 +55,13 @@ export const ProtectedCloudApiHandlers = Layer.mergeAll( · 17 unmodified lines | ||
| 47 | 55 | GraphqlHandlers, |
| 48 | 56 | ); |
| 49 | 57 | |
| 58 | +// `ErrorCaptureLive` is provided above the handler + middleware layers | |
| 59 | +// so the `withCapture` translation path (typed-channel `StorageError → | |
| 60 | +// InternalError(traceId)`) AND the observability middleware's defect | |
| 61 | +// catchall both see the same Sentry-backed implementation. | |
| 50 | 62 | export const ProtectedCloudApiLive = HttpApiBuilder.api(ProtectedCloudApi).pipe( |
| 51 | − Layer.provide(Layer.merge(ProtectedCloudApiHandlers, OrgAuthLive)), | |
| 63 | + Layer.provide( | |
| 64 | + Layer.mergeAll(ProtectedCloudApiHandlers, OrgAuthLive, ObservabilityLive), | |
| 65 | + ), | |
| 66 | + Layer.provide(ErrorCaptureLive), | |
| 52 | 67 | ); |
| @@ -34,6 +34,9 @@ const createProtectedApp = (organizationId: string, organizationName: string) => · 33 unmodified lines | ||
| 34 | 34 | Effect.gen(function* () { |
| 35 | 35 | const { executor, engine } = yield* makeExecutionStack(organizationId, organizationName); |
| 36 | 36 | |
| 37 | + // Handlers wrap their own bodies with `capture(...)` — the edge | |
| 38 | + // translation lives per-handler, not at service construction. See | |
| 39 | + // notes/error-handling.md. | |
| 37 | 40 | const requestServices = Layer.mergeAll( |
| 38 | 41 | Layer.succeed(ExecutorService, executor), |
| 39 | 42 | Layer.succeed(ExecutionEngineService, engine), |
| @@ -0,0 +1,31 @@ | ||
| 1 | +// --------------------------------------------------------------------------- | |
| 2 | +// Cloud-app implementation of the shared `ErrorCapture` service. This is the | |
| 3 | +// only file in the cloud-app that imports `@sentry/cloudflare` for error | |
| 4 | +// capture — handlers, plugin SDKs, and storage code all stay | |
| 5 | +// Sentry-agnostic and request the `ErrorCapture` tag instead. | |
| 6 | +// | |
| 7 | +// `withObservability` (in @executor/api) wraps every handler effect; when | |
| 8 | +// it sees an unmapped cause it asks `ErrorCapture.captureException` for a | |
| 9 | +// trace id and fails with `InternalError({ traceId })`. The client gets | |
| 10 | +// the opaque id, we get the full cause + stack in Sentry. | |
| 11 | +// --------------------------------------------------------------------------- | |
| 12 | + | |
| 13 | +import * as Sentry from "@sentry/cloudflare"; | |
| 14 | +import { Cause, Effect, Layer } from "effect"; | |
| 15 | + | |
| 16 | +import { ErrorCapture } from "@executor/api"; | |
| 17 | + | |
| 18 | +export const ErrorCaptureLive: Layer.Layer<ErrorCapture> = Layer.succeed( | |
| 19 | + ErrorCapture, | |
| 20 | + ErrorCapture.of({ | |
| 21 | + captureException: (cause) => | |
| 22 | + Effect.sync(() => { | |
| 23 | + const squashed = Cause.squash(cause); | |
| 24 | + const eventId = Sentry.captureException(squashed, (scope) => { | |
| 25 | + scope.setExtra("cause", Cause.pretty(cause)); | |
| 26 | + return scope; | |
| 27 | + }); | |
| 28 | + return eventId ?? ""; | |
| 29 | + }), | |
| 30 | + }), | |
| 31 | +); | |
| @@ -78,5 +78,9 @@ export const createScopedExecutor = ( · 77 unmodified lines | ||
| 78 | 78 | createdAt: new Date(), |
| 79 | 79 | }); |
| 80 | 80 | |
| 81 | + // The executor surface returns raw `StorageFailure`; translation to | |
| 82 | + // the opaque `InternalError({ traceId })` happens at the HTTP edge | |
| 83 | + // via `withCapture` (see `api/protected-layers.ts`). That's | |
| 84 | + // where `ErrorCaptureLive` (Sentry) gets wired in. | |
| 81 | 85 | return yield* createExecutor({ scope, adapter, blobs, plugins }); |
| 82 | 86 | }); |