-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix: unified error notification system for user-fixable errors #1183
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 |
|---|---|---|
| @@ -1,14 +1,16 @@ | ||
| import prisma from "@/utils/prisma"; | ||
| import { createScopedLogger } from "@/utils/logger"; | ||
| import type { Logger } from "@/utils/logger"; | ||
| import { captureException } from "@/utils/error"; | ||
|
|
||
| const logger = createScopedLogger("error-messages"); | ||
| import { sendActionRequiredEmail } from "@inboxzero/resend"; | ||
| import { env } from "@/env"; | ||
| import { createUnsubscribeToken } from "@/utils/unsubscribe"; | ||
|
|
||
| // Used to store error messages for a user which we display in the UI | ||
|
|
||
| type ErrorMessageEntry = { | ||
| message: string; | ||
| timestamp: string; | ||
| emailSentAt?: string; | ||
| }; | ||
|
|
||
| type ErrorMessages = Record<string, ErrorMessageEntry>; | ||
|
|
@@ -27,10 +29,11 @@ export async function addUserErrorMessage( | |
| userId: string, | ||
| errorType: (typeof ErrorType)[keyof typeof ErrorType], | ||
| errorMessage: string, | ||
| logger: Logger, | ||
| ): Promise<void> { | ||
| const user = await prisma.user.findUnique({ where: { id: userId } }); | ||
| if (!user) { | ||
| logger.warn("User not found", { userId }); | ||
| logger.warn("User not found"); | ||
| return; | ||
| } | ||
|
|
||
|
|
@@ -52,20 +55,57 @@ export async function addUserErrorMessage( | |
|
|
||
| export async function clearUserErrorMessages({ | ||
| userId, | ||
| logger, | ||
| }: { | ||
| userId: string; | ||
| logger: Logger; | ||
| }): Promise<void> { | ||
| try { | ||
| await prisma.user.update({ | ||
| where: { id: userId }, | ||
| data: { errorMessages: {} }, | ||
| }); | ||
| } catch (error) { | ||
| logger.error("Error clearing user error messages:", { | ||
| logger.error("Error clearing user error messages:", { error }); | ||
| captureException(error, { extra: { userId } }); | ||
| } | ||
| } | ||
|
|
||
| export async function clearSpecificErrorMessages({ | ||
| userId, | ||
| errorTypes, | ||
| logger, | ||
| }: { | ||
| userId: string; | ||
| errorTypes: (typeof ErrorType)[keyof typeof ErrorType][]; | ||
| logger: Logger; | ||
| }): Promise<void> { | ||
| try { | ||
| const user = await prisma.user.findUnique({ | ||
| where: { id: userId }, | ||
| select: { errorMessages: true }, | ||
| }); | ||
|
|
||
| if (!user) return; | ||
|
|
||
| const currentErrorMessages = (user.errorMessages as ErrorMessages) || {}; | ||
| const updatedErrorMessages = { ...currentErrorMessages }; | ||
|
|
||
| for (const errorType of errorTypes) { | ||
| delete updatedErrorMessages[errorType]; | ||
| } | ||
|
|
||
| await prisma.user.update({ | ||
| where: { id: userId }, | ||
| data: { errorMessages: updatedErrorMessages }, | ||
| }); | ||
| } catch (error) { | ||
| logger.error("Error clearing specific error messages:", { | ||
| userId, | ||
| errorTypes, | ||
| error, | ||
| }); | ||
| captureException(error, { extra: { userId } }); | ||
| captureException(error, { extra: { userId, errorTypes } }); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -77,3 +117,121 @@ export const ErrorType = { | |
| ANTHROPIC_INSUFFICIENT_BALANCE: "Anthropic insufficient balance", | ||
| ACCOUNT_DISCONNECTED: "Account disconnected", | ||
| }; | ||
|
|
||
| const errorTypeConfig: Record< | ||
| (typeof ErrorType)[keyof typeof ErrorType], | ||
| { label: string; actionUrl: string; actionLabel: string } | ||
| > = { | ||
| [ErrorType.INCORRECT_OPENAI_API_KEY]: { | ||
| label: "API Key Issue", | ||
| actionUrl: "/settings", | ||
| actionLabel: "Update API Key", | ||
| }, | ||
| [ErrorType.INVALID_OPENAI_MODEL]: { | ||
| label: "Invalid AI Model", | ||
| actionUrl: "/settings", | ||
| actionLabel: "Update Settings", | ||
| }, | ||
| [ErrorType.OPENAI_API_KEY_DEACTIVATED]: { | ||
| label: "API Key Deactivated", | ||
| actionUrl: "/settings", | ||
| actionLabel: "Update API Key", | ||
| }, | ||
| [ErrorType.OPENAI_RETRY_ERROR]: { | ||
| label: "API Quota Exceeded", | ||
| actionUrl: "/settings", | ||
| actionLabel: "Update Settings", | ||
| }, | ||
| [ErrorType.ANTHROPIC_INSUFFICIENT_BALANCE]: { | ||
| label: "Insufficient Credits", | ||
| actionUrl: "/settings", | ||
| actionLabel: "Update Settings", | ||
| }, | ||
| [ErrorType.ACCOUNT_DISCONNECTED]: { | ||
| label: "Account Disconnected", | ||
| actionUrl: "/accounts", | ||
| actionLabel: "Reconnect Account", | ||
| }, | ||
| }; | ||
|
|
||
| export async function addUserErrorMessageWithNotification({ | ||
| userId, | ||
| userEmail, | ||
| emailAccountId, | ||
| errorType, | ||
| errorMessage, | ||
| logger, | ||
| }: { | ||
| userId: string; | ||
| userEmail: string; | ||
| emailAccountId: string; | ||
| errorType: (typeof ErrorType)[keyof typeof ErrorType]; | ||
| errorMessage: string; | ||
| logger: Logger; | ||
| }): Promise<void> { | ||
| try { | ||
| const user = await prisma.user.findUnique({ | ||
| where: { id: userId }, | ||
| select: { errorMessages: true }, | ||
| }); | ||
|
|
||
| if (!user) { | ||
| logger.warn("User not found"); | ||
| return; | ||
| } | ||
|
|
||
| const currentErrorMessages = (user.errorMessages as ErrorMessages) || {}; | ||
| const existingEntry = currentErrorMessages[errorType]; | ||
| const shouldSendEmail = !existingEntry?.emailSentAt; | ||
|
|
||
| const newEntry: ErrorMessageEntry = { | ||
| message: errorMessage, | ||
| timestamp: new Date().toISOString(), | ||
| emailSentAt: existingEntry?.emailSentAt, | ||
| }; | ||
|
|
||
| if (shouldSendEmail) { | ||
| try { | ||
| const config = errorTypeConfig[errorType]; | ||
| const unsubscribeToken = await createUnsubscribeToken({ | ||
| emailAccountId, | ||
| }); | ||
|
|
||
| await sendActionRequiredEmail({ | ||
| from: env.RESEND_FROM_EMAIL, | ||
| to: userEmail, | ||
| emailProps: { | ||
| baseUrl: env.NEXT_PUBLIC_BASE_URL, | ||
| email: userEmail, | ||
| unsubscribeToken, | ||
| errorType: config.label, | ||
| errorMessage, | ||
| actionUrl: config.actionUrl, | ||
| actionLabel: config.actionLabel, | ||
| }, | ||
| }); | ||
|
|
||
| newEntry.emailSentAt = new Date().toISOString(); | ||
| logger.info("Sent action required email", { errorType }); | ||
|
Comment on lines
+193
to
+215
Contributor
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. When Resend is not configured ( Finding type: |
||
| } catch (emailError) { | ||
| logger.error("Failed to send action required email", { | ||
| error: emailError, | ||
| }); | ||
| // Continue to save the error message even if email fails | ||
| } | ||
| } | ||
|
elie222 marked this conversation as resolved.
|
||
|
|
||
| const newErrorMessages = { | ||
| ...currentErrorMessages, | ||
| [errorType]: newEntry, | ||
| }; | ||
|
|
||
| await prisma.user.update({ | ||
| where: { id: userId }, | ||
| data: { errorMessages: newErrorMessages }, | ||
| }); | ||
| } catch (error) { | ||
| logger.error("Error in addUserErrorMessageWithNotification", { error }); | ||
| captureException(error, { extra: { userId, errorType } }); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.