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
6import { HttpApiBuilder, HttpRouter, HttpServer } from "@effect/platform";6import { HttpApiBuilder, HttpRouter, HttpServer } from "@effect/platform";
7import { Layer } from "effect";7import { Layer } from "effect";
8 8
9import { CoreExecutorApi } from "@executor/api";9import {
10 CoreExecutorApi,
11 InternalError,
12 observabilityMiddleware,
13} from "@executor/api";
10import { CoreHandlers } from "@executor/api/server";14import { CoreHandlers } from "@executor/api/server";
11import { OpenApiGroup, OpenApiHandlers } from "@executor/plugin-openapi/api";15import { OpenApiGroup, OpenApiHandlers } from "@executor/plugin-openapi/api";
12import { McpGroup, McpHandlers } from "@executor/plugin-mcp/api";16import { McpGroup, McpHandlers } from "@executor/plugin-mcp/api";
@@ -18,12 +22,16 @@ import { UserStoreService } from "../auth/context"; · 5 unmodified lines
18import { WorkOSAuth } from "../auth/workos";22import { WorkOSAuth } from "../auth/workos";
19import { AutumnService } from "../services/autumn";23import { AutumnService } from "../services/autumn";
20import { DbService } from "../services/db";24import { DbService } from "../services/db";
25import { ErrorCaptureLive } from "../observability";
21 26
22export const ProtectedCloudApi = CoreExecutorApi.add(OpenApiGroup)27export const ProtectedCloudApi = CoreExecutorApi.add(OpenApiGroup)
23 .add(McpGroup)28 .add(McpGroup)
24 .add(GraphqlGroup)29 .add(GraphqlGroup)
30 .addError(InternalError)
25 .middleware(OrgAuth);31 .middleware(OrgAuth);
26 32
33const ObservabilityLive = observabilityMiddleware(ProtectedCloudApi);
34
27const DbLive = DbService.Live;35const DbLive = DbService.Live;
28const UserStoreLive = UserStoreService.Live.pipe(Layer.provide(DbLive));36const UserStoreLive = UserStoreService.Live.pipe(Layer.provide(DbLive));
29 37
@@ -47,6 +55,13 @@ export const ProtectedCloudApiHandlers = Layer.mergeAll( · 17 unmodified lines
47 GraphqlHandlers,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.
50export const ProtectedCloudApiLive = HttpApiBuilder.api(ProtectedCloudApi).pipe(62export 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);
apps/cloud/src/api/protected.ts+3−0
@@ -34,6 +34,9 @@ const createProtectedApp = (organizationId: string, organizationName: string) => · 33 unmodified lines
34 Effect.gen(function* () {34 Effect.gen(function* () {
35 const { executor, engine } = yield* makeExecutionStack(organizationId, organizationName);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 const requestServices = Layer.mergeAll(40 const requestServices = Layer.mergeAll(
38 Layer.succeed(ExecutorService, executor),41 Layer.succeed(ExecutorService, executor),
39 Layer.succeed(ExecutionEngineService, engine),42 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
13import * as Sentry from "@sentry/cloudflare";
14import { Cause, Effect, Layer } from "effect";
15
16import { ErrorCapture } from "@executor/api";
17
18export 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
78 createdAt: new Date(),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 return yield* createExecutor({ scope, adapter, blobs, plugins });85 return yield* createExecutor({ scope, adapter, blobs, plugins });
82 });86 });

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