Discussion:
[dart-misc] How to catch dart.io socket error?
Niyazi Toros
2018-07-05 05:52:54 UTC
Permalink
Hi,

I am new to Dart, Aqueduct and socket. I am trying to learn all while I am
working and making test project to understand. Any deep info and
help appreciated.

Below code works for me as I expected in aqueduct. Problem is that I need
to modify value before sending a socket, so I need to await like this. "await
for (var data in socket)" Also I have some heavy work in 2 places. First I
need to decrypt the request before sending to socket "socket.write(
'$tempData\r\n');".
Once I get a data from socket (listen) "await for (var data in socket)" I
need to do some heavy work again before sending as response.

I had a 3 question that shown below.
Question-1: How to catch error if serevr is not reachable or down?
Question-2: How to catch error when trying send data to socket?
Question-3: How to catch error while socket listen returning data?



import 'package:aqueduct/aqueduct.dart';
import 'package:niyaziapi/niyaziapi.dart';
import 'package:niyaziapi/util/niyaziprivate.dart';

class LoginController extends Controller {
@override
Future<RequestOrResponse> processRequest(Request request) async {
if (request.path.variables.containsKey('value')) {
var _xPrivate = (request.path.variables['value']).trim();
var tempData = await *getPrivate**(_xPrivate**)*;

// TODO: Question-1: How to catch error if serevr is not reachable or down
var socket = await Socket.connect('192.168.1.22', 1024);
print("socket: $socket");

// TODO: Question-2: How to catch error when trying send data to socket
socket.write('$tempData\r\n');

// TODO: Question-3: How to catch error while socket listen (returning data)has error
await for (var data in socket) {
var reply = new String.fromCharCodes(data).trim();
// Do some heavy work in here then
return new Response.ok("$reply");
}
} else {
return new Response.ok("wrong rquest");
}
}

Future<String> getPrivate(_xPrivate) async {
// Do some heavy decryption in here then

return "data";
}
}
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/c9495b92-3ccd-498f-95ac-80a9453196b1%40dartlang.org.
'William Hesse' via Dart Misc
2018-07-05 08:28:53 UTC
Permalink
You may get more replies if you post questions like these on
StackOverflow. For example, more than 100 dart questions were posted there
this week. You may also find questions already asked, that are similar to
yours.

https://stackoverflow.com/questions/tagged/dart

In general, when working with async functions and streams using the
async/await keywords, error events are thrown as exceptions, and can be
caught with try/catch.
If instead of using async/await, you use .then() on futures and .listen()
on streams, then there are onError parameters and methods you would use to
add an error handler.
The best way to answer basic questions like these is to find example code
that does similar things to what you want to do, and see how it does it.
Post by Niyazi Toros
Hi,
I am new to Dart, Aqueduct and socket. I am trying to learn all while I am
working and making test project to understand. Any deep info and
help appreciated.
Below code works for me as I expected in aqueduct. Problem is that I need
to modify value before sending a socket, so I need to await like this. "await
for (var data in socket)" Also I have some heavy work in 2 places. First
I need to decrypt the request before sending to socket "socket.write(
'$tempData\r\n');".
Once I get a data from socket (listen) "await for (var data in socket)" I
need to do some heavy work again before sending as response.
I had a 3 question that shown below.
Question-1: How to catch error if serevr is not reachable or down?
Question-2: How to catch error when trying send data to socket?
Question-3: How to catch error while socket listen returning data?
import 'package:aqueduct/aqueduct.dart';
import 'package:niyaziapi/niyaziapi.dart';
import 'package:niyaziapi/util/niyaziprivate.dart';
class LoginController extends Controller {
@override
Future<RequestOrResponse> processRequest(Request request) async {
if (request.path.variables.containsKey('value')) {
var _xPrivate = (request.path.variables['value']).trim();
var tempData = await *getPrivate**(_xPrivate**)*;
// TODO: Question-1: How to catch error if serevr is not reachable or down
var socket = await Socket.connect('192.168.1.22', 1024);
print("socket: $socket");
// TODO: Question-2: How to catch error when trying send data to socket
socket.write('$tempData\r\n');
// TODO: Question-3: How to catch error while socket listen (returning data)has error
await for (var data in socket) {
var reply = new String.fromCharCodes(data).trim();
// Do some heavy work in here then
return new Response.ok("$reply");
}
} else {
return new Response.ok("wrong rquest");
}
}
Future<String> getPrivate(_xPrivate) async {
// Do some heavy decryption in here then
return "data";
}
}
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups
"Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an
To view this discussion on the web visit
https://groups.google.com/a/dartlang.org/d/msgid/misc/c9495b92-3ccd-498f-95ac-80a9453196b1%40dartlang.org
<https://groups.google.com/a/dartlang.org/d/msgid/misc/c9495b92-3ccd-498f-95ac-80a9453196b1%40dartlang.org?utm_medium=email&utm_source=footer>
.
--
William Hesse
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/CAMQL4kiwudpYhzX6KGZW4AwB%3Dm4uMrmMxs1T0GJSvGyvoJ__8g%40mail.gmail.com.
Niyazi Toros
2018-07-05 08:35:37 UTC
Permalink
Thanks William
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/bcf42a10-6eef-4d1a-bf9d-1c0fd6c684f8%40dartlang.org.
Niyazi Toros
2018-07-05 12:47:59 UTC
Permalink
Going to Like Aqueduct twice :)

It was very simple:
var _xPrivate = *(request.path.variables['value']).trim()*;* change to:*

var _xPrivate = request.path.remainingPath;
print("request: $_xPrivate");
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/5ccc222a-27f5-45f2-b298-428c9a0b8c13%40dartlang.org.
Loading...