EventSource Error with Dart - dart

I'am trying to use EventSource with Dart.
var source = new EventSource('/myUrl');
source.on.error.add((Event e) => print('error'));
source.on.message.add((MessageEvent me) => print(me.data));
The messages are well received, but 'error' is always print.
How can I got more information about this error ?
I tryied to use ErrorEvent instead of Event but it fail because this event is not a ErrorEvent

I don't have an event source to test with, but I can help you answer your question, "How can I got more information about this error?"
Change:
source.on.error.add((Event e) => print('error'));
To:
source.on.error.add((e) {
print('error');
});
Then, set a breakpoint on the line with print. This will let you inspect the local variable e when you debug your application in Dartium.
You can also use dart2js to compile the application to JavaScript and the Chrome's JavaScript debugger to learn more about e. Search for "error" in the generated JavaScript in the debugger to figure out where to put the breakpoint.
Another trick that is sometimes helpful is to change the code to:
source.on.error.add((int e) => print('error'));
e is not an int, so DartEditor will give you a warning, telling you what type e really is. Then, you can use that type. Next, Command-click on that type to look at the source code for it.
My best advice is to try to use Chrome's JavaScript debugger and debug the problem from there. That's helped me isolate this sort of problem in the past.

Related

What are the actual differences between the print() vs debugPrint() and log() in Flutter?

I am trying to create a util for logging.
I need to know the actual differences between print() vs debugPrint() and log().
Many answers I have seen are outdated or confusing. Also, some upvoted answers are contradict.
I am trying to print error messages in red color. I am using ANSI code to achieve it.
debugPrint("\x1B[31m HelloDebug \x1B[0m");
The above one print in red color.
But when i do the same using log(), its not printing in red color. Its escaping the ANSI code.
One thing I found out was log() has error parameter.
If i pass something as error , it handles red color by default.
Here in the first one using log, red is not coming because ANSI code is ignored. But in debugPrint using it's working fine. In third one it takes error red by default. But extra line is added even for empty string message.
Code:
Output:
Is it possible use ANSI code for making color text using log?
Because I have other ANSI code to make different color text.
I dont want use debugPrint because if I print some thing too frequently , the system ignore the logs.
After digging some time I found out that log function is implemented in c++ in the dart runtime.As the log function is external function in dart sdk.
external void log(
String message, {
DateTime? time,
int? sequenceNumber,
int level = 0,
String name = '',
Zone? zone,
Object? error,
StackTrace? stackTrace,
});
https://github.com/dart-lang/sdk/blob/main/runtime/lib/developer.cc
Also I see some different answers while digging.
The below answer says debugPrint only available inside widget class. Is it still true?
https://stackoverflow.com/a/52241553/9248098
EDIT:
While using debugPrint and when I launch app from Android studio terminal, the ANSI color is working in android but when I run it same in iOS its escaping the characters in same Android Studio terminal.
If ANSI code support is based on terminal support, I couldn't figure out why its having issue in same terminal in iOS.
It is well explained in this page Debugging Flutter apps programmatically
debugPrint() is similar to print() but with a throttles applied to prevents being dropped by Android’s kernel.
If you have linter setup on VSCode or other IDE, you will see this warning whenever you use print in your code. For more information, you can refer to Avoid print calls in production code.
To avoid the lint error, you can use kDebugMode with print or debugPrint instead.
if (kDebugMode) {
print('test print');
}
// OR
debugPrint('test debugPrint');
As for log(), it actually allows you to include more information on the logging output. For example, you can add the runtimeType and the relevant object in the log that can help you in debugging the code.
log(
'test log',
name: runtimeType.toString(),
error: myObj,
);
** btw I tried log with ANSI code log("\x1B[31m HelloDebug \x1B[0m");, it is working as expected.

Response isn't a type, cannot and expected to find ';'

I'm trying to send a post request within dart with the following code.
Future<Response> post (url,
{
headers, // My Map<String, String> for header info
body // My Map<String, String> for body info
})
return JSON.jsonDecode(post);
I expect it to send, however, I get two errors.
The name response isn't a type, and cant be used as a type argument, when I hover over "Response" and
expected to find ;, when I hover over "post".
I also get green underlining in android studio saying avoid unnecessary statements
This problem happens in Computers with low specs which makes android studio take time to recognize new input. Waiting will fix the issue, and you can speed this "recognition" process by clicking on the type Response in you case which will make the editor focus on it. And also add parenthesis and commas in correct way before adding new code as Dart Analyzer will quickly get confused and fill your editor with error messages.
I encountered the same issue, and I found that rerunning flutter pub get helped as that command makes sure the Http library gets downloaded.

override assert in lua

my setup board uses lighttpd to provide web UI to the board.
It uses lua and JS to draw logic.
What I am seeing is If I enter an URL as "IPofboard/somejunkhere"; its properly throwing "404 not found"
But when I fire "IPofboard/somejunk.lp" (which is some junk lua file); It produces an "assert" error for file not found. Thats how lua works.
But I want to modify/override this assert to show same custom message as "404 not found"
any idea?
I am new to lua. Is it even doable?
As lhf mentions, it is very easy to redefine any function in Lua, but I think this may not be what you need. The issue is that after you do
local origAssert = assert
assert = function(message)
do something (possibly using origAssert)
end
then every function call that uses assert will use your new assert function, which is probably not what you want. Instead, you can call your function in "protected" mode: this will trap the assertion as an error message, and you can then decide what to do. For example,
ok, ret1, ret2 = pcall(yourFunction, arg1)
if not ok then
do something, possibly print ret1 (the error message)
end
Same thing if you are requiring a module that does some initialization:
ok, module = pcall(require, yourModuleName)
if not ok then
print("ERROR:", module) -- if not ok then module is err message
end
I'm not familiar with how lighttpd embeds Lua, but in Lua you can redefine anything, including functions from the standard Lua library, such as assert.

How do exceptions during Rhino readFile work?

I have this code running inside Rhino under Linux. The file doesn't exist.
try {
var u = readFile("/tmp/wtf");
print(u);
} catch (e) {
print("error!");
}
The code in the 'catch' doesn't run, even though the file definitely isn't there. I just get a blank value assigned to 'u'. Is this normal?
Are there other situations (besides a file missing) where the catch would run?
Could I differentiate an empty file from a missing one without invoking some other function? (I realize Rhino gives me access to most of the standard Java libaries).
Just want to confirm that readFile is always synchronous?
I can't find anything much on SO or MDN about how readFile works. Any insight appreciated.

having problem in blackberry

I am doing coding from beggining blackberry by Anthony Rizk.
I am stuck with this code as it is showing error again and again...
private void getURL() {
HttpRequestDispatcher dispatcher = new HttpRequestDispatcher(urlField.getText(),
"GET", this);
dispatcher.start();
}
Can anyone explain me why we are passing this as parameter and why actually this code is doing...
"this" refers to the main screen you passed to the class so you can alert the requestFailed string. Check the run method on page 170. You'll see screen.requestFailed("Unexpected...").
As for your error - I suggest adding this line:
System.out.println(" ----------------- HTTPREQUESTDISPATCHER ---------- " + urlField.getText());
right before your dispatcher.start(); line and then compile in debug mode to see what your console says. Just to make sure your URL to request is a valid web URL.
Additionally, make sure your simulator has MDS enabled. You need that to make web calls.
In eclipse it is under Run->run configurations-> simulator tab -> general -> checkbox for Mobile Data System.
I don't know where it is in the RIM package. If you're not using eclipse, you might want to switch over to it. It will highlight errors and try to help you resolve them.

Resources