node writeFileSync not writing to file - javascript

Here is the command that is broken:
fs.writeFileSync('Metadata.json', metadataString);
console.log("Metadata written.");
I have placed a breakpoint and verified that metadataString is actually a string. The console logs Metadata written just fine and the file stays empty...
Not sure what else I can check... Thanks in advance.

fs.writeFileSync() throws an Error exception if it fails. Use a try/catch block:
try {
fs.writeFileSync('Metadata.json', metadataString);
} catch (err) {
console.log('Error writing Metadata.json:' + err.message)
}

Related

Errors not showing in ts-node

When I have an Error in my code, compiling with ts-node, the error does not appear in the console.
For exemple:let data = await fs.readFileSync(path);
I have this code where I use "fs" to read a file, using a path that the function receive as parameter. But when the path is from a file that does not exists nothing happends in the console. The only thing that happens is that the requisition never finish loading.
If I put a console.log like this in the code, the error appears saying that the file does not exists : console.log(fs.readFile(path, (data) => { }))
It doesn't make sense, since the errors are supposed to show in the console. (This happens with lot of type of commum errors that make it harder to finish the application)
here's the repository with all the code https://github.com/Macintosh-1984/Pokedex_Express
somebody help please
You are not capture the error on readFile
fs.readFile(path, 'utf8', function(err, data){
// you have err here
});

Sending error in a specific channel Discord.js

I have a try...catch system to send an error to a channel when there is an error.
Here is the code:
try {
cmd.execute(client, message, args, prefix, result);
} catch (err) {
echannel.send(err);
}
echannel is the channel that I got with client.channels.cache.get(). The channel is the correct one.
I made a command to throw the error with:
message.channel.send('Throwing error...');
throw new Error('Testing');
I can see the error in the console though. Any help?
Try to simply send the error to the channel:
message.channel.send(err);
I've figured it out. I need to add process.on('uncaughtException, err => {})

Nodejs process freezes

I have a huge script (3 modules x 2000 lines each one) which runs about 3000 times per day. Few times a week it freezes. Nodejs process is still running on Linux server, but nothing happens, it just stops working without any understandable reason. I've tried to write logs everywhere it was possible with try ... catch, but there is still no errors or warnings. It hangs in different places, different loops. There are also no errors in server logs.
Are there any other ways to debug such problem?
in your main file( the file which your start app) put these codes :
// to log all bugs that occured in synchronus code but not handlled anywhere (add below code on top of your main file)
process.on('uncaughtException', err => {
console.log('uncaughtException!! shutting down...');
console.log(err.name, err.message);
process.exit(1);
});
// to log all unhandledRejection in you APP (Async Code)
process.on('unhandledRejection', (err) => {
console.error(`Error Name : ${err.name}\nError Message : ${err.message}`);
console.error(`unhandledRejection!💥 Shutting Down...`);
process.exit(1); // 0 for success & 1 for uncaught exeptions
});
By these lines of codes, when any problem happen in your app you can observe errors wich you haven't handle on your code.

javascript try catch statement not working in my code

I got "Uncaught SyntaxError: Invalid or unexpected token" error.
And try to catch with 'try ~ catch' but it's not working.
function a(){
try{
var img1 = "==""; <#-- it occurs error -->
}catch (e) {
console.log("image error:" + e.message);
}
}
You have a syntax error and you can not catch a syntax error.
This error is checked at the parsing or validation time of your code. The try .. catch statement is executed at the running time. And because of this it was not functioning.
If you eval or parse (for ex. JSON) your code you can handle syntax errors only. Or you can create a syntax error like this:
try {
throw new SyntaxError('Hello', 'someFile.js', 18);
} catch (e) {
console.log(e.message); // "Hello"
console.log(e.name); // "SyntaxError"
}
For the eval handled or by self created syntax errors:
A SyntaxError is thrown when the JavaScript engine encounters tokens or token order that does not conform to the syntax of the language when parsing code.
From MDN
Try this:
function a(){
try{
var img1 = "==\"";
//but you have to put some error here without parsing or validation error of your code.
}catch (e) {
console.log("image error:" + e.message);
}
}
I would like to recommend you read:
try...catch Statement
Exception Handling State
Syntax Error

can't exec javascript with zombie.js?

ok, I am a new learner to node.js:
when I try to load google.com,and exec the script in the page,like this:
var zombie=require("zombie");
browser = new zombie.Browser({ debug: true })
browser.visit("http://www.google.com",function(err,_browser,status){
if(err){
throw(err.message);
}
})
but get error:
28 Feb 16:06:40 - The onerror handler
on target
{ frames: [ [Circular] ],
contentWindow: [Circular],
window: [Circular],
…………………………
what can I do?
The actual error message is hidden part way down the error output. Within the browser.visit function, add...
console.log(err.message);
...above throw(err.message);. This should give you a better indication of what's going wrong :)
EDIT: Having tested node 0.4.1 / zombie 0.9.1 on www.google.com, it looks like a javascript error (ILLEGAL TOKEN) is causing the problem. You can circumvent javascript errors by setting up your zombie code like so:
browser = new zombie.Browser();
browser.runScripts = false; // this disables executing javascript
browser.visit("http://www.google.com/", function (err, browser) {
if (err) { console.log('Error:' + err.message); }
else { console.log('Page loaded successfully'); }
// ... rest of your code here
});
Personally, I don't find the current zombie error output very helpful, so I prefer to turn it off and display a simple console.log message when something goes wrong.
Lastly, you can also debug errors by passing in debug: true to the browser function:
browser = new zombie.Browser({ debug: true });
This will output all calls (inc. ajax) and might help to pinpoint the source of an error.
Good luck!

Categories

Resources