Skip to content

feat: add support for zod error #1146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions js/src/sdk/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ describe("Basic SDK spec suite", () => {
type: "BadRequestError",
name: "InvalidRequestError",
message: "Invalid request for apps",
details: [
{
property: "triggerConfig",
children: [],
constraints: {
isObject: "triggerConfig must be an object",
},
},
],
});

const client = new Composio({ apiKey: COMPOSIO_API_KEY });
Expand All @@ -70,9 +79,10 @@ describe("Basic SDK spec suite", () => {
const error = e as ComposioError;
const errorCode = COMPOSIO_SDK_ERROR_CODES.BACKEND.BAD_REQUEST;
expect(error.errCode).toBe(errorCode);
expect(error.message).toContain("InvalidRequestError ");
expect(error.message).toContain("InvalidRequestError");
expect(error.description).toContain("Invalid request for apps");
expect(error.description).toContain(
`Validation Errors: {"property":"triggerConfig","children":[],"constraints":{"isObject":"triggerConfig must be an object"}}`
);
}

mock.reset();
Expand Down
15 changes: 14 additions & 1 deletion js/src/sdk/utils/errors/src/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface ErrorResponseData {
type: string;
name: string;
message: string;
details?: Record<string, unknown>[] | Record<string, unknown>;
}

interface ErrorDetails {
Expand Down Expand Up @@ -53,13 +54,25 @@ export const getAPIErrorDetails = (
}

switch (errorCode) {
case COMPOSIO_SDK_ERROR_CODES.BACKEND.BAD_REQUEST:
const validationErrors = axiosError.response?.data?.details;
const formattedErrors = Array.isArray(validationErrors)
? validationErrors.map((err) => JSON.stringify(err)).join(", ")
: JSON.stringify(validationErrors);

return {
message: genericMessage,
description: `Validation Errors: ${formattedErrors}`,
possibleFix:
"Please check the request parameters and ensure they are correct.",
metadata,
};
case COMPOSIO_SDK_ERROR_CODES.BACKEND.NOT_FOUND:
case COMPOSIO_SDK_ERROR_CODES.BACKEND.UNAUTHORIZED:
case COMPOSIO_SDK_ERROR_CODES.BACKEND.SERVER_ERROR:
case COMPOSIO_SDK_ERROR_CODES.BACKEND.SERVER_UNAVAILABLE:
case COMPOSIO_SDK_ERROR_CODES.BACKEND.RATE_LIMIT:
case COMPOSIO_SDK_ERROR_CODES.BACKEND.UNKNOWN:
case COMPOSIO_SDK_ERROR_CODES.BACKEND.BAD_REQUEST:
return {
message: genericMessage,
description: errorMessage || (predefinedError.description as string),
Expand Down
Loading