1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-09-18 15:05:32 -04:00

sentry instrumentation

This commit is contained in:
2025-03-29 20:37:28 -04:00
parent 4f5bc129b6
commit 87a24a98f0
21 changed files with 2244 additions and 333 deletions

View File

@@ -3,6 +3,7 @@
import { headers } from "next/headers";
import { z } from "zod";
import { Resend } from "resend";
import * as Sentry from "@sentry/nextjs";
import * as config from "../../lib/config";
const schema = z.object({
@@ -22,71 +23,81 @@ export const sendMessage = async (
errors?: z.inferFormattedError<typeof schema>;
payload?: FormData;
}> => {
try {
const validatedFields = schema.safeParse(Object.fromEntries(formData));
return await Sentry.withServerActionInstrumentation(
"sendMessage",
{
formData,
headers: headers(),
recordResponse: true,
},
async () => {
try {
const validatedFields = schema.safeParse(Object.fromEntries(formData));
// backup to client-side validations just in case someone squeezes through without them
if (!validatedFields.success) {
console.debug("[contact form] validation error:", validatedFields.error.flatten());
// backup to client-side validations just in case someone squeezes through without them
if (!validatedFields.success) {
console.debug("[contact form] validation error:", validatedFields.error.flatten());
return {
success: false,
message: "Please make sure that all fields are properly filled in.",
errors: validatedFields.error.format(),
payload: formData,
};
return {
success: false,
message: "Please make sure that all fields are properly filled in.",
errors: validatedFields.error.format(),
payload: formData,
};
}
// validate captcha
const turnstileResponse = await fetch("https://challenges.cloudflare.com/turnstile/v0/siteverify", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
secret: process.env.TURNSTILE_SECRET_KEY || "1x0000000000000000000000000000000AA",
response: validatedFields.data["cf-turnstile-response"],
remoteip: (await headers()).get("x-forwarded-for") || "",
}),
cache: "no-store",
signal: AbortSignal.timeout(5000), // 5 second timeout
});
if (!turnstileResponse.ok) {
throw new Error(`[contact form] turnstile validation failed: ${turnstileResponse.status}`);
}
const turnstileData = (await turnstileResponse.json()) as { success: boolean };
if (!turnstileData.success) {
return {
success: false,
message: "Did you complete the CAPTCHA? (If you're human, that is...)",
payload: formData,
};
}
if (!process.env.RESEND_FROM_EMAIL) {
console.warn("[contact form] RESEND_FROM_EMAIL not set, falling back to onboarding@resend.dev.");
}
// send email
const resend = new Resend(process.env.RESEND_API_KEY);
await resend.emails.send({
from: `${validatedFields.data.name} <${process.env.RESEND_FROM_EMAIL ?? "onboarding@resend.dev"}>`,
replyTo: `${validatedFields.data.name} <${validatedFields.data.email}>`,
to: [config.authorEmail],
subject: `[${config.siteName}] Contact Form Submission`,
text: validatedFields.data.message,
});
return { success: true, message: "Thanks! You should hear from me soon.", payload: formData };
} catch (error) {
Sentry.captureException(error);
return {
success: false,
message: "Internal server error... Try again later or shoot me an old-fashioned email?",
errors: error instanceof z.ZodError ? error.format() : undefined,
payload: formData,
};
}
}
// validate captcha
const turnstileResponse = await fetch("https://challenges.cloudflare.com/turnstile/v0/siteverify", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
secret: process.env.TURNSTILE_SECRET_KEY || "1x0000000000000000000000000000000AA",
response: validatedFields.data["cf-turnstile-response"],
remoteip: (await headers()).get("x-forwarded-for") || "",
}),
cache: "no-store",
signal: AbortSignal.timeout(5000), // 5 second timeout
});
if (!turnstileResponse.ok) {
throw new Error(`[contact form] turnstile validation failed: ${turnstileResponse.status}`);
}
const turnstileData = (await turnstileResponse.json()) as { success: boolean };
if (!turnstileData.success) {
return {
success: false,
message: "Did you complete the CAPTCHA? (If you're human, that is...)",
payload: formData,
};
}
if (!process.env.RESEND_FROM_EMAIL) {
console.warn("[contact form] RESEND_FROM_EMAIL not set, falling back to onboarding@resend.dev.");
}
// send email
const resend = new Resend(process.env.RESEND_API_KEY);
await resend.emails.send({
from: `${validatedFields.data.name} <${process.env.RESEND_FROM_EMAIL ?? "onboarding@resend.dev"}>`,
replyTo: `${validatedFields.data.name} <${validatedFields.data.email}>`,
to: [config.authorEmail],
subject: `[${config.siteName}] Contact Form Submission`,
text: validatedFields.data.message,
});
} catch (error) {
console.error("[contact form] fatal error:", error);
return {
success: false,
message: "Internal server error... Try again later or shoot me an old-fashioned email?",
errors: error instanceof z.ZodError ? error.format() : undefined,
payload: formData,
};
}
return { success: true, message: "Thanks! You should hear from me soon.", payload: formData };
);
};

View File

@@ -2,7 +2,6 @@ import PageTitle from "../../components/PageTitle";
import Link from "../../components/Link";
import ContactForm from "./form";
import { addMetadata } from "../../lib/helpers/metadata";
import type { Route } from "next";
export const metadata = addMetadata({
title: "Contact Me",
@@ -29,7 +28,7 @@ const Page = () => {
</p>
<p>
🔐 You can grab my public key here:{" "}
<Link href={"/pubkey.asc" as Route} title="My Public PGP Key" rel="pgpkey authn" openInNewTab>
<Link href="/pubkey.asc" title="My Public PGP Key" rel="pgpkey authn" openInNewTab>
<code style={{ fontSize: "0.925em", letterSpacing: "0.075em", wordSpacing: "-0.3em" }}>
6BF3 79D3 6F67 1480 2B0C 9CF2 51E6 9A39
</code>