From 79e7b8743b8b625b24779c3d58b4bad5e6becaa7 Mon Sep 17 00:00:00 2001 From: Himanshu Dixit Date: Fri, 3 Jan 2025 17:07:00 +0530 Subject: [PATCH] feat: add support for zod error (#1146) --- js/src/sdk/index.spec.ts | 14 ++++++++++++-- js/src/sdk/utils/errors/src/formatter.ts | 15 ++++++++++++++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/js/src/sdk/index.spec.ts b/js/src/sdk/index.spec.ts index 4e46a66295a..bc069b9864d 100644 --- a/js/src/sdk/index.spec.ts +++ b/js/src/sdk/index.spec.ts @@ -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 }); @@ -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(); diff --git a/js/src/sdk/utils/errors/src/formatter.ts b/js/src/sdk/utils/errors/src/formatter.ts index 1771da74b53..bd04a39be35 100644 --- a/js/src/sdk/utils/errors/src/formatter.ts +++ b/js/src/sdk/utils/errors/src/formatter.ts @@ -9,6 +9,7 @@ export interface ErrorResponseData { type: string; name: string; message: string; + details?: Record[] | Record; } interface ErrorDetails { @@ -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),