-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
feat: update backend error #1089
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import { AxiosInstance } from "axios"; | ||
import apiClient from "../client/client"; | ||
import { client as axiosClient } from "../client/services.gen"; | ||
import { setAxiosClientConfig } from "../utils/config"; | ||
|
@@ -22,6 +23,7 @@ export class BackendClient { | |
* The runtime environment where the client is being used. | ||
*/ | ||
public runtime: string; | ||
public instance: AxiosInstance; | ||
|
||
/** | ||
* Creates an instance of apiClientDetails. | ||
|
@@ -34,6 +36,7 @@ export class BackendClient { | |
this.runtime = runtime || ""; | ||
this.apiKey = apiKey; | ||
this.baseUrl = baseUrl; | ||
this.instance = axiosClient.instance; | ||
|
||
if (!apiKey) { | ||
throw CEG.getCustomError( | ||
|
@@ -86,5 +89,6 @@ export class BackendClient { | |
}); | ||
|
||
setAxiosClientConfig(axiosClient.instance); | ||
this.instance = axiosClient.instance; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Redundant assignment of |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,16 @@ | ||
import { AxiosError } from "axios"; | ||
import { COMPOSIO_SDK_ERROR_CODES } from "./constants"; | ||
import { | ||
API_TO_SDK_ERROR_CODE, | ||
BASE_ERROR_CODE_INFO, | ||
COMPOSIO_SDK_ERROR_CODES, | ||
} from "./constants"; | ||
|
||
export interface ErrorResponseData { | ||
message: string; | ||
error: string; | ||
errors?: Record<string, unknown>[]; | ||
error: { | ||
type: string; | ||
name: string; | ||
message: string; | ||
}; | ||
} | ||
|
||
interface ErrorDetails { | ||
|
@@ -15,72 +21,56 @@ interface ErrorDetails { | |
} | ||
|
||
export const getAPIErrorDetails = ( | ||
errorKey: string, | ||
axiosError: AxiosError<ErrorResponseData>, | ||
predefinedError: Record<string, unknown> | ||
axiosError: AxiosError<ErrorResponseData> | ||
): ErrorDetails => { | ||
const statusCode = axiosError.response?.status; | ||
const errorCode = statusCode | ||
? API_TO_SDK_ERROR_CODE[statusCode] | ||
: COMPOSIO_SDK_ERROR_CODES.BACKEND.UNKNOWN; | ||
const predefinedError = BASE_ERROR_CODE_INFO[errorCode]; | ||
|
||
const defaultErrorDetails = { | ||
message: axiosError.message, | ||
description: | ||
axiosError.response?.data?.message || | ||
axiosError.response?.data?.error?.message || | ||
axiosError.response?.data?.error || | ||
axiosError.message, | ||
possibleFix: | ||
"Please check the network connection, request parameters, and ensure the API endpoint is correct.", | ||
}; | ||
|
||
const metadata = generateMetadataFromAxiosError(axiosError); | ||
switch (errorKey) { | ||
|
||
const errorNameFromBE = axiosError.response?.data?.error?.name; | ||
const errorTypeFromBE = axiosError.response?.data?.error?.type; | ||
const errorMessage = axiosError.response?.data?.error?.message; | ||
|
||
const genericMessage = `${errorNameFromBE || predefinedError.message} ${errorTypeFromBE ? `- ${errorTypeFromBE}` : ""} on ${axiosError.config?.baseURL! + axiosError.config?.url!}`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding null/undefined checks for |
||
|
||
switch (errorCode) { | ||
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: | ||
return { | ||
message: `${predefinedError.message || axiosError.message} for ${axiosError.config?.baseURL! + axiosError.config?.url!}`, | ||
description: (axiosError.response?.data?.message! || | ||
predefinedError.description) as string, | ||
possibleFix: (predefinedError.possibleFix! || | ||
defaultErrorDetails.possibleFix) as string, | ||
metadata, | ||
}; | ||
|
||
case COMPOSIO_SDK_ERROR_CODES.BACKEND.UNKNOWN: | ||
case COMPOSIO_SDK_ERROR_CODES.BACKEND.BAD_REQUEST: | ||
const validationErrors = axiosError.response?.data?.errors; | ||
const formattedErrors = Array.isArray(validationErrors) | ||
? validationErrors | ||
.map((err) => JSON.stringify(err as Record<string, unknown>)) | ||
.join(", ") | ||
: JSON.stringify( | ||
validationErrors as unknown as Record<string, unknown> | ||
); | ||
|
||
return { | ||
message: `Validation Errors while making request to ${axiosError.config?.baseURL! + axiosError.config?.url!}`, | ||
description: `Validation Errors: ${formattedErrors}`, | ||
message: genericMessage, | ||
description: errorMessage || (predefinedError.description as string), | ||
possibleFix: | ||
"Please check the request parameters and ensure they are correct.", | ||
metadata, | ||
}; | ||
|
||
case COMPOSIO_SDK_ERROR_CODES.BACKEND.UNKNOWN: | ||
case COMPOSIO_SDK_ERROR_CODES.COMMON.UNKNOWN: | ||
return { | ||
message: `${axiosError.message} for ${axiosError.config?.baseURL! + axiosError.config?.url!}`, | ||
description: (axiosError.response?.data?.message! || | ||
axiosError.response?.data?.error! || | ||
axiosError.message) as string, | ||
possibleFix: "Please contact tech@composio.dev with the error details.", | ||
predefinedError.possibleFix! || | ||
(defaultErrorDetails.possibleFix as string), | ||
metadata, | ||
}; | ||
|
||
default: | ||
return { | ||
message: `${predefinedError.message || axiosError.message} for ${axiosError.config?.baseURL! + axiosError.config?.url!}`, | ||
description: (axiosError.response?.data?.message! || | ||
predefinedError.description) as string, | ||
possibleFix: (predefinedError.possibleFix! || | ||
defaultErrorDetails.possibleFix) as string, | ||
message: genericMessage, | ||
description: errorMessage || (predefinedError.description as string), | ||
possibleFix: | ||
predefinedError.possibleFix! || | ||
(defaultErrorDetails.possibleFix as string), | ||
metadata, | ||
}; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test assertion
expect(e.possibleFix).toBe(e.possibleFix)
is comparing a value with itself, which will always pass. This should probably be comparing against an expected value instead.