All files / sdk/utils/errors index.ts

84.21% Statements 16/19
33.33% Branches 1/3
100% Functions 2/2
84.21% Lines 16/19

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 7716x 16x 16x 16x                         16x   5x 5x     5x 5x 5x 5x                 5x     5x                   5x                 5x   5x                                    
import { TELEMETRY_URL } from "../constants";
import ComposioSDKContext from "../composioContext";
import { sendBrowserReq, sendProcessReq } from "../../../utils/external";
import { getEnvVariable } from "../../../utils/shared";
 
type ErrorPayload = {
  error_id: string;
  error_code: string;
  original_error: string;
  description: string;
  metadata: Record<string, any>;
  message: string;
  possible_fix: string;
  current_stack: string[];
};
 
export async function logError(payload: ErrorPayload) {
  const isTelementryDisabled =
    getEnvVariable("TELEMETRY_DISABLED", "false") === "true";
  Iif (isTelementryDisabled) {
    return;
  }
  try {
    const isBrowser = typeof window !== "undefined";
    const reportingPayload = await generateReportingPayload(payload);
    const reqPayload = {
      data: reportingPayload,
      url: `${TELEMETRY_URL}/api/sdk_metrics/error`,
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
    };
 
    Iif (isBrowser) {
      await sendBrowserReq(reqPayload);
    } else {
      await sendProcessReq(reqPayload);
    }
  } catch (error) {
    console.error("Error sending error to telemetry", error);
    // DO NOTHING
  }
}
 
async function generateReportingPayload(payload: ErrorPayload) {
  const { apiKey, baseURL, composioVersion, frameworkRuntime, source } =
    ComposioSDKContext;
  const {
    error_id,
    error_code,
    description,
    message,
    possible_fix,
    original_error,
    current_stack,
  } = payload;
 
  return {
    error_id,
    error_code,
    description,
    error_message: message,
    possible_fix,
    original_error,
    current_stack,
    sdk_meta: {
      platform: process.platform,
      version: composioVersion,
      baseURL,
      apiKey,
      frameworkRuntime,
      source,
    },
  };
}