All files / sdk/utils/errors/src formatter.ts

76.47% Statements 13/17
23.15% Branches 22/95
66.66% Functions 2/3
73.33% Lines 11/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105  16x                                           16x         1x                   1x 1x                               1x 1x       1x                                                               16x     2x 2x                
import { ComposioError } from "./composioError";
import {
  SDK_ERROR_CODES,
  BASE_ERROR_CODE_INFO,
  BE_STATUS_CODE_TO_SDK_ERROR_CODES,
} from "./constants";
import { AxiosError } from "axios";
 
interface ErrorResponse {
  errorKey: string;
  message: string;
  description: string;
  possibleFix: string;
  metadata?: Record<string, any>;
}
 
interface ErrorDetails {
  message: string;
  description: string;
  possibleFix: string;
  metadata?: Record<string, any>;
}
 
export const getAPIErrorDetails = (
  errorKey: string,
  axiosError: any,
  predefinedError: any
): ErrorDetails => {
  const defaultErrorDetails = {
    message: axiosError.message,
    description:
      axiosError.response?.data?.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) {
    case SDK_ERROR_CODES.BACKEND.NOT_FOUND:
    case SDK_ERROR_CODES.BACKEND.UNAUTHORIZED:
    case SDK_ERROR_CODES.BACKEND.SERVER_ERROR:
    case SDK_ERROR_CODES.BACKEND.SERVER_UNAVAILABLE:
    case 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,
        possibleFix:
          predefinedError.possibleFix || defaultErrorDetails.possibleFix,
        metadata,
      };
 
    case SDK_ERROR_CODES.BACKEND.BAD_REQUEST:
      const validationErrors = axiosError.response?.data?.errors;
      const formattedErrors = Array.isArray(validationErrors)
        ? validationErrors.map((err) => JSON.stringify(err)).join(", ")
        : JSON.stringify(validationErrors);
 
      return {
        message: `Validation Errors while making request to ${axiosError.config.baseURL + axiosError.config.url}`,
        description: `Validation Errors: ${formattedErrors}`,
        possibleFix:
          "Please check the request parameters and ensure they are correct.",
        metadata,
      };
 
    case SDK_ERROR_CODES.BACKEND.UNKNOWN:
    case 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,
        possibleFix: "Please contact tech@composio.dev with the error details.",
        metadata,
      };
 
    default:
      return {
        message: `${predefinedError.message || axiosError.message} for ${axiosError.config.baseURL + axiosError.config.url}`,
        description:
          axiosError.response?.data?.message || predefinedError.description,
        possibleFix:
          predefinedError.possibleFix || defaultErrorDetails.possibleFix,
        metadata,
      };
  }
};
 
export const generateMetadataFromAxiosError = (
  axiosError: any
): Record<string, any> => {
  const requestId = axiosError.response?.headers["x-request-id"];
  return {
    fullUrl: axiosError.config.baseURL + axiosError.config.url,
    method: axiosError.config.method.toUpperCase(),
    statusCode: axiosError.response?.status,
    requestId: requestId ? `Request ID: ${requestId}` : undefined,
    metadata: axiosError.metadata,
  };
};