I want to ensure that a page can only be created from the default locale, as I need to ensure it's default pathname is created, before any sort of localized pathname is added.
I'd like to do something like this:
if (operation === "create" && isDefaultLocale) {
throw new Error("Pages must be created within the default locale.");
}
However, that error only shows in the server's console. Is there a way I can send that error message to the client, in order to be displayed within a toast message/notification, instead of saying a non-descriptive "Something went wrong"?
In order for an error to show up in the toast notification it must have the
isPublic
property on it.
The easiest way to do this would be to extend our existing
APIError
with a new class, then throw that class. Here is an example:
import { APIError } from 'payload/errors'
class MySpecialError extends APIError {
constructor(message: string) {
super(message, 400, undefined, true)
}
}
The 4th parameter is the
isPublic
value.
Then throw as you'd expect
throw new MySpecialError('Cannot create in default locale')
Thank you 👍🏻
Star
Discord
online
Get help straight from the Payload team with an Enterprise License.