Skip to content

Commit

Permalink
Merge pull request #1146 from jitsucom/feat/js_client_improvements
Browse files Browse the repository at this point in the history
@jitsu/js error handling improvements
  • Loading branch information
vklimontovich authored Nov 15, 2024
2 parents 12f79fb + 77b98cb commit 38c66c8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
25 changes: 23 additions & 2 deletions libs/jitsu-js/src/analytics-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
ID,
JSONObject,
Options,
ErrorHandler,
} from "@jitsu/protocols/analytics";
import parse from "./index";

Expand All @@ -35,6 +36,7 @@ const defaultConfig: Required<JitsuOptions> = {
fetchTimeoutMs: undefined,
s2s: undefined,
idEndpoint: undefined,
errorPolicy: "log",
privacy: {
dontSend: false,
disableUserIds: false,
Expand Down Expand Up @@ -628,6 +630,22 @@ function maskWriteKey(writeKey?: string): string | undefined {
return writeKey;
}

function getErrorHandler(opts: JitsuOptions): ErrorHandler {
const configuredHandler = opts.errorPolicy || "log";
if (typeof configuredHandler === "function") {
return configuredHandler;
} else if (configuredHandler === "rethrow") {
return e => {
throw e;
};
} else {
//"log" and unsupported value
return e => {
console.error(`[JITSU ERROR] ${e?.message || e?.toString() || "unknown error"}`, e);
};
}
}

async function send(
method,
payload,
Expand All @@ -643,6 +661,7 @@ async function send(
const url = s2s ? `${jitsuConfig.host}/api/s/s2s/${method}` : `${jitsuConfig.host}/api/s/${method}`;
const fetch = jitsuConfig.fetch || globalThis.fetch;
if (!fetch) {
//don't run it through error handler since error is critical and should be addressed
throw new Error(
"Please specify fetch function in jitsu plugin initialization, fetch isn't available in global scope"
);
Expand Down Expand Up @@ -682,7 +701,8 @@ async function send(
clearTimeout(abortTimeout);
}
} catch (e: any) {
throw new Error(`Calling ${url} failed: ${e.message}`);
getErrorHandler(jitsuConfig)(new Error(`Calling ${url} failed: ${e.message}`));
return Promise.resolve();
}
let responseText;
try {
Expand All @@ -709,7 +729,8 @@ async function send(
try {
responseJson = JSON.parse(responseText);
} catch (e) {
return Promise.reject(`Can't parse JSON: ${responseText}: ${e?.message}`);
getErrorHandler(jitsuConfig)(new Error(`Can't parse JSON: ${responseText}: ${e?.message}`));
return Promise.resolve();
}

if (responseJson.destinations && responseJson.destinations.length > 0) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"console:dev": "dotenv -e .env.local -- turbo run console:dev",
"console:storybook": "dotenv -e .env.local -- turbo run console:storybook",
"rotor:dev": "dotenv -e .env.local -- turbo run rotor:dev",
"console:start": "dotenv -e .env.local -- pnpm run --filter=console console:start",
"console:start": "dotenv -e .env.local -- turbo run console:start",
"console:db-prepare": "dotenv -e .env.local -- pnpm run --filter=console db:update-schema",
"console:db-prepare-force": "dotenv -e .env.local -- pnpm run --filter=console db:update-schema-force",
"dev": "dotenv -e .env.local -- turbo run dev",
Expand Down
7 changes: 7 additions & 0 deletions types/protocols/analytics.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ export type RuntimeFacade = {
pageTitle(): string | undefined;
};

export type ErrorHandler = (error: any) => void;

export type JitsuOptions = {
/**
* API Key. Optional. If not set, Jitsu will send event to the server without auth, and server
Expand Down Expand Up @@ -372,6 +374,11 @@ export type JitsuOptions = {
*/
idEndpoint?: string;

/**
* What to do with errors. It can log it, rethrow or run a custom handler. Default value: "log"
*/
errorPolicy?: ErrorHandler | "rethrow" | "log";

privacy?: {
dontSend?: boolean;
disableUserIds?: boolean;
Expand Down
2 changes: 1 addition & 1 deletion webapps/console/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"prisma": "prisma",
"lint": "next lint",
"console:dev": "next dev",
"dev:rotor": "ts-node service/rotor.ts",
"console:start": "next start",
"dev:rotor": "ts-node service/rotor.ts",
"build": "prisma generate && next build",
"clean": "rm -rf ./.next ./.turbo",
"compile": "tsc -p .",
Expand Down

0 comments on commit 38c66c8

Please sign in to comment.