Skip to content

Commit

Permalink
feat: add support for zod error (#1146)
Browse files Browse the repository at this point in the history
  • Loading branch information
himanshu-dixit authored Jan 3, 2025
1 parent 343f57a commit 79e7b87
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
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

0 comments on commit 79e7b87

Please sign in to comment.