How to retrieve try/catch error - javascript

how can I find the error
try {
undef
} catch (e){
console.log(e)
console.dir(e)
}
the information bust be there somewhere, because the console.log (in firebug) includes:
ReferenceError: undef is not defined
but when I browse the e object, I can't find it.
How do I find out what the error is programmatically, so I can handle the error accordingly?
Edit:

try {
if(typeof undef == 'undefined'){
console.log('We should not access this "undef" var');
}
console.log('The next line will produce an exception');
undef
} catch (e){
console.log(e);
for(index in e){
console.log(index+' ('+(typeof e[index])+'): '+ e[index]);
}
}
Which will produce:
We should not access this "undef" var
The next line will produce an exception
ReferenceError: undef is not defined
fileName (string): file:///B:/xampp/htdocs/study/test.html
lineNumber (number): 12
stack (string): #file:///B:/xampp/htdocs/study/test.html:12

I don't think you can pull its type explicitly, but you can test it:
try {
undef
} catch (e){
console.log(e)
console.dir(e)
if(e instanceof ReferenceError) {
console.log("Ooops! Fat fingers!");
}
}

But of trial and error gives me this...
try {
undef
} catch (e){
console.log(e.toString())
// ReferenceError: undef is not defined
}
I guess firebug is just accessing the .toString() method of e
Edit:
I am guessing the .toString() method just concatenates the only two properties that are guaranteed cross browser - name and message - so I think e.message is the only reliable and useful piece of information to go with.

Related

Safari not catching exception when trying to access parent window object with Javascript try/catch

So the code below can be seen working via the fiddle link. Safari is refusing to catch the exception- I am assuming this may be because it is not a 'Javascript' error? Either way, if you run the code in any other browser, you will see the page URL in the console.
The purpose of the function is find the page URL when executed multiple iframes deep on the page. If anyone can confirm why Safari won't catch the error and/or maybe offer a solution, that would be great...Thanks!
function logURL() {
var oFrame = window,
exception = false;
try {
while (oFrame.parent.document !== oFrame.document) {
oFrame = oFrame.parent;
}
} catch (e) {
exception = true;
}
if(exception) {
console.log('excepted', oFrame.document.referrer);
} else {
console.log('no exception', oFrame.location.href);
}
}
http://jsfiddle.net/HPu9n/82/
While an error is logged to the Safari console, Safari apparently does not thrown a JavaScript exception when accessing a window property across frame origins. Instead, Safari simply returns undefined for any property which is not accessible cross-origin.
With this knowledge, we can simply check if oFrame.parent.document is set, and if it's not, break off the loop an do what would happen if the browser threw an exception.
Example (JSFiddle):
function logURL() {
var oFrame = window,
exception = false;
try {
while (oFrame.parent.document !== oFrame.document) {
//Check if document property is accessible.
if (oFrame.parent.document) {
oFrame = oFrame.parent;
}
else {
//If document was not set, break the loop and set exception flag.
exception = true;
break;
}
}
} catch (e) {
exception = true;
}
if(exception) {
console.log('excepted', oFrame.document.referrer);
} else {
console.log('no exception', oFrame.location.href);
}
}
logURL();
Logs:
excepted http://jsfiddle.net/ggh0a0f4/

Parse Failed with: TypeError: Cannot read property 'success' of undefined

I keep getting this error Failed with: TypeError: Cannot read property 'success' of undefined which i cannot figure out the problem
Parse.Cloud.httpRequest(
{
url:url,
success:function(httpResponse)
{
var Day = Parse.Object.extend("TestDay");
var queryToday = new Parse.Query(Day);
queryToday.equalTo("dayday", day);
queryToday.equalTo("daymonth", month);
queryToday.equalTo("dayyear", year);
queryToday.equalTo("owner", theUser);
queryToday.first().then(function(dayObject) <---line 662
{
if(dayObject == undefined)
{
console.log("not found");
}
else
{
console.log(dayObject);
}
}, function(error)
{
console.log("first failed");
});
}
});
Failed with: TypeError: Cannot read property 'success' of undefined
at Object.b.Query.first (Parse.js:1:57000)
at Object.Parse.Cloud.httpRequest.success (main.js:662:48)
How do you know that's the line? I mean, are you on web using the console?
Is there something else to know? Like are you working on a cloud function?
My guess is that returning dayObject.save(); doesn't work. Try using a console.log(dayObject) to know exactly the query's response. It may happens that you'll have to use a conditional as follows:
if(dayObject == undefined){
return Parse.Promise.error("Not Found");
}else{
dayObject.increment("totalnumberofphotos");
return dayObject.save();
}
I have found the answer to my question. it is because the parse sdk on cloud is too low version, so i have to update it to the latest one and it works now. thank you very much

How to specify a "caused by" in a JavaScript Error?

In my NodeJS program, I parse some user JSON file.
So I use :
this.config = JSON.parse(fs.readFileSync(path));
The problem is that if the json file is not correctly formated, the error thrown is like:
undefined:55
},
^
SyntaxError: Unexpected token }
at Object.parse (native)
at new MyApp (/path/to/docker/lib/node_modules/myApp/lib/my-app.js:30:28)
...
As it is not really user friendly I would like to throw an Error specifying some user friendly message (like "your config file is not well formated") but I want to keep the stacktrace in order to point to the problematic line.
In the Java world I used throw new Exception("My user friendly message", catchedException) in order to have the original exception which caused that one.
How is it possible in the JS world?
What I finally did is:
try {
this.config = JSON.parse(fs.readFileSync(path));
} catch(err) {
var newErr = new Error('Problem while reading the JSON file');
newErr.stack += '\nCaused by: '+err.stack;
throw newErr;
}
There is an new Error Cause proposal for ECMAScript, and it reached stage-4 at TC34!
It means it will be in the next ECMAScript version!
https://github.com/tc39/proposal-error-cause
You would provide the cause as an error option:
throw new Error(`Couldn't parse file at path ${filePath}`, { cause: err });
The ES proposal only formalize it on the language level, but browsers/NodeJS should normally agree to log the full causal chain in practice (see https://github.com/nodejs/node/issues/38725)
As of today (end of 2021), Firefox Devtools are already able to log nested stacktraces!
Joyent released a Node.js package that can be used exactly for that. It is called VError. I paste an example of how you would use the pacakge:
var fs = require('fs');
var filename = '/nonexistent';
fs.stat(filename, function (err1) {
var err2 = new VError(err1, 'stat "%s"', filename);
console.error(err2.message);
});
would print the following:
stat "/nonexistent": ENOENT, stat '/nonexistent'
2021 Update: To chain exceptions in JS:
class MyAppError extends Error {
constructor(...params) {
super(...params)
if (Error.captureStackTrace) {
// This is the key line!
Error.captureStackTrace(this, this.constructor);
}
this.name = this.constructor.name
}
}
See the Mozilla docs on Error.captureStackTrace
Use a try / catch block:
try {
this.config = JSON.parse("}}junkJSON}");
//...etc
}
catch (e) {
//console.log(e.message);//the original error message
e.message = "Your config file is not well formatted.";//replace with new custom message
console.error(e);//raise the exception in the console
//or re-throw it without catching
throw e;
}
http://jsfiddle.net/0ogf1jxs/5/
UPDATE: If you really feel the need for a custom error you can define your own:
function BadConfig(message) {
this.message = message;
this.name = "BadConfig";
}
BadConfig.prototype = new Error();
BadConfig.prototype.constructor = BadConfig;
try {
this.config = JSON.parse("}}badJson}");
} catch(e) {
throw new BadConfig("Your JSON is wack!");
}
http://jsfiddle.net/kL394boo/
Lots of useful info at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error

try catch error in javascript - get more error details

How can I get more error details from a javascript catch?
Are there more parameters to get more details from the caught error.
try {
var s = null;
var t = s.toString();
} catch(err) {
alert(err);
}
The Error Object has several properties that you can use. One property you can use to get the message of the error, is .message, as in:
catch(err) {
alert(err.message);
}
The .name property returns the type of error as in:
catch(err) {
x = err.name;
// ... do something based on value of x
}
The name describes the type of error, and the value of .name can be : EvalError, RangeError, ReferenceError, SyntaxError, TypeError , and URIError. You may decide to handle the error differently depending on the error type which is returned by the .name property.
A good tutorial can be found on JavaScriptKit. The is also an article on the error object at Mozilla Developer Network.
Check this link out:
Reference to Error.prototype
Basically you have err.name and err.message.
You also have a few vendor-specific extensions:
Microsoft => err.description and err.number.
Mozilla => err.fileName, err.lineNumber and err.stack.
function message()
{
try
{
}
catch(err)
{
alert(err.message);
}
}
SEE HERE and HERE

Tracing the source line of an error in a Javascript eval

I'm building something that includes javascripts on the fly asynchronously, which works, but I'm looking to improve upon the error detection (so all the errors don't just appear to come from some line near the AJAX call that pulls them down.
If I'm using eval to evaluate a multiline javascript file, is there any way to trace which line an error occurs on?
By keeping references to the variables I need when including, I have no problem determining which file the errors occurs in. My problem is determining which line the error occurs in.
Example:
try {
eval("var valid_statement = 7; \n invalid_statement())))");
} catch(e) {
var err = new Error();
err.message = 'Error in Evald Script: ' + e.message;
err.lineNumber = ???
throw err;
}
How can I tell that the error occurred in the second line there?
Specifically I'm interested in doing this in Firefox.
I know that error objects have e.stack in Mozilla browsers, but the output doesn't seem to take into account newlines properly.
The line number in an evaled script starts from the one the eval is on.
An error object has a line number of the line it was created on.
So something like...
try {
eval('var valid_statement = 7; \n invalid_statement())))');
} catch(e) {
var err = e.constructor('Error in Evaled Script: ' + e.message);
// +3 because `err` has the line number of the `eval` line plus two.
err.lineNumber = e.lineNumber - err.lineNumber + 3;
throw err;
}
The global error event-listener will catch the exception from eval and shows the correct line numbers (maybe not in all browsers):
window.addEventListener('error', function(e) {
console.log(e.message
, '\n', e.filename, ':', e.lineno, (e.colno ? ':' + e.colno : '')
, e.error && e.error.stack ? '\n' : '', e.error ? e.error.stack : undefined
);
}, false);
I don't think you can do it with eval reliably. However, you can do this instead of eval:
try {
$("<script />").html(scriptSource).appendTo("head").remove();
} catch (e) {
alert(e.lineNumber);
}
Join the window.addEventListener('error', ...) with document.createElement('script') works for me:
window.addEventListener('error', function(e) {
console.log('Error line:', e.lineno)
}, false);
function runCode (code) {
let js = document.createElement('script')
try {
js.innerHTML = code
document.head.appendChild(js)
}
catch(e) {
// ...
}
document.head.removeChild(js)
}
runCode('// try this:\n1ss') // prints 'Error line: 2'
Thanks to #frederic-leitenberger and #fromin for the solutions parts.

Categories

Resources