diff --git a/.changeset/curvy-dolphins-camp.md b/.changeset/curvy-dolphins-camp.md new file mode 100644 index 00000000..0f83cec6 --- /dev/null +++ b/.changeset/curvy-dolphins-camp.md @@ -0,0 +1,5 @@ +--- +"@effect-aws/client-api-gateway-management-api": minor +--- + +return tagged errors in the failure channel diff --git a/packages/client-api-gateway-management-api/src/ApiGatewayManagementApi.ts b/packages/client-api-gateway-management-api/src/ApiGatewayManagementApi.ts index d726faa2..81220bea 100644 --- a/packages/client-api-gateway-management-api/src/ApiGatewayManagementApi.ts +++ b/packages/client-api-gateway-management-api/src/ApiGatewayManagementApi.ts @@ -2,9 +2,13 @@ import { DeleteConnectionCommand, DeleteConnectionCommandInput, DeleteConnectionCommandOutput, + ForbiddenException, GetConnectionCommand, GetConnectionCommandInput, GetConnectionCommandOutput, + GoneException, + LimitExceededException, + PayloadTooLargeException, PostToConnectionCommand, PostToConnectionCommandInput, PostToConnectionCommandOutput, @@ -17,6 +21,13 @@ import { ApiGatewayManagementApiClientInstanceTag, DefaultApiGatewayManagementApiClientInstanceLayer, } from "./Context"; +import { + ForbiddenError, + GoneError, + LimitExceededError, + PayloadTooLargeError, + SdkError, +} from "./Errors"; const commands = { DeleteConnectionCommand, @@ -31,7 +42,11 @@ export interface ApiGatewayManagementApiService { deleteConnection( args: DeleteConnectionCommandInput, options?: __HttpHandlerOptions, - ): Effect.Effect; + ): Effect.Effect< + never, + SdkError | GoneError | ForbiddenError | LimitExceededError, + DeleteConnectionCommandOutput + >; /** * @see {@link GetConnectionCommand} @@ -39,7 +54,11 @@ export interface ApiGatewayManagementApiService { getConnection( args: GetConnectionCommandInput, options?: __HttpHandlerOptions, - ): Effect.Effect; + ): Effect.Effect< + never, + SdkError | GoneError | ForbiddenError | LimitExceededError, + GetConnectionCommandOutput + >; /** * @see {@link PostToConnectionCommand} @@ -47,7 +66,15 @@ export interface ApiGatewayManagementApiService { postToConnection( args: PostToConnectionCommandInput, options?: __HttpHandlerOptions, - ): Effect.Effect; + ): Effect.Effect< + never, + | SdkError + | GoneError + | ForbiddenError + | LimitExceededError + | PayloadTooLargeError, + PostToConnectionCommandOutput + >; } export const BaseApiGatewayManagementApiServiceEffect = Effect.gen( @@ -57,9 +84,27 @@ export const BaseApiGatewayManagementApiServiceEffect = Effect.gen( return RR.toEntries(commands).reduce((acc, [command]) => { const CommandCtor = commands[command] as any; const methodImpl = (args: any, options: any) => - Effect.tryPromise(() => - client.send(new CommandCtor(args), options ?? {}), - ); + Effect.tryPromise({ + try: () => client.send(new CommandCtor(args), options ?? {}), + catch: (e) => { + if (e instanceof ForbiddenException) { + return new ForbiddenError({ ...e, stack: e.stack }); + } + if (e instanceof GoneException) { + return new GoneError({ ...e, stack: e.stack }); + } + if (e instanceof LimitExceededException) { + return new LimitExceededError({ ...e, stack: e.stack }); + } + if (e instanceof PayloadTooLargeException) { + return new PayloadTooLargeError({ ...e, stack: e.stack }); + } + if (e instanceof Error) { + return new SdkError({ ...e, stack: e.stack }); + } + return e; + }, + }); const methodName = (command[0].toLowerCase() + command.slice(1)).replace( /Command$/, "", diff --git a/packages/client-api-gateway-management-api/src/Errors.ts b/packages/client-api-gateway-management-api/src/Errors.ts new file mode 100644 index 00000000..13b774c3 --- /dev/null +++ b/packages/client-api-gateway-management-api/src/Errors.ts @@ -0,0 +1,23 @@ +import type { + ForbiddenException, + GoneException, + LimitExceededException, + PayloadTooLargeException, +} from "@aws-sdk/client-apigatewaymanagementapi"; +import * as Data from "@effect/data/Data"; + +export class SdkError extends Data.TaggedClass("SdkError") {} + +export class ForbiddenError extends Data.TaggedClass( + "ForbiddenError", +) {} + +export class GoneError extends Data.TaggedClass("GoneError") {} + +export class LimitExceededError extends Data.TaggedClass( + "LimitExceededError", +) {} + +export class PayloadTooLargeError extends Data.TaggedClass( + "PayloadTooLargeError", +) {} diff --git a/packages/client-api-gateway-management-api/src/index.ts b/packages/client-api-gateway-management-api/src/index.ts index fc2cf951..e81a898c 100644 --- a/packages/client-api-gateway-management-api/src/index.ts +++ b/packages/client-api-gateway-management-api/src/index.ts @@ -1,2 +1,3 @@ export * from "./Context"; +export * from "./Errors"; export * from "./ApiGatewayManagementApi"; diff --git a/packages/client-api-gateway-management-api/test/ApiGatewayManagementApi.test.ts b/packages/client-api-gateway-management-api/test/ApiGatewayManagementApi.test.ts index 98796ead..a433a816 100644 --- a/packages/client-api-gateway-management-api/test/ApiGatewayManagementApi.test.ts +++ b/packages/client-api-gateway-management-api/test/ApiGatewayManagementApi.test.ts @@ -1,7 +1,7 @@ import { + ApiGatewayManagementApiClient, PostToConnectionCommand, PostToConnectionCommandInput, - ApiGatewayManagementApiClient, } from "@aws-sdk/client-apigatewaymanagementapi"; import { pipe } from "@effect/data/Function"; import * as Effect from "@effect/io/Effect"; @@ -9,13 +9,14 @@ import * as Exit from "@effect/io/Exit"; import * as Layer from "@effect/io/Layer"; import { mockClient } from "aws-sdk-client-mock"; import { - BaseApiGatewayManagementApiServiceEffect, - DefaultApiGatewayManagementApiClientConfigLayer, - DefaultApiGatewayManagementApiServiceEffect, ApiGatewayManagementApiClientConfigTag, ApiGatewayManagementApiClientInstanceTag, ApiGatewayManagementApiClientOptions, ApiGatewayManagementApiServiceEffect, + BaseApiGatewayManagementApiServiceEffect, + DefaultApiGatewayManagementApiClientConfigLayer, + DefaultApiGatewayManagementApiServiceEffect, + SdkError, } from "../src"; import "aws-sdk-client-mock-jest"; @@ -202,7 +203,11 @@ describe("ApiGatewayManagementApiClientImpl", () => { const result = await pipe(program, Effect.runPromiseExit); - expect(result).toEqual(Exit.fail(new Error("test"))); + expect(result).toEqual( + Exit.fail( + new SdkError({ ...new Error("test"), stack: expect.any(String) }), + ), + ); expect(apigatewaymanagementapiMock).toHaveReceivedCommandTimes( PostToConnectionCommand, 1,