61 lines
1.7 KiB
TypeScript
61 lines
1.7 KiB
TypeScript
import * as Sentry from "@sentry/react-native";
|
|
import { NetworkFailureKind } from "./types";
|
|
|
|
const SLOW_SUCCESS_SAMPLE_RATE = 0;
|
|
|
|
export const truncateResponseHead = (value: unknown) =>
|
|
typeof value === "string" ? value.slice(0, 300) : value;
|
|
|
|
export function reportDataFetchSuccess(args: {
|
|
tags: Record<string, string>;
|
|
context: Record<string, unknown>;
|
|
slow: boolean;
|
|
}) {
|
|
Sentry.addBreadcrumb({
|
|
category: "data_fetch",
|
|
level: args.slow ? "warning" : "info",
|
|
message: args.slow ? "fetch:slow_success" : "fetch:success",
|
|
data: args.context,
|
|
});
|
|
|
|
if (args.slow && SLOW_SUCCESS_SAMPLE_RATE > 0 && Math.random() < SLOW_SUCCESS_SAMPLE_RATE) {
|
|
Sentry.captureMessage("data_fetch.slow_success", {
|
|
level: "warning",
|
|
tags: args.tags,
|
|
contexts: { data_fetch: args.context },
|
|
});
|
|
}
|
|
}
|
|
|
|
export function reportDataFetchFailure(args: {
|
|
kind: NetworkFailureKind;
|
|
error: unknown;
|
|
tags: Record<string, string>;
|
|
context: Record<string, unknown>;
|
|
capture?: boolean;
|
|
}) {
|
|
Sentry.addBreadcrumb({
|
|
category: "data_fetch",
|
|
level: args.kind === "aborted" ? "info" : "error",
|
|
message: "fetch:" + args.kind,
|
|
data: args.context,
|
|
});
|
|
|
|
if (args.kind === "aborted" || args.capture === false) return;
|
|
|
|
const level = args.kind === "json_parse_error" ? "error" : "warning";
|
|
const captureError = args.error instanceof Error ? args.error : new Error(String(args.error));
|
|
|
|
Sentry.captureException(captureError, {
|
|
level,
|
|
tags: args.tags,
|
|
contexts: {
|
|
data_fetch: {
|
|
kind: args.kind,
|
|
...args.context,
|
|
},
|
|
},
|
|
fingerprint: ["data_fetch", args.kind, args.tags.endpoint ?? "unknown"],
|
|
});
|
|
}
|