Custom error response from collection access control #12267
Fitch24
started this conversation in
Feature Requests & Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
In Payload v2, I could throw an
APIError
(or custom errors inheriting from theAPIError
class) with a custom status code and message. For example, I could return a 401 status for unauthenticated users, 403 if the user lacked the required permissions, or 404 to hide certain resources from specific users.However, after migrating to Payload v3, throwing errors in a collection access hook breaks the admin panel. This happens because the admin panel calls each access hook to determine user permissions for each operation in every collection, and the Next.js app crashes if any error is thrown (though REST API requests still work as expected, like in v2).
Currently, I’ve solved this problem by creating a wrapper around the
access
function:But maybe it would be better to have some built-in functionality for this use case — for example, the ability to detect whether an
access hook
(or even the entirereq
) was invoked from the admin panel?Beta Was this translation helpful? Give feedback.
All reactions