From 3b7b0f720e1a9caeb4efefe742f8078f2a85c764 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benoi=CC=82t=20Rouleau?= Date: Mon, 2 Nov 2026 14:39:26 -0500 Subject: [PATCH] fix(amp admin api): keep http client errors typed, map 401/403 to Unauthorized/Forbidden Every AdminApi method turned HttpClientError into a defect, so failures its type declares (connection errors, undeclared statuses) killed the fiber and catchTag("HttpClientError") never ran. 401 and 403 responses now become Unauthorized and Forbidden on every method whatever their body, as HttpError promises. Schema errors stay defects. Co-Authored-By: Claude Opus 5.5 --- packages/amp/src/admin/service.ts | 48 ++++++++++----- packages/amp/test/admin/service.test.ts | 78 +++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 15 deletions(-) diff --git a/packages/amp/src/admin/service.ts b/packages/amp/src/admin/service.ts index fd15418..5635602 100644 --- a/packages/amp/src/admin/service.ts +++ b/packages/amp/src/admin/service.ts @@ -14,11 +14,12 @@ import * as Effect from "effect/Effect" import { constUndefined } from "effect/Function" import * as Layer from "effect/Layer" import * as Option from "effect/Option" +import * as Schema from "effect/Schema" import * as HttpClient from "effect/http/HttpClient" import type * as HttpClientError from "effect/http/HttpClientError" import * as HttpClientRequest from "effect/http/HttpClientRequest" import * as HttpApiClient from "effect/http-api/HttpApiClient" -import type * as HttpApiError from "effect/http-api/HttpApiError" +import * as HttpApiError from "effect/http-api/HttpApiError" import type * as KeyValueStore from "effect/persistence/KeyValueStore" import * as Auth from "../auth/service.ts" import type * as Models from "../core/domain.ts" @@ -216,6 +217,20 @@ export interface MakeOptions { readonly jwt?: string | null | undefined } +/** + * Keeps HTTP client errors as typed failures, and turns 401 and 403 responses + * into `Unauthorized` and `Forbidden` whatever their body, since the Admin API's + * error bodies don't decode into them. Schema errors become defects. + */ +const handleHttpError = ( + error: HttpClientError.HttpClientError | Schema.SchemaError +): Effect.Effect => { + if (Schema.isSchemaError(error)) return Effect.die(error) + if (error.response?.status === 401) return Effect.fail(new HttpApiError.Unauthorized({})) + if (error.response?.status === 403) return Effect.fail(new HttpApiError.Forbidden({})) + return Effect.fail(error) +} + const make = Effect.fnUntraced(function* (options: MakeOptions) { type Service = typeof AdminApi.Service @@ -255,7 +270,7 @@ const make = Effect.fnUntraced(function* (options: MakeOptions) { yield* Effect.annotateCurrentSpan({ params }) return yield* client.dataset.getDatasetManifest({ params }) }, - Effect.catchTag(["HttpClientError", "SchemaError"], Effect.die) + Effect.catchTag(["HttpClientError", "SchemaError"], handleHttpError) ) const getDatasetLineage: Service["getDatasetLineage"] = Effect.fn("AdminApi.getDatasetLineage")( @@ -269,12 +284,12 @@ const make = Effect.fnUntraced(function* (options: MakeOptions) { yield* Effect.annotateCurrentSpan({ params, query }) return yield* client.dataset.getDatasetLineage({ params, query }) }, - Effect.catchTag(["HttpClientError", "SchemaError"], Effect.die) + Effect.catchTag(["HttpClientError", "SchemaError"], handleHttpError) ) const getDatasets: Service["getDatasets"] = client.dataset .getDatasets({}) - .pipe(Effect.catchTag(["HttpClientError", "SchemaError"], Effect.die), Effect.withSpan("AdminApi.getDatasets")) + .pipe(Effect.catchTag(["HttpClientError", "SchemaError"], handleHttpError), Effect.withSpan("AdminApi.getDatasets")) const getDatasetVersion: Service["getDatasetVersion"] = Effect.fn("AdminApi.getDatasetVersion")( function* (namespace, name, revision) { @@ -282,7 +297,7 @@ const make = Effect.fnUntraced(function* (options: MakeOptions) { yield* Effect.annotateCurrentSpan({ params }) return yield* client.dataset.getDatasetVersion({ params }) }, - Effect.catchTag(["HttpClientError", "SchemaError"], Effect.die) + Effect.catchTag(["HttpClientError", "SchemaError"], handleHttpError) ) const getDatasetVersions: Service["getDatasetVersions"] = Effect.fn("AdminApi.getDatasetVersions")( @@ -291,7 +306,7 @@ const make = Effect.fnUntraced(function* (options: MakeOptions) { yield* Effect.annotateCurrentSpan({ params }) return yield* client.dataset.getDatasetVersions({ params }) }, - Effect.catchTag(["HttpClientError", "SchemaError"], Effect.die) + Effect.catchTag(["HttpClientError", "SchemaError"], handleHttpError) ) const registerDataset: Service["registerDataset"] = Effect.fn("AdminApi.registerDataset")( @@ -300,7 +315,7 @@ const make = Effect.fnUntraced(function* (options: MakeOptions) { yield* Effect.annotateCurrentSpan({ payload }) return yield* client.dataset.registerDataset({ payload }) }, - Effect.catchTag(["HttpClientError", "SchemaError"], Effect.die) + Effect.catchTag(["HttpClientError", "SchemaError"], handleHttpError) ) // Job Operations @@ -315,7 +330,7 @@ const make = Effect.fnUntraced(function* (options: MakeOptions) { yield* Effect.annotateCurrentSpan({ query }) return yield* client.job.getJobs({ query }) }, - Effect.catchTag(["HttpClientError", "SchemaError"], Effect.die) + Effect.catchTag(["HttpClientError", "SchemaError"], handleHttpError) ) const getJobById: Service["getJobById"] = Effect.fn("AdminApi.getJobById")( @@ -324,7 +339,7 @@ const make = Effect.fnUntraced(function* (options: MakeOptions) { yield* Effect.annotateCurrentSpan({ params }) return yield* client.job.getJobById({ params }) }, - Effect.catchTag(["HttpClientError", "SchemaError"], Effect.die) + Effect.catchTag(["HttpClientError", "SchemaError"], handleHttpError) ) const stopJob: Service["stopJob"] = Effect.fn("AdminApi.stopJob")( @@ -333,7 +348,7 @@ const make = Effect.fnUntraced(function* (options: MakeOptions) { yield* Effect.annotateCurrentSpan({ params }) return yield* client.job.stopJob({ params }) }, - Effect.catchTag(["HttpClientError", "SchemaError"], Effect.die) + Effect.catchTag(["HttpClientError", "SchemaError"], handleHttpError) ) const deleteJob: Service["deleteJob"] = Effect.fn("AdminApi.deleteJob")( @@ -342,20 +357,23 @@ const make = Effect.fnUntraced(function* (options: MakeOptions) { yield* Effect.annotateCurrentSpan({ params }) return yield* client.job.deleteJob({ params }) }, - Effect.catchTag(["HttpClientError", "SchemaError"], Effect.die) + Effect.catchTag(["HttpClientError", "SchemaError"], handleHttpError) ) // Worker Operations const getWorkers: Service["getWorkers"] = client.worker .getWorkers({}) - .pipe(Effect.catchTag(["HttpClientError", "SchemaError"], Effect.die), Effect.withSpan("AdminApi.getWorkers")) + .pipe(Effect.catchTag(["HttpClientError", "SchemaError"], handleHttpError), Effect.withSpan("AdminApi.getWorkers")) // Provider Operations const getProviders: Service["getProviders"] = client.provider .getProviders({}) - .pipe(Effect.catchTag(["HttpClientError", "SchemaError"], Effect.die), Effect.withSpan("AdminApi.getProviders")) + .pipe( + Effect.catchTag(["HttpClientError", "SchemaError"], handleHttpError), + Effect.withSpan("AdminApi.getProviders") + ) // Manifest Operations @@ -363,7 +381,7 @@ const make = Effect.fnUntraced(function* (options: MakeOptions) { function* (manifest) { return yield* client.manifest.registerManifest({ payload: manifest }) }, - Effect.catchTag(["HttpClientError", "SchemaError"], Effect.die) + Effect.catchTag(["HttpClientError", "SchemaError"], handleHttpError) ) // Schema Operations @@ -372,7 +390,7 @@ const make = Effect.fnUntraced(function* (options: MakeOptions) { function* (payload) { return yield* client.schema.getOutputSchema({ payload }) }, - Effect.catchTag(["HttpClientError", "SchemaError"], Effect.die) + Effect.catchTag(["HttpClientError", "SchemaError"], handleHttpError) ) return AdminApi.of({ diff --git a/packages/amp/test/admin/service.test.ts b/packages/amp/test/admin/service.test.ts index 85a1ab0..e75e007 100644 --- a/packages/amp/test/admin/service.test.ts +++ b/packages/amp/test/admin/service.test.ts @@ -6,6 +6,7 @@ import * as Effect from "effect/Effect" import * as Layer from "effect/Layer" import * as Redacted from "effect/Redacted" import * as HttpClient from "effect/http/HttpClient" +import * as HttpClientError from "effect/http/HttpClientError" import * as HttpClientResponse from "effect/http/HttpClientResponse" import * as KeyValueStore from "effect/persistence/KeyValueStore" @@ -351,6 +352,83 @@ describe("AdminApi", () => { ) }) +describe("AdminApi failures", () => { + const unauthorized = { error_code: "UNAUTHORIZED", error_message: "missing bearer token" } + + it.effect("keeps transport failures as HttpClientError", () => + Effect.gen(function* () { + const admin = yield* AdminApi.AdminApi + const reason = yield* admin.getDatasets.pipe( + Effect.map(() => "none"), + Effect.catchTag("HttpClientError", (error) => Effect.succeed(error.reason._tag)) + ) + assert.strictEqual(reason, "TransportError") + }).pipe( + Effect.provide( + AdminApi.layer({ url: "http://localhost:1610" }).pipe( + Layer.provide( + Layer.succeed( + HttpClient.HttpClient, + HttpClient.make((request) => + Effect.fail( + new HttpClientError.HttpClientError({ + reason: new HttpClientError.TransportError({ request, description: "connection refused" }) + }) + ) + ) + ) + ) + ) + ) + ) + ) + + it.effect("keeps an undeclared status as HttpClientError", () => + Effect.gen(function* () { + const admin = yield* AdminApi.AdminApi + const error = yield* admin.getJobs().pipe(Effect.flip) + assert.strictEqual(error._tag, "HttpClientError") + }).pipe(Effect.provide(makeLayer({ "GET /jobs": { status: 502, body: "bad gateway" } }))) + ) + + it.effect("maps a 401 to Unauthorized on every endpoint, whatever its body", () => + Effect.gen(function* () { + const admin = yield* AdminApi.AdminApi + const jobs = yield* admin.getJobs().pipe(Effect.flip) + assert.strictEqual(jobs._tag, "Unauthorized") + const datasets = yield* admin.getDatasets.pipe(Effect.flip) + assert.strictEqual(datasets._tag, "Unauthorized") + const workers = yield* admin.getWorkers.pipe(Effect.flip) + assert.strictEqual(workers._tag, "Unauthorized") + }).pipe( + Effect.provide( + makeLayer({ + "GET /jobs": { status: 401, body: unauthorized }, + "GET /datasets": { status: 401 }, + "GET /workers": { status: 401, body: { _tag: "Unauthorized" } } + }) + ) + ) + ) + + it.effect("maps a 403 to Forbidden on every endpoint, whatever its body", () => + Effect.gen(function* () { + const admin = yield* AdminApi.AdminApi + const providers = yield* admin.getProviders.pipe(Effect.flip) + assert.strictEqual(providers._tag, "Forbidden") + const datasets = yield* admin.getDatasets.pipe(Effect.flip) + assert.strictEqual(datasets._tag, "Forbidden") + }).pipe( + Effect.provide( + makeLayer({ + "GET /providers": { status: 403 }, + "GET /datasets": { status: 403, body: { error_code: "FORBIDDEN", error_message: "not allowed" } } + }) + ) + ) + ) +}) + describe("AdminApi authentication", () => { const makeRecordingClient = (authorization: Array) => Layer.succeed(