mkdirsync is throwing an exception that can't be caught?
This code used to work, so I don't know what changed.
export default
{
validateEnvironment()
{
try
{
Deno.mkdirSync(config.UPLOAD_PATH); // <- THROWS
}
catch (error)
{
if(error.name == "AlreadyExists")
{
console.log("Uploads directory exists.");
}
else
{
console.error(error);
return;
}
}
console.log("Environment validated.");
}
}
I call this on startup in my main.ts. It's all synchronous. But the server halts with an exception because the directory already exists. Why?
7 Replies
This code does throw (prints AlreadyExists on console):
deno run --allow-write=./new_dir main.ts
#main.ts
try {Deno.mkdirSync("new_dir");} catch (error) {console.log(error.name)}
Of course on the first run it does not print out anything, further reruns do print AlreadyExists
Also works:
function validateEnvironment() { try {Deno.mkdirSync("new_dir");} catch (error) {console.log(error.name)}; }
// validateEnvironment();
IMO, I'm not sure exactly what's going on in your code but you seem to have dropped the function keyword from the code (before the body function). I'm not experienced at all, it's just my 0.02$
Doesn't throw on mine. Execution halts: https://imgur.com/a/H7I1ypp
You can't have the function keyword there. I'm pretty new too, so I don't know why.
So which version of deno are you using? Mine is: deno 1.45.2
I'm not sure how you tried it, do you have an asciinema or such?
If validateEnvironment() works in the code as you write it, it means you have defined it before. Otherwise validateEnvironment() {} is actually two unrelated pieces of code. It will be interpreted as:
``javascript
validateEnvironment(); /// call to some non existing function
{
/// some unrelated code
}
Thanks, but that's the only definition. However, I found the problem: https://imgur.com/a/tFYQ3id
Yeah, do validate your code independent of the tool you use
I mean do try to run your code in console (CMD on windows, iTerm on Mac, or some terminal emulator on linux)
deno <params> -> makes your problem easy to cut into smaller pieces
thanks
Anyway, I guess the news here is that some VS Code update has toggled this setting. What a PITA.