catchError and "try - catch" is not working (async) - flutter

My code is trying to communicate using Socket, but I can't seem to catch the exception.
I've tried these things, but they don't work and I get an exception on the IDE.
Socket socket;
try {
socket = await Socket.connect(ip, port);
// Instead of jumping to errorProcess(), the IDE will show an exception here...
} catch(e) {
errorProcess(e);
}
Socket socket = await Socket.connect(ip, port).catchError((e) {
errorProcess(e);
// catchError says we need to return FutureOr<Socket>...
});
How can I catch exceptions?

Try adding SocketException for your try catch statement.
Example Code:
try {} on SocketException catch (e) {
print(e);
} catch (e) {
print(e);
}

Related

How to catch completeError from play?

I am checking the commit referenced here:
https://github.com/ryanheise/just_audio/issues/65
I am also facing the issue of the iOS browser not autoplaying and would like to catch the error. However the following does not seem to trigger the error:
try {
await player.play().onError((error, stackTrace));
} catch (e) {
print(e);
}
or
Future<void> playAudio() async {
try {
await player.play().catchError(onError);
} catch (e) {
print(e);
}
}
FutureOr<void> onError(error) {
print('ERROR DURING PLAY');
print(error); }
errors don't seem to show up in the console :(
How do I react to the completeError in the web imlementation? Sorry for the noob question. :) Thanks!

Flutter: How do I avoid repetition of try-catch-exception?

This is the code I want to avoid repeating. If possible also try block.
try {
//
} on TimeoutException catch (e) {
log('Timeout Error: $e');
rethrow;
} on SocketException catch (e) {
log('Socket Error: $e');
rethrow;
} on Error catch (e) {
log('General Error: $e');
rethrow;
} catch (e) {
log('All other Errors: $e');
rethrow;
}
You could define a function like:
void tryCatch(Function f) {
try {
f();
} on TimeoutException catch (e) {
log('Timeout Error: $e');
rethrow;
} on SocketException catch (e) {
log('Socket Error: $e');
rethrow;
} on Error catch (e) {
log('General Error: $e');
rethrow;
} catch (e) {
log('All other Errors: $e');
rethrow;
}
}
and then anywhere you want to use it do
tryCatch((){
//your code here
});
One trivial answer could be to refactor that try-catch cascade in a dedicated function or class, in which you return the value if everything's all right, or handle the corresponding error.
I guess yours is just an example code, but if you're just interested in logging an error and then rethrow, you don't need to distinguish between those exceptions and errors.
You can easily retrieve your error's type like so:
try {
// some risky code
} catch(error, stackTrace) {
print(error.runtimeType);
print(error); // calls its internal toString() method
rethrow;
}
Even the above example could be refactored in a dedicated function or class, by the way.
If you're looking into something even more specific let me know, because I'm kinda curious about this, too.

How to stop dio get when the internet is off?

the request is just waiting and not resulting an error, i want an error to be produced when there is no internet but it's just waiting forever
i tried
sendTimeout: 600000,
receiveTimeout: 600000,
but same result
vscode screenshot
You need to wrap your logic inside try/catch block.
try {
var response = await _dio.getUri(
Uri.parse(uri));
if (response.statusCode == 200) {
return response.data;
} else {
throw response;
}
} on SocketException catch (e) {
throw SocketException(e.toString());
} on FormatException catch (_) {
throw FormatException("Unable to process the data");
} catch (e) {
throw e;
}

What's the best practice to throw an Exception in Flutter

I have an exception that gets thrown multiple times before getting handled in my code, (the same error object gets thrown 4 times in the tree).
During each throw , flutter appends the word Exception: to my error object, so the final shape of the message is:
Exception: Exception: Exception: Exception: SocketException: OS Error: Connection refused, errno = 111, address = 10.12.7.15, port = 39682
And this is an example of how I handle exceptions:
getSomeData () async {
try {
await myFunction3();
} catch (e) {
throw Exception(e);
}
}
/*****************************************/
Future<void> myFunction3 () async {
try {
await myFunction2();
} catch (e) {
throw Exception(e);
}
}
/*****************************************/
Future<void> myFunction2 () async {
try {
await myFunction1();
} catch (e) {
throw Exception(e);
}
}
/*****************************************/
Future<void> myFunction1() async {
try {
await dio.get(/*some parameters*/);
}
on DioError catch (e) {
throw Exception(e.error);
}
catch (e) {
throw Exception(e);
}
}
I want to throw the Exception() in a right way so the repetition of word Exception: doesn't appear anymore in my error string.
Instead of throwing a new exception, you could rethrow the catched one:
Future<void> myFunction1() async {
try {
await dio.get(/*some parameters*/);
}
on DioError catch (e) {
// do something
rethrow;
}
catch (e) {
// do something
rethrow;
}
}
See more: https://dart.dev/guides/language/effective-dart/usage#do-use-rethrow-to-rethrow-a-caught-exception

Flutter & Dart Error Handling Middleware Concept

I check my errors between try blocks. How can I get rid of code repetition by creating an intermediate layer instead of rewriting it within each method?
As seen in the codes, I use try blocks in all operations.
Future<List<Post>> getAll(){
try {
//http get code
}on SocketException {
throw Failure('Message');
}on HttpException {
throw Failure("Http Exception Message");
}on FormatException {
throw Failure("Format Message");
}
}
Future<Post> getPost(){
try {
//http get code
}on SocketException {
throw Failure('Message');
}on HttpException {
throw Failure("Http Exception Message");
}on FormatException {
throw Failure("Format Message");
}
}