3

I am doing a post request like this in flutter using HTTP dart package but sometimes when the network fails a ClientException is thrown from IOClient class but I don't get anything in catch block and app crashes.

http.post(url, headers: headers, body: body).then((response) {
  //response parsing
}).catchError((error) {
 //ClientException is never catched in this block.
});
4
  • 1
    Have you tried wrapping it in try{ ... } catch (e) { }? Commented Sep 26, 2018 at 10:13
  • I am using Future API not async-await, I think try catch not used in Future API? Commented Sep 26, 2018 at 10:20
  • Wouldn't be too hard to try it out. If the error is thrown sync, try/catch still works. You could also try onError as shown in dartlang.org/guides/libraries/futures-error-handling Commented Sep 26, 2018 at 10:22
  • try-catch with await seem to be working. Not able to reproduce till now. Commented Sep 26, 2018 at 11:00

4 Answers 4

2

As mentioned in package:http/src/io_client.dart:

Any internal HTTP errors should be wrapped as [ClientException]s.

As mentioned in the docs this is a clear case of:

Potential problem: accidentally mixing synchronous and asynchronous errors

To resolve this issue, you need to wrap your code in Future.sync().

Future.sync() makes your code resilient against uncaught exceptions. If your function has a lot of code packed into it, chances are that you could be doing something dangerous without realizing it:

return new Future.sync(() {
  http.post(url, headers: headers, body: body).then((response) {});
});

Future.sync() not only allows you to handle errors you know might occur, but also prevents errors from accidentally leaking out of your function.

Sign up to request clarification or add additional context in comments.

Comments

0

In my case the call was to an https address using Microsoft Internet Information Services as backend, in the SSL settings of the website in IIS i had mistakenly set "Client certificates: Accept" instead of "Client certificates: Ignore", setting "Ignore" solved the problem.

Comments

0

Just goto your C:\Users{user}\AppData\Local\Pub and delete the cache folder.

Comments

0

From the IOClient class docs, here's how ClientException class is used to handle exceptions coming from pkg/http client;

final client = http.Client();
late String data;

try {
  data = await client.post(url, headers: headers, body: body);
} on SocketException catch (e) {
  // Exception is transport-related, check `e.osError` for more details.
} on http.ClientException catch (e) {
  // Exception is HTTP-related (e.g. the server returned a 404 status code).
  // If the handler for `SocketException` were removed then all exceptions
  // would be caught by this handler.
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.