How to type-annotate ServiceErrors #1170
-
I am trying to handle an UnauthorizedException gracefully with a user friendly error message.
The code where I am trying to handle those nested errors looks at the moment like this:
This fails, because the type annotation for the ServiceError expects two types <E, R> an I cannot for the life of me figure out, where in all the aws sdks the types for an "UnauthorizedException" or a "Response" are defined and what I can put there instead of E and R. Also, overall I am quite puzzled, that it is such a hassle to get to the underlying root cause of an error. The overall code is like 200 lines and already 40 of them are just to get the first error handled, not to mention all the open todo!s. I would like to say, that it is my lack of expertise with this sdk, but since I have now searched for a proper error handling guidance for 8 hours and found so many struggling with this, that I tend to think that there is something severely lacking, be it documentation, examples or code... Is there someone out there, who has implemented error handling in a Rust idiomatic way, that is concise and clear to understand? Thank you so much in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
So now I found the types as:
which does not compile either, because of:
So nothing really works. Not the .into_service_error way, the destructuring way, the downcast way... I give up. Error handling is unusable without better documentation. Enough time wasted. |
Beta Was this translation helpful? Give feedback.
-
We apologize for having you spend long hours trying to obtain what you need. Is the end goal of the error handling above accessing the underlying error message from S3? If so, you can try using |
Beta Was this translation helpful? Give feedback.
-
Thank you, @ysaito1001 and @Velfi, for replying and offering a solution, it is highly appreciated. It is my fault that I have not described better, what I am trying to achieve, so let me try again with some concrete minimalistic code examples. If I start from one of the basic examples from the SDK and I set up my AWS profiles and sso tokens correctly, then I get correctly the bucket name back:
Code:
If I logout again, I get this:
This error message is different, when the token is still there, but expired:
Or if the region is misconfigured:
My goal was to distinguish between different error messages and give the user a more helpful information on how to overcome the respective problem. Here in both cases, a re-login is necessary, but there may be other kinds of errors, that require a different fix, like to correct the region. If I now change the code to use the
this would only be a slight change:
So the shown message changes from So I want to get at the
And the Therefore my question, what am I doing wrong. I am a novice in rust with a few months experience, so I totally blame this on my lack of knowledge and so I ask for guidance what the correct way of achieving my goal is, or of it is nothing I should concern myself with and let the errors speak for themselves. Thank you for taking the time to read all of this and if you can help out with a small code example, that would be very much appreciated. Michael. |
Beta Was this translation helpful? Give feedback.
Thank you for taking time to clarify your use case.
Sounds like you wish to write an error handling code with a match expression that has arms based on the underlying error reasons?
One thing to note is that the errors you shared with us above indicate they are wrapped in DispatchError, which is an informative error. You can take a look at this RFC for more details, but the gist is that there are two broad categories of errors in the SDK, actionable and informative. It makes sense to write a match expression on the former but makes less sense to do so with the latte…