Skip to content

Commit

Permalink
feat: improve errors
Browse files Browse the repository at this point in the history
  • Loading branch information
floydspace committed Oct 2, 2023
1 parent a489684 commit bd7db85
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/curvy-dolphins-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect-aws/client-api-gateway-management-api": minor
---

return tagged errors in the failure channel
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ import {
DeleteConnectionCommand,
DeleteConnectionCommandInput,
DeleteConnectionCommandOutput,
ForbiddenException,
GetConnectionCommand,
GetConnectionCommandInput,
GetConnectionCommandOutput,
GoneException,
LimitExceededException,
PayloadTooLargeException,
PostToConnectionCommand,
PostToConnectionCommandInput,
PostToConnectionCommandOutput,
Expand All @@ -17,6 +21,13 @@ import {
ApiGatewayManagementApiClientInstanceTag,
DefaultApiGatewayManagementApiClientInstanceLayer,
} from "./Context";
import {
ForbiddenError,
GoneError,
LimitExceededError,
PayloadTooLargeError,
SdkError,
} from "./Errors";

const commands = {
DeleteConnectionCommand,
Expand All @@ -31,23 +42,39 @@ export interface ApiGatewayManagementApiService {
deleteConnection(
args: DeleteConnectionCommandInput,
options?: __HttpHandlerOptions,
): Effect.Effect<never, unknown, DeleteConnectionCommandOutput>;
): Effect.Effect<
never,
SdkError | GoneError | ForbiddenError | LimitExceededError,
DeleteConnectionCommandOutput
>;

/**
* @see {@link GetConnectionCommand}
*/
getConnection(
args: GetConnectionCommandInput,
options?: __HttpHandlerOptions,
): Effect.Effect<never, unknown, GetConnectionCommandOutput>;
): Effect.Effect<
never,
SdkError | GoneError | ForbiddenError | LimitExceededError,
GetConnectionCommandOutput
>;

/**
* @see {@link PostToConnectionCommand}
*/
postToConnection(
args: PostToConnectionCommandInput,
options?: __HttpHandlerOptions,
): Effect.Effect<never, unknown, PostToConnectionCommandOutput>;
): Effect.Effect<
never,
| SdkError
| GoneError
| ForbiddenError
| LimitExceededError
| PayloadTooLargeError,
PostToConnectionCommandOutput
>;
}

export const BaseApiGatewayManagementApiServiceEffect = Effect.gen(
Expand All @@ -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$/,
"",
Expand Down
23 changes: 23 additions & 0 deletions packages/client-api-gateway-management-api/src/Errors.ts
Original file line number Diff line number Diff line change
@@ -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")<Error> {}

export class ForbiddenError extends Data.TaggedClass(
"ForbiddenError",
)<ForbiddenException> {}

export class GoneError extends Data.TaggedClass("GoneError")<GoneException> {}

export class LimitExceededError extends Data.TaggedClass(
"LimitExceededError",
)<LimitExceededException> {}

export class PayloadTooLargeError extends Data.TaggedClass(
"PayloadTooLargeError",
)<PayloadTooLargeException> {}
1 change: 1 addition & 0 deletions packages/client-api-gateway-management-api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./Context";
export * from "./Errors";
export * from "./ApiGatewayManagementApi";
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import {
ApiGatewayManagementApiClient,
PostToConnectionCommand,
PostToConnectionCommandInput,
ApiGatewayManagementApiClient,
} from "@aws-sdk/client-apigatewaymanagementapi";
import { pipe } from "@effect/data/Function";
import * as Effect from "@effect/io/Effect";
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";
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit bd7db85

Please sign in to comment.