Is there a way to create a toast error message on the frontend from within a doc, or field hook?

default discord avatar
Zephury
last month
2

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"?

  • discord user avatar
    denolfe
    Payload Team
    last month

    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')
    CleanShot_2023-10-20_at_20.39.27.png
  • default discord avatar
    Zephury
    last month

    Thank you 👍🏻

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.