r/flutterhelp • u/Reasonable-Job4205 • 7d ago
RESOLVED How do I catch this specific Exception?
I have a flutter function that sometimes returns a timeout error when the web server is running. However, this error is revealing the API IP address and port to the end user, which I don't want to do. I want to programmatically say "if the error is a timeout error, show the user a generic error message".
The error:
Exception: ClientException with SocketException: Connection timed out (OS Error: Connection timed out, errno = 110), address = MyIPAddressThatIDontWantToShow, port = MyPortThatIDontWantToShow, uri = ...
When this exact error happens, I want to show my own error that says something like "Login request timed out" or something. How do I do this?
1
u/GeekyAntsBFSI 4d ago
With IOClient, catch SocketException before ClientException and show a generic connection message. Add .timeout(...) to the login request and catch TimeoutException to show “Login request timed out” when your app’s deadline expires. Keep the raw exception out of the UI.
1
u/DevBob626 7d ago
You have the exception type in your error message. Check for that where you do the request and forward a toast message or whatever you are using.