Is there a way to customize error messages: Save failed: user with the same identity already exists?

I'd like to add something helpful to tell them "perhaps you used a different login method" or something to that effect.
6 Replies
kapa.ai
kapa.ai4w ago
Hi @rachelslurs. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Based on the knowledge sources, I can provide some guidance on customizing error messages in Wasp's authentication flow. Current Error Handling The knowledge sources show that Wasp has improved error messaging in recent versions. According to a Discord support thread, when you signup with an existing user, "the response body will contain an extra message for you." However, the same thread indicates that you can't easily get "lower level" access to catch auth errors yourself unless you create a custom signup action. Customizing Error Messages To customize error messages like "Save failed: user with the same identity already exists," you have a couple of options: 1. Use HttpError in Custom Actions: If you're implementing custom authentication actions, you can throw custom HttpError instances with your own messages. According to the error handling documentation:
import { HttpError } from 'wasp/server'

export const createTask = async (args, context) => {
throw new HttpError(
403, // status code
"You can't do this!", // message
{ foo: 'bar' } // data
)
}
import { HttpError } from 'wasp/server'

export const createTask = async (args, context) => {
throw new HttpError(
403, // status code
"You can't do this!", // message
{ foo: 'bar' } // data
)
}
For 4xx status codes, the client will receive the message and data fields you provide. 2. Handle Prisma Errors: The knowledge sources mention a pattern used in Wasp's built-in signup that catches Prisma errors and converts them to appropriate HTTP errors. From a GitHub issue discussion:
try {
await prisma.user.create({ data: userFields })
} catch (e) {
if (e instanceof AuthError) {
throw new HttpError(422, 'Validation failed', { message: e.message })
} else if (isPrismaError(e)) {
throw prismaErrorToHttpError(e)
} else {
throw new HttpError(500)
}
}
try {
await prisma.user.create({ data: userFields })
} catch (e) {
if (e instanceof AuthError) {
throw new HttpError(422, 'Validation failed', { message: e.message })
} else if (isPrismaError(e)) {
throw prismaErrorToHttpError(e)
} else {
throw new HttpError(500)
}
}
Limitations The knowledge sources indicate that there's ongoing work to improve error messaging customization. A GitHub issue was created to make signup forms more flexible for customization, though it notes this won't be tackled immediately. For your specific use case of detecting duplicate identity errors and suggesting alternative login methods, you would likely need to implement a custom signup action that catches the specific error and returns a more helpful message using the HttpError pattern shown above. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @rachelslurs, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
rachelslurs
rachelslursOP4w ago
@kapa.ai awesome this thread https://discord.com/channels/686873244791210014/1189296190924267551/1192590518648066209 was very helpful. has there been any more recent solutions or findings shared on customizing error messages like that or does the handle prisma errors solution cover it?
kapa.ai
kapa.ai4w ago
Let me check the knowledge base for you again. One moment! :books:
MEE6
MEE64w ago
Wohooo @rachelslurs, you just became a Waspeteer level 3!
kapa.ai
kapa.ai4w ago
---------------------- Based on the knowledge sources provided, I can see that the Discord thread you referenced is from December 2023, and the documents I have access to don't show any more recent solutions beyond what was discussed in that thread. Current State of Error Message Customization The knowledge sources indicate that the main approaches for customizing error messages remain: 1. Creating Custom Actions - As discussed in the Discord thread, you can create custom signup actions and handle Prisma errors directly. This allows you to catch specific errors and return custom messages. 2. Using the Prisma Error Handling Pattern - The pattern shown in the GitHub issue for catching and converting Prisma errors to HTTP errors is still the recommended approach:
try {
await prisma.user.create({ data: userFields })
} catch (e) {
if (e instanceof AuthError) {
throw new HttpError(422, 'Validation failed', { message: e.message })
} else if (isPrismaError(e)) {
throw prismaErrorToHttpError(e)
} else {
throw new HttpError(500)
}
}
try {
await prisma.user.create({ data: userFields })
} catch (e) {
if (e instanceof AuthError) {
throw new HttpError(422, 'Validation failed', { message: e.message })
} else if (isPrismaError(e)) {
throw prismaErrorToHttpError(e)
} else {
throw new HttpError(500)
}
}
No Newer Solutions Found The knowledge sources don't contain information about any more recent solutions or improvements to error message customization beyond what was discussed in that December 2023 thread. The GitHub issue #1617 mentioned in the thread for making signup forms more flexible was created but noted it wouldn't be tackled immediately. The Prisma error handling solution you mentioned should cover most use cases for customizing error messages when working with custom authentication actions. If you need more specific functionality, you would still need to implement custom actions as described in that thread. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @rachelslurs, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
Carlos
Carlos4w ago
Hi @rachelslurs, you can check this yourself in an Auth Hook, and return a spercific error: https://wasp.sh/docs/auth/auth-hooks#executing-code-before-the-user-signs-up However, be mindful that the current general guideline is not to say if the user already exists. It is considered a privacy breach, plus it could ease the amount of work in some attacks should someone target your app. Another approach would be to do user merging (so that someone can log in to the same account with multiple identities). We have a demo project here and you can copy the approach: https://github.com/wasp-lang/account-merging-example/

Did you find this page helpful?