Closed vmalloc closed 5 years ago
You can use enum for error and implement ErrorResponse trait for it
Indeed, but that just moves the translation into yet another place. For example, if I want to translate constraint violations errors from diesel into 409 errors, that would mean adding a match statement to translate it, and also a fallback to "all other errors", which failure::Error
already provides...
Perhaps there's a way to do this with middleware?
how could you distinguish what error happen with failure::Error
? if you need different responses you have to use different errors or enum as error
i use enums for errors with custom ErrorResponse impl. you are free to choose different approach
Given an Error
and specific cases one wants to test against, there's downcast
and downcast_ref
for that purpose - check if an error is of a specific type...
So just to understand -- your enum would include something like ConflictError, BadRequestError and GeneralError(Error), each one translating to the correct response in the ErrorResponse implementation?
that is correct
I see. Thanks!
On Wed, 31 Jul 2019 at 18:12 Nikolay Kim notifications@github.com wrote:
that is correct
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/actix/actix-web/issues/1018?email_source=notifications&email_token=AAAHAJRUCNXE5YKK6WOFPC3QCGTWRA5CNFSM4IIHTLFKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD3HSQVI#issuecomment-516892757, or mute the thread https://github.com/notifications/unsubscribe-auth/AAAHAJWOCKP22YZNSTYTAG3QCGTWRANCNFSM4IIHTLFA .
-- Rotem
Assuming I have a handler function that internally calls several functions returning
Result<T, Error>
(Error
beingfailure::Error
), I'd like a convenient way of using?
on all those calls and have different specific errors map into different responses (some toConflict
for example, others toBadRequest
and all the rest to internal server error as is the default).I couldn't find a convenient way to achieve this with actix, although it feels like it should be possible. Is it?
Thanks in advance!