Standardize errors for Directus and extensions by exporting from @api/errors #19872
Replies: 3 comments 3 replies
-
I'm a little hesitant to expose the ones directly exported from |
Beta Was this translation helpful? Give feedback.
3 replies
-
This feature request has been accepted and is queued to be implemented! You can follow along with the progress here: #19906 |
Beta Was this translation helpful? Give feedback.
0 replies
-
This was completed! |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Summary
Currently
isDirectusError()
method accept a generic argument for the kind of errors but there is no way to import the extension from the errors source.It's will also provide the extension creator to throw errors with the same format of the directus ones. Like if an auth extension need to throw an
MethodNotAllowedError
error.It's a way to standardise errors for extensions and not having errors that is almost identical but not excatly the same.
Basic Example
The ability to import api errors to be used in extension
With current namming of the const in
@directus/api
if they are exported we could do :But maybe it's worth reworking isDirectusError to accept a
createError
return instead of a string;Motivation
I am using a custom endpoint to insert data into the database. It's an uploaded "zip" file with many data inside that need to be splited up to multiple tables with some extended validation. So for that case I was not able to use the included validator.
So I need to insert data into multiple collections with some uniques constraint so I may get errors from multiple sources.
And to give clear errors message I need to read from it to determine what to do.
Detailed Design
The
@directus/errors
package will need to have any errors used by directus from thecreateError()
call. Any errors that is used should be migrated to that package to be referenced from the api package.I think it's worth exporting both string codes like the
ErrorCode
enum here :directus/api/src/errors/codes.ts
Lines 1 to 29 in 01f0a18
Migrate any
createError
that could be without side effects in the@directus/errors
package.For example :
directus/api/src/errors/record-not-unique.ts
Lines 25 to 29 in 01f0a18
Requirements List
Must Have:
Could Have:
createError
isDirectusError
to acceptcreateError
responsecreateError
to not accept codes that are defined in the well-known errors.Drawbacks
If we limit
createError
code param to exclude any codes defined in the well-known codes, users will get a typescript errors, they will need to use the exported one instead of making a new one.Alternatives
As an alternative I was able to make it work with :
And in tsconfig.json
But not really a good idea to do that.
Adoption Strategy
No kreaking change as you just add exports and you can check
typeof code
for theisDirectusError
methodUnresolved Questions
No response
Beta Was this translation helpful? Give feedback.
All reactions