Is there a way to catch expression change error? - javascript

I'm getting NG0100 error (Expression changed) in my console. Is there any way I can catch this error in my code and print it in the console via my code?
I tried try catch block but it doesn't seem to be working.
try{
//error generating code
}
catch(e){
console.log(e.message)
}
Thanks!

Actually, catch a NG0100 won't help that much. It's not a blocking error, it's just a big warning to tell yout that your code is unsafe and some expressions change at a point of the component genereation where it shouldn't.
It is supported by Angular DoubleCheck wich is a system that check your template binded attributes to make sure your code is stable and predictable. NG0100 means your expression has changed between these two checks.
It is just a warning because this DoubleCheck only exists in devmode (aka ng serve or ng build --configuration developement).
Best you can do is find why and where this happens in your code, to avoid it.
The informations given can't let us know, but you can watch this video from Angular official documentation that explains most common reasons of NG0100

Related

How to fix 'thrown exception caught locally' when using instanceof to determine internal errors type

I have a block of code that loads a set of plugins. For each plugin, a list of functions is called inside of a try catch block to determine if the plugin has failed or not and act accordingly. At the same time, one of the multiple functions of the plugin can return a specific error that mean that the plugin has not failed but is not valid to execute the next functions with the data.
Let's see an example (the code is Typescript, but I am going to make it as language agnostic as possible):
for each plugin:
try:
plugin.function1(data)
plugin.function2(data)
plugin.function3(data)
try:
plugin.function4(data)
catch error:
if error instanceof PluginNotValidForThisKindOfDataError:
continue
else:
throw error
plugin.function5(data)
plugin.function6(data)
catch error:
log(plugin has failed)
(I hope the code is clear enough. I'll update it if required)
As can be seen, I execute function4 and I parse the possible errors because one of them (there are multiple) is "tolerable" and just means that it is not valid for function5 and function6 with that specific set of data. However, I still have to throw the other errors because they are not good. At the end, I catch the global set of errors to determine if the plugin has crashed or not.
What I get in my IDE, JetBrains (WebStorm specifically) is a thrown exception caught locally warning. But I am not able to reimagine how to redo that block to act differently. I am not using throws as flow control but just passing the error.
I admit that the main problem is that in Javascript I can not do catch PluginNotValidForThisKindOfDataError, which would be the optimal situation (hope its added some day). But, with the tools I have, how can I refactor this?
Thank you very much.
Added to both language-agnostic and javascript because of the specific Javascript try-catch method
I see three options:
Ideally, the plugin wouldn't throw an error for a non-error condition like that. Instead, have it return a value that tells the code in your question whether it should run functions 5 and 6.
If you want to keep it the way it is, though, you can either:
Ignore the warning (and probably there's a way to disable it for one line), or
Don't re-throw; instead, do the same thing you're doing in the outer catch (log(plugin has failed)) followed by continue. Provided that's just a single function call or similar, the duplication isn't ideal, but isn't horrible either. If there's any complexity to that step, isolate it into a function (perhaps even a local function) and call that from both places.

Turns warnings into errors in Ember.JS

Is it possible to throw warnings as errors in Ember application. I found
Ember.ENV.RAISE_ON_DEPRECATION = true
but as I understand it works only for depreciation warning.
I want to throw warnings like
"WARNING: Binding style attributes may introduce cross-site scripting vulnerabilities; please ensure that values being bound are properly escaped. For more information, including how to disable this warning, see http://emberjs.com/deprecations/v1.x/#toc_warning-when-binding-style-attributes30."
You can use Ember.Logger.warn or Ember.warn. The differences between the two is that the second in a production build, is defined as an empty function.
Ember.Logger.warnseems undocumented but you can give a look at the source code https://github.com/emberjs/ember.js/blob/master/packages/ember-console/lib/index.js#L81.
Docs for Ember.warn: http://emberjs.com/api/#method_warn

Getting more information from “SyntaxError: Parse error” message in PhantomJS/CasperJS

I have a long CasperJS script. When I run it I get:
phantomjs file.js
SyntaxError: Parse error
Is there a way to get some more information about the error.
At least a line number? or any hint at all?
Try run the file.js with node, so for your example:
node file.js
It's not possible to determine this in PhantomJS itself. The documentation on phantom.onError says:
This is the closest it gets to having a global error handler in PhantomJS
And this doesn't catch the syntax error. If you try to run it with the --debug=true option, you will see a lot of debug messages, but the final error has still the same amount of information.
Another thing that I tried was to use a second PhantomJS script which reads the original script and tries to eval it. The phantom.onError event is triggered in this case, but the trace argument is empty.
The good thing is that PhantomJS/CasperJS scripts are just JavaScript, so you can paste them to http://jslint.com/ or run a dedicated jslinter on them to see where the problem lies. There are some options that you have to mark on the site or otherwise you will get a lot of errors:
add phantom to the global variables box,
enable node.js mode and
tolerate "everything" (or those things that you actually want to tolerate)
I spent a whole 8hrs on this to find a trick for this problem. The trick is to run "phantomjs" and type 'require "path_to_js_file"'. I used 2.1.1 version of phantomjs. Likely 2.2 also works.
Then there will be a stack trace that shows which line is the offender. You won't see this in testem output.
In my case, if you define a property twice for an object, it will work for chrome, firefox etc, but not phantomjs. Lint might help but there are >5K lint errors for the project I work on and it is practically impossible to see what's wrong. Also the particular problem is likely hidden under the same bucket of "javascript strict mode violation". Nodejs didn't complain this either.

Access source map for debugging Closure Compiler?

I am in the process of ironing out my codebase so it can compile with ADVANCED_OPTIMIZATIONS on google's closure compiler.
After properly setting up the debugging environment required for this task (source map file, chrome, wrapping the compiled js file) i stumbled upon try catch issues. It appears that extensive use of try/catch statements in my codebase has backfired on me.
On almost all methods and functions i use a typical try { } catch(e) {ss.error(e);} statement, where ss.error() is a generic error handler that depending on environment either prints out debug stuff or reports back the exceptions...
In the process of ironing my codebase, when i get an error that i need to fix, what happens is that instead of having Chrome report the offending file and line, it points to the error handler ss.error(). Thus leaving me with no way of backtracing the problem. The ss.error() func however, does print where the problem originated from:
Error! type:TypeError at Db (/jsc/compiled.js:547:246) msg:Cannot call
method 'ka' of undefined source:
After i get these type of errors i have to do two steps:
1. Fiddle with the compiled code at line 547 char 246 and try to figure out which part in my uncompiled code this refers to...
2. After i locate it, remove the try/catch blocks so i can directly and more clearly see what caused the error...
I must say i am not happy with this workflow and need to find an alternative that will both allow me to properly catch exceptions and debug my compiled and uncompiled code while retaining mind sanity =)
I was thinking of somehow using the Line:CharPosition info to query the source map and have the ss.error() function do the mapping to my uncompiled source code...
ideas?
There is a java interface to the SourceMaps as part of the closure compiler. There are also JS implementations in various states of repair. I try to keep links to them up to date here:
http://code.google.com/p/closure-compiler/wiki/SourceMaps
For the Java implementation you simply load the source map using the SourceMapConsumerFactory, the interface is pretty simple.

What could cause an UmbrellaException anonymous function on deployed GWT app?

I seem to be running into an odd problem. When using my GWT application in a local environment, everything works as it should. The problem comes in after I compile and deploy my application. When I go through my project workflow and click on a certain link to switch into a new panel, I get the following error (from my console in Chrome):
Uncaught com.google.gwt.event.shared.UmbrellaException: One or more exceptions
caught, see full set in UmbrellaException#getCauses (anonymous function)
This error is thrown by one of the cache file generated by GWT at compile time. But this never happens on the locally deployed program (deployed from Eclipse, "Run as Web Application"). Has anyone ever run into this issue or can provide any direction for a fix?
Thank you! :)
I had the same problem just now. Works locally, fails with the mentioned Javascript console error, nothing in server logs.
Turns out that client Java code (which is complied to Javascript) had try/catch block which worked when executed in Java, but failed silently when compiled to Javascript. I'm still not sure what was the exact nature of the problem, but try removing try/catch blocks.
(It seems that in my case, table.getWidget() call was failing and throwing exception.)
I had the same problem, i think interpretation of try catch is not the same than in Java... after gwt compilation, when you are in catch case, the execution failed. If you open firebug, you can see point of errors into JS.
I had the same problem, it worked in development mode. Then, after I compiled I would get an error. To fix, I had to get rid of:
try{
//some code
} catch(NullPointerException ex){
//more code
}
Instead I did:
if(variable != null){
//some code
} else {
//more code
}
After that it worked perfectly.

Categories

Resources