// A shop's failed payments and slow imports reported to Sentry, with the request and the user,
// so the team sees them grouped as issues.
//
//   SENTRY_DSN=https://<key>@o1.ingest.sentry.io/42 quillo run sentry.qwe      then open http://localhost:8080/pay
//
// Without SENTRY_DSN, a DSN for SINK_URL (a stand-in server, in tests) is used.

use "sentry.qwer"

let DSN = env("SENTRY_DSN", null) ?? replace(env("SINK_URL", "http://127.0.0.1:9"), "://", "://public123@") + "/42"

route GET /pay {
    // The payment provider said no: report it, and answer the customer.
    let r = sentry_error(DSN, "PaymentDeclined", "card_declined: insufficient funds for order A-1042", {
        tags: { provider: "stripe", order: "A-1042" },
        user: { id: "u42", email: "ada@example.org" },
        request: { url: "https://shop.example.com/pay", method: "POST", headers: request.headers },
        release: "shop@2.4.0",
        environment: "production",
        stacktrace: [{ function: "route POST /pay", filename: "shop.qwe", lineno: 88 }, { function: "charge", filename: "payments.qwe", lineno: 41 }],
        breadcrumbs: [{ category: "cart", message: "3 items, €52.90", level: "info" }],
    })?
    { reported: r.ok, status: r.status, event_id_length: len(r.event_id) }
}

route GET /slow = sentry_message(DSN, "nightly import took 14 minutes", "warning", { tags: { job: "import" } })?

route GET /dsn = sentry_dsn("https://abc123@o450000.ingest.sentry.io/4500000000000001")?
