I've got quite a big library written in node.js, too big to paste here (>20 files). I've got a try..catch that catches the error, I debug the catch part and I console.log the error as below:
{ [ParseError: value.key.split is not a function]
message: 'value.key.split is not a function',
path: [ 'properties', 'statement1' ],
name: 'ParseError' }
The thing is - I don't know which file is that and I'm somehow unable to debug it. Can I access the source file (line, preferably) where this error was thrown? What node.js returns is the actual line that it tried to execute, but I can hardly spot where it is... Any suggestions would be welcome.
Try
console.dir( error );
on error instead. You also might try logging the stack property explicitly:
console.log( error.stack );
Related
I'm currently building an application for file backup, and it got quite a bit of reading and writing to the filesystem. Most of it works great, but I'm struggling a bit with the error handling of the app.
In the screenshot below the last path is not a valid directory and return an exception as you can see.
function getTotalSize(pathToDir, dir) {
fs.readdir(pathToDir, function(err, files) {
if (err) {
// handle my error here
throw new Error('something bad happened');
return;
}
// continue if no errors :)
My question is, is it possible to replace the standard error window with my own? Or in some cases ignore the pop up of the error window? First time working with Electron so sorry if this is an obvious one.
Thanks!
When you throw the error from readdir it gets caught by the top-level uncaughtException handler, indicated by the the first line: "Uncaught Exception".
What you need to do is add your own custom handler for the uncaughtException in your main process and show whatever dialog you want from there.
Take a look at the dialog module.
As an example, you can use the dialog.showMessageBox method to configure all sorts of things about the error dialog like this:
process.on("uncaughtException", (err) => {
const messageBoxOptions = {
type: "error",
title: "Error in Main process",
message: "Something failed"
};
dialog.showMessageBoxSync(messageBoxOptions);
// I believe it used to be the case that doing a "throw err;" here would
// terminate the process, but now it appears that you have to use's Electron's
// app module to exit (process.exit(1) seems to not terminate the process)
app.exit(1);
});
So I'm creating a mod for the singleplayer browser game Cookie Clicker. In my mod I allow the user to insert in their own code to do their own special things to interact with my mod's main function.
However, when the user codes on my custom editor, I want to "test" their code before they save to make sure no errors happen, and if they do, display a error message with what they did and where they did it. Getting the error is easy with a try/catch. But I noticed the error message is:
SynaxError: missing ) after argument list
at new Function (<anonymous>)
at HTMLAnchorElement.save.onclick (chrome-extension://dhdgffkkebhmkfjojejmpbldmpobfkfo/userscript.html?name=Building%2520Sorter.user.js&id=18320655-b018-42e2-8fa5-7fb0cc8d2d70:578:24)
Which isn't helpful for me at all. The most I could salvage from this is the first line. However, that doesn't tell the user at all where the error is located in their code.
the 578:24 that points to the supposed error is:
try{
//code.value is a STRING of the user's code
let func = new Function(code.value);//<-- error points here in my source code.
func.call(null, [[0, 1, 2], Game]);
save.classList.remove('ModBuildingSorter_unsaved');
}
catch(e){
console.dir(e);
}
What I would like to happen is when the user sumbits:
return function(array){
return array.sort(function(building1,building2){
return building1.price - building2.price;
};// missing array.sort closing parenthesis
}
get's ran, I can get a syntax error telling me it's on line 4
Is there a way I can do this? Make the user's code act kinda like it's own file and try running it so I can find out which row & column the error is located?
You could, in theory, run the function from an eval()
i.e.:
try {
let a = "function test(o){console.lo(o)}test('hello');" // Minified function
eval(a)
} catch (e) {
console.log(e)
}
Here is the unminified function for example purposes:
function test(o)
{
console.lo(o) // <-- Error
}
test('hello');
and this returns the error correctly, which is
TypeError: console.lo is not a function
at test (eval at <anonymous> (D:\StackOverflowSandbox\index.js:3:5), <anonymous>:1:26)
Hope I've helped.
I can't work out why the library I have installed (nightmare is showing me errors). The errors are mainly on lines that contain :
=>
the error shows as expression expected.
when running my program I get this error from the nightmare.js:
return endInstance(self, () => fn.apply(self, doneargs));
^
TypeError: Cannot read property 'apply' of undefined
I get these errors too:
electronLog.stdout(data);
expected :
In my code I can't use statements like this either:
.evaluate(() =>
document.querySelector('#zero_click_wrapper .c-info__title a').href
)
Error: statement expected.
Any ideas would be much appreciated. All my other node modules are fine.
This is a weird one. If you require a module with a syntax error in it. You can catch the error, however node or v8 or some part of the system will print the syntax error to the console, even if you catch the error. You can replicate the issue by placing the following three files in the same directory and running "node example.js". How do I prevent this output? Redirecting isn't a viable solution, because I want to use stderr and stdout in my program. I want complete control of the error output. I've been searching through the V8 and node.js source, I can't seem to find where it is happening, so I can't tell how to stop it.
Thanks!
File one:
// example.js
try{ require('./error-one.js'); }
catch(e){ console.log("Caught:", e); }
try{ require('./error-two.js'); }
catch(e){ console.log("Caught:", e); }
File two:
// error-one.js
var a = { "b" c };
File three:
// error-two.js
var d = { "e" f };
EDIT: To clarify, the line I can't seem to remove is "var a = { "b" c};" with the arrows under it that appears before the caught errors. As I said in the comments, the odd part is that it doesn't appear twice. Only for the first file with a syntax error.
today I had a strange problem in my serverside javascript.
I have a scriptLibrary in which I have some error-handling (try {...} catch(e) {...})
But somethings wrong with the variable "e". On the command e.printStacktrace() (which works useally) I got an exception: Error calling method 'printStackTrace()' on an object of type 'Error [JavaScript Object]'
I have another library, in which I pass the exception to a Java Class which creates a log document in my database, using it thows another exception which says that the variable "e" is not a thowable Exception. Checking that with a print(typeof e) return only "object".
Shouldn't that be a kind of exception on which I can use the standard methods? Do you have any idea what could cause that?
Thanks in advance.
Matthias
The error is not a Java Exception, this is why there is no stack information and no printStackTrace() method available.
Your code throws a Javascript error object. Try e.getMessage() or just a print(e) to get the reason for the failing of your code.