How to handle error from middleware #2750
Unanswered
SardineFish
asked this question in
Q&A
Replies: 1 comment 2 replies
-
In general you don't. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Problem
In the middleware service chain, we call next service with
ServiceRequest
and return theServiceResponse
from the next service.But if next service return an error, how can we make it into a
ServiceResponse
Background
In our project, we need to make our APIs always response in a certain JSON format.
But if we have error in middleware, we can only return it out and let actix-web response an
Internal Serivce Error
.We cannot make a custom
ServiceResponse
becauseServiceRequest
is moved to next service and the innerHttpRequest
cannot clone.For example in official middleware
ErrorHandler
line 157actix-web/actix-web/src/middleware/err_handlers.rs
Lines 148 to 177 in 6a5b370
The error from next service is just returned to upper service.
Finally it will become a
500
response with default text bodyInternal Serivce Error
.Beta Was this translation helpful? Give feedback.
All reactions