dio icon indicating copy to clipboard operation
dio copied to clipboard

Developer should throw custom exception in interceptor

Open WutangNailao opened this issue 2 years ago • 14 comments

Request Statement

When i write a interceptor class extends Interceptor, in some case, i wish throw some exception to tell the developer some thing went wrong. so i throw a custom exception in override function 'onResponse', but this exception was catched by DioException, and this exception can not tell me more infomation about this error.

image image

So, i wish this will be supported

Solution Brainstorm

No response

WutangNailao avatar Sep 01 '23 06:09 WutangNailao

This makes a lot of sense.

I think we either should properly wrap the exception, or we shouldn't wrap it at all.

ueman avatar Sep 01 '23 14:09 ueman

I think we do.

Just from looking at the screens, the reported exception probably originated at the jsonDecode.

kuhnroyal avatar Sep 01 '23 14:09 kuhnroyal

These (#1949) are quite similar which requires a nicer first-look regarding the exception.

AlexV525 avatar Sep 15 '23 13:09 AlexV525

Adding request information to the exception is one thing. Figuring out a sensible way to convert arbitrary exceptions/objects into strings is another. Any ideas for this? We call toString() on the inner error but we don't always pass a message to the DioException - that is why it says [unknown]: null.

kuhnroyal avatar Sep 15 '23 15:09 kuhnroyal

DioException has a error field, this is probably what you're looking for? https://github.com/cfug/dio/blob/4d7ea685bd9de004bffe276e24e966400b98e818/dio/lib/src/dio_exception.dart#L176-L178

brian030128 avatar Sep 30 '23 15:09 brian030128

Based on the above comments, the root cause of the exception is the JSON fails to be decoded before it reaches your exception, so a FormatException was thrown. Otherwise, it would be DioException(APIException).

Closing as it's working as intended.

AlexV525 avatar Oct 14 '23 15:10 AlexV525

it's my wrong, i show a wrong example. this is a good example i think for my issue. image image

when i get a response with code 401, i throw a apiexception, in the terminal, i see which i throwed apiexception, but it wrapped with dioException, so i can't not catch this error exactly next time.

WutangNailao avatar Oct 21 '23 09:10 WutangNailao

Not sure how we can achieve that other than the developer registering custom types somewhere.

kuhnroyal avatar Nov 03 '23 22:11 kuhnroyal