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 1 commit
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
10 changes: 8 additions & 2 deletions js/src/sdk/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ describe("Basic SDK spec suite", () => {
type: "BadRequestError",
name: "InvalidRequestError",
message: "Invalid request for apps",
errors: [
{
code: "invalid_type",
message: "Invalid request for apps",
path: ["apps"],
},
],
});

const client = new Composio({ apiKey: COMPOSIO_API_KEY });
Expand All @@ -70,9 +77,8 @@ 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: {"code`);
}

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;
errors?: 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?.errors;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding type validation for the validation errors to ensure type safety:

const isValidationError = (errors: unknown): errors is Record<string, unknown>[] | Record<string, unknown> => {
  return errors !== null && typeof errors === 'object';
};

const validationErrors = axiosError.response?.data?.errors;
if (validationErrors && isValidationError(validationErrors)) {
  // process the errors
}

This would help prevent runtime errors if the error structure is unexpected.

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