Repository navigation
Record why an email send was rejected or failed #1235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,7 +46,12 @@ export async function sendEmailHandler(request: NextRequest): Promise<NextRespon | |
| { status: "error", error: result.error }, | ||
| { status: 502, headers: getCorsHeaders() }, | ||
| ); | ||
| attempt = { status: "send_failed", accountId, chatId: chat_id }; | ||
| attempt = { | ||
| status: "send_failed", | ||
| accountId, | ||
| chatId: chat_id, | ||
| reason: `resend_error: ${result.error}`, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: The logged Resend reason is always generic with the real adapter, so rate limits, validation failures, and other provider causes cannot be distinguished in Prompt for AI agents |
||
| }; | ||
| } else { | ||
| response = NextResponse.json( | ||
| { success: true, message: result.message, id: result.id }, | ||
|
|
@@ -56,7 +61,7 @@ export async function sendEmailHandler(request: NextRequest): Promise<NextRespon | |
| } | ||
| } else { | ||
| response = validated.error; | ||
| attempt = { status: "rejected" }; | ||
| attempt = { status: "rejected", accountId: validated.accountId, reason: validated.reason }; | ||
| } | ||
|
|
||
| await logEmailAttempt({ rawBody: validated.rawBody, ...attempt }); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,10 +41,12 @@ export type ValidatedSendEmailRequest = Omit<SendEmailBody, "to" | "subject"> & | |
| /** | ||
| * Validation outcome. Always carries `rawBody` — the request body as received — | ||
| * so the handler can record it in `email_send_log` on both the rejected and the | ||
| * accepted paths without re-reading the request. | ||
| * accepted paths without re-reading the request. A rejection also carries | ||
| * `reason` (a short code, then detail) and the account once auth has resolved | ||
| * it, so the rejection rate can be grouped by cause (recoupable/app#1949 F2). | ||
| */ | ||
| export type ValidateSendEmailResult = | ||
| | { rawBody: string; error: NextResponse } | ||
| | { rawBody: string; error: NextResponse; reason: string; accountId?: string } | ||
| | { rawBody: string; data: ValidatedSendEmailRequest }; | ||
|
|
||
| /** | ||
|
|
@@ -54,7 +56,7 @@ export type ValidateSendEmailResult = | |
| * `subject` is derived from the body when omitted. Always returns `rawBody`. | ||
| * | ||
| * @param request - The NextRequest object | ||
| * @returns `{ rawBody, error }` if validation/auth/recipients fail, else `{ rawBody, data }`. | ||
| * @returns `{ rawBody, error, reason }` if validation/auth/recipients fail, else `{ rawBody, data }`. | ||
| */ | ||
| export async function validateSendEmailBody( | ||
| request: NextRequest, | ||
|
|
@@ -72,18 +74,20 @@ export async function validateSendEmailBody( | |
| const result = sendEmailBodySchema.safeParse(parsed); | ||
| if (!result.success) { | ||
| const firstError = result.error.issues[0]; | ||
| const field = firstError.path.join("."); | ||
| return { | ||
| rawBody, | ||
| error: NextResponse.json( | ||
| { status: "error", missing_fields: firstError.path, error: firstError.message }, | ||
| { status: 400, headers: getCorsHeaders() }, | ||
| ), | ||
| reason: `invalid_body: ${field ? `${field}: ` : ""}${firstError.message}`, | ||
| }; | ||
| } | ||
|
|
||
| const authContext = await validateAuthContext(request, { accountId: result.data.account_id }); | ||
| if (authContext instanceof NextResponse) { | ||
| return { rawBody, error: authContext }; | ||
| return { rawBody, error: authContext, reason: `auth_failed: ${authContext.status}` }; | ||
| } | ||
|
|
||
| let to = result.data.to ?? []; | ||
|
|
@@ -97,6 +101,8 @@ export async function validateSendEmailBody( | |
| { status: "error", error: "No email address found for the authenticated account." }, | ||
| { status: 400, headers: getCorsHeaders() }, | ||
| ), | ||
| reason: "no_account_email", | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Prompt for AI agents |
||
| accountId: authContext.accountId, | ||
| }; | ||
| } | ||
| } | ||
|
|
@@ -116,6 +122,8 @@ export async function validateSendEmailBody( | |
| }, | ||
| { status: 403, headers: getCorsHeaders() }, | ||
| ), | ||
| reason: `recipient_not_allowed: ${recipientCheck.disallowed.join(", ")}`, | ||
| accountId: authContext.accountId, | ||
| }; | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: These new error-path tests never assert the returned
NextResponseenvelope, so they do not protect the caller-facing rejection contract. Assert theerrorbranch, expected status, and parsed{ status: "error", error: expect.any(String) }body in each rejection test (includingdisallowed_recipientsfor the 403 case).(Based on your team's feedback about asserting validator error envelopes.) [3bbabfba-f1c6-4e42-9d18-d0036968bf66]
Prompt for AI agents