payloadcms / payload

Payload is the open-source, fullstack Next.js framework, giving you instant backend superpowers. Get a full TypeScript backend and admin panel instantly. Use Payload as a headless CMS or for building powerful applications.
https://payloadcms.com
MIT License
23.12k stars 1.44k forks source link

Toast shows generic message from list view in admin panel #7214

Closed wrobson-lllow closed 1 month ago

wrobson-lllow commented 1 month ago

Link to reproduction

No response

Describe the Bug

We have a "CustomeAPIError" class used to throw custom error toasts from hooks:

import {APIError} from "payload/errors";

class CustomAPIError extends APIError {
    constructor(message: string) {
        super(message, 400, undefined, true)
    }
}

export default CustomAPIError

These are used mainly for displaying descriptive error messages to the user when deleting records from a collection. It works as expected when deleting a record from the individual record view.

However, when deleting from the list view the custom error message is not displayed. Instead a generic "Unable to delete n out of n records" is displayed. image image

To Reproduce

Throw a CustomAPIError from a beforeDelete hook.

Currently using v2.22.0 but this has been an issue in all v2 versions.

Payload Version

2.22.0

Adapters and Plugins

bundler-webpack, db-mongodb, plugin-cloud, richtext-slate

JarrodMFlesch commented 1 month ago

@wrobson-lllow could you throw together a simple reproduction so we can drop in some code to replicate? Will make looking into it a lot easier.

wrobson-lllow commented 1 month ago

@wrobson-lllow could you throw together a simple reproduction so we can drop in some code to replicate? Will make looking into it a lot easier.

Sure thing. Is there a simple starter repo anywhere that I can use?

Never mind found it.

wrobson-lllow commented 1 month ago

@JarrodMFlesch reproduction branch custom-API-error-list-view-bug

One thing I've noticed and wondered about before, is there a reason the beforeDelete hook doesn't include 'doc' in the args object?

To reproduce, delete a record from the posts collection. If this is done from the 'record view' a toast error with the message "You must select the delete checkbox before deleting this post" will be displayed. If this is done from the 'list view' the toast error will display "Unable to delete n out of n posts"

paulpopus commented 1 month ago

Coming out with a fix for both v2 and v3. In v3 it will look a bit like this and in v2 it will just be appended to the end of the error.

image

github-actions[bot] commented 1 day ago

This issue has been automatically locked. Please open a new issue if this issue persists with any additional detail.