ወደ ትምህርቱ ይመለሱ

የመጀመሪያው የሚሠራ ፋይል

worked-solution/app.js

ኮዱ እና የሚሠሩ ምሳሌ ገጾች የመጀመሪያ መለያዎቻቸውንና የምሳሌ ጽሑፋቸውን ይይዛሉ። ትምህርቱ እና የፋይል መመሪያዎች በእርስዎ ቋንቋ ያብራሯቸዋል።

import { SENTRY_LOADER_URL, RELEASE } from "./config.js";
const status = document.querySelector("[role=status]");
document.querySelector("#monitor").addEventListener("click", () => {
  let url;
  try {
    url = new URL(SENTRY_LOADER_URL);
    if (
      url.protocol !== "https:" ||
      url.hostname !== "js.sentry-cdn.com" ||
      url.href.includes("REPLACE")
    )
      throw Error();
  } catch {
    status.textContent = "Configure your own Sentry project loader URL first.";
    return;
  }
  if (document.querySelector("#sentry-loader")) return;
  window.sentryOnLoad = () => {
    Sentry.init({
      release: RELEASE,
      environment: "practice",
      sendDefaultPii: false,
      tracesSampleRate: 0,
      replaysSessionSampleRate: 0,
      replaysOnErrorSampleRate: 0,
      beforeSend(event) {
        const value = event.exception?.values?.find(
          (v) => v.value === "Practice: missing contact target",
        );
        if (!value) {
          document.querySelector("[role=status]").textContent =
            "Practice monitoring filtered this event: it does not match the fixed synthetic allowlist. No diagnostic was sent.";
          return null;
        }
        const frames = (value.stacktrace?.frames || []).map((f) => {
          let filename;
          try {
            const u = new URL(f.filename, location.origin);
            filename = ["http:", "https:"].includes(u.protocol)
              ? u.origin + u.pathname
              : "redacted";
          } catch {
            filename = "redacted";
          }
          return {
            filename,
            function: f.function,
            lineno: f.lineno,
            colno: f.colno,
            in_app: f.in_app,
          };
        });
        return {
          event_id: event.event_id,
          timestamp: event.timestamp,
          platform: "javascript",
          level: "error",
          release: RELEASE,
          environment: "practice",
          exception: {
            values: [
              {
                type: "Error",
                value: "Practice: missing contact target",
                stacktrace: { frames },
              },
            ],
          },
        };
      },
    });
    status.textContent =
      "Monitoring loaded. Generate the synthetic practice error and verify receipt in Sentry.";
  };
  const script = document.createElement("script");
  script.id = "sentry-loader";
  script.src = url.href;
  script.crossOrigin = "anonymous";
  script.dataset.lazy = "no"; // documented data-lazy="no" requests the full SDK early
  script.onerror = () =>
    (status.textContent = "Monitoring could not load. The page remains usable.");
  document.head.append(script);
});
document.querySelector("#contact-action").addEventListener("click", () => {
  try {
    const target = document.querySelector("#contact");
    if (!target) throw Error("Missing contact element");
    target.scrollIntoView();
    status.textContent = "Contact section opened.";
  } catch (error) {
    status.textContent = "Contact action failed. This is the deliberate practice bug.";
    if (window.Sentry) {
      const safe = new Error("Practice: missing contact target");
      safe.stack = error.stack;
      Sentry.captureException(safe);
    }
  }
});