1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-10-26 01:05:49 -04:00

extract sentry config/init

This commit is contained in:
2022-02-12 11:24:31 -05:00
parent e4cd233530
commit cb28562869
7 changed files with 89 additions and 68 deletions

View File

@@ -1,13 +1,8 @@
import * as Sentry from "@sentry/node";
import fetch from "node-fetch";
import queryString from "query-string";
import { logServerError } from "../../lib/sentry";
import type { NextApiRequest, NextApiResponse } from "next";
Sentry.init({
dsn: process.env.SENTRY_DSN || process.env.NEXT_PUBLIC_SENTRY_DSN || "",
environment: process.env.NODE_ENV || process.env.VERCEL_ENV || process.env.NEXT_PUBLIC_VERCEL_ENV || "",
});
// fallback to dummy secret for testing: https://docs.hcaptcha.com/#integration-testing-test-keys
const HCAPTCHA_SITE_KEY =
process.env.HCAPTCHA_SITE_KEY || process.env.NEXT_PUBLIC_HCAPTCHA_SITE_KEY || "10000000-ffff-ffff-ffff-000000000001";
@@ -56,15 +51,12 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
// success! let the client know
return res.status(200).json({ success: true });
} catch (error) {
console.error(error);
// extract just the error message to send back to client
const message = error instanceof Error ? error.message : "UNKNOWN_EXCEPTION";
// don't log PEBCAK errors to sentry
// log errors (except PEBCAK) to console and sentry
if (!message.startsWith("USER_")) {
// log error to sentry, give it 2 seconds to finish sending
Sentry.captureException(error);
await Sentry.flush(2000);
await logServerError(error);
}
// 500 Internal Server Error

View File

@@ -1,14 +1,9 @@
import * as Sentry from "@sentry/node";
import pRetry from "p-retry";
import faunadb from "faunadb";
import { getAllNotes } from "../../lib/parse-notes";
import { logServerError } from "../../lib/sentry";
import type { NextApiRequest, NextApiResponse } from "next";
Sentry.init({
dsn: process.env.SENTRY_DSN || process.env.NEXT_PUBLIC_SENTRY_DSN || "",
environment: process.env.NODE_ENV || process.env.VERCEL_ENV || process.env.NEXT_PUBLIC_VERCEL_ENV || "",
});
type PageStats = {
slug: string;
hits: number;
@@ -57,14 +52,12 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return res.status(200).json(siteStats);
}
} catch (error) {
console.error(error);
// log error to sentry, give it 2 seconds to finish sending
Sentry.captureException(error);
await Sentry.flush(2000);
// extract just the error message to send back to client
const message = error instanceof Error ? error.message : "Unknown error.";
// log full error to console and sentry
await logServerError(error);
// 500 Internal Server Error
return res.status(500).json({ success: false, message });
}