History

Merge pull request #282 from RhysSullivan/rs/typed-error-edge-model

feat(sdk): typed errors propagate to one edge mapping (StorageError, InternalError, Telemetry)

Rhys Sullivan committed 7 months ago

Commit c28e7102 parents c64a159 4d95b1375 files · +2357 −1017Browse files

Showing the changes to apps/cloud/src (4 of 75 files). Show all

Unified · Split

4 files
  1. apps/cloud/src/api/protected-layers.ts +17 −2
  2. apps/cloud/src/api/protected.ts +3 −0
  3. apps/cloud/src/observability.ts +31 −0
  4. apps/cloud/src/services/executor.ts +4 −0
apps/cloud/src/api/protected-layers.ts+17−2
@@ -6,7 +6,11 @@ · 5 unmodified lines
66 import { HttpApiBuilder, HttpRouter, HttpServer } from "@effect/platform";
77 import { Layer } from "effect";
88
9−import { CoreExecutorApi } from "@executor/api";
9+import {
10+ CoreExecutorApi,
11+ InternalError,
12+ observabilityMiddleware,
13+} from "@executor/api";
1014 import { CoreHandlers } from "@executor/api/server";
1115 import { OpenApiGroup, OpenApiHandlers } from "@executor/plugin-openapi/api";
1216 import { McpGroup, McpHandlers } from "@executor/plugin-mcp/api";
@@ -18,12 +22,16 @@ import { UserStoreService } from "../auth/context"; · 5 unmodified lines
1822 import { WorkOSAuth } from "../auth/workos";
1923 import { AutumnService } from "../services/autumn";
2024 import { DbService } from "../services/db";
25+import { ErrorCaptureLive } from "../observability";
2126
2227 export const ProtectedCloudApi = CoreExecutorApi.add(OpenApiGroup)
2328 .add(McpGroup)
2429 .add(GraphqlGroup)
30+ .addError(InternalError)
2531 .middleware(OrgAuth);
2632
33+const ObservabilityLive = observabilityMiddleware(ProtectedCloudApi);
34+
2735 const DbLive = DbService.Live;
2836 const UserStoreLive = UserStoreService.Live.pipe(Layer.provide(DbLive));
2937
@@ -47,6 +55,13 @@ export const ProtectedCloudApiHandlers = Layer.mergeAll( · 17 unmodified lines
4755 GraphqlHandlers,
4856 );
4957
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.
5062 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),
5267 );
apps/cloud/src/api/protected.ts+3−0
@@ -34,6 +34,9 @@ const createProtectedApp = (organizationId: string, organizationName: string) => · 33 unmodified lines
3434 Effect.gen(function* () {
3535 const { executor, engine } = yield* makeExecutionStack(organizationId, organizationName);
3636
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.
3740 const requestServices = Layer.mergeAll(
3841 Layer.succeed(ExecutorService, executor),
3942 Layer.succeed(ExecutionEngineService, engine),
apps/cloud/src/observability.ts+31−0
@@ -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+);
apps/cloud/src/services/executor.ts+4−0
@@ -78,5 +78,9 @@ export const createScopedExecutor = ( · 77 unmodified lines
7878 createdAt: new Date(),
7979 });
8080
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.
8185 return yield* createExecutor({ scope, adapter, blobs, plugins });
8286 });

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t