-
-
Notifications
You must be signed in to change notification settings - Fork 393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
new Database ( incorrectPathToDB, { fileMustExist: true } ) does not throw error (while readOnly misspell does) #628
Comments
This works as expected for me: const fs = require('fs');
const betterSQLite3 = require('better-sqlite3');
const incorrectPathToDB = 'nope.db'
try {
console.log(fs.existsSync(incorrectPathToDB));
let db = new betterSQLite3( incorrectPathToDB, {
readonly: false,
fileMustExist: true,
} );
}
catch ( error ) {
console.log( 'Disaster. All is... Wait... we didn’t catch anything?' ) // not shown in the console
console.log( error ); // nothing... silence
}
Maybe your path is an empty string and you're creating a temporary database? Turns out this is not documented that an empty string does that (it's SQLite behavior) https://github.com/JoshuaWise/better-sqlite3/blob/02c9c250bc77311b08bec574d3d40890c0b17256/lib/database.js#L33 |
@JoshuaWise arguably |
Also can't reproduce this. As @Prinzhorn said, probably |
Perhaps that could be a helpful error message. |
@Prinzhorn & @JoshuaWise — Yes, if path is passed through an Exactly. 😀 In my view, that’s not the behavior described by IMO:
fs.existsSync(str_PathToDB) My initial concern was raised when I just hardcoded a @JoshuaWise ’s comment somewhere that "we don’t use any fancy path logic" inspired me to input an absolute path and the DB was correctly accessed without any complaints of inexisting tables. |
OK, I drilled in a bit further down my commits… it turns out it is rather an edge case. To very quickly test a try/catch, I add an inline or outside To reproduce: const correctPathToDB = '/Users/johndoe/Applications/GoodApp/perfect.db'
const incorrectPathToDB = correctPathToDB + 1;
console.log( `Incorrect path is: ${incorrectPathToDB}` );
console.log( `Var type is: ${typeof incorrectPathToDB}` ); // string, passing a 'Expected first argument to be a string' check
try {
console.log( fs.existsSync( incorrectPathToDB ) );
let db = new betterSQLite3( incorrectPathToDB, {
readonly: false,
fileMustExist: true,
} );
}
catch ( error ) {
console.log( 'Disaster. All is... Wait... we didn’t catch anything?' ) // not shown in the console
console.log( error ); // nothing... silence
} This silently creates a My suggestion
In my case, a relative path did not work and I assembled an absolute using Node's Assuming import * as path from 'path';
import * as url from 'url';
/**
* - The import.meta object exposes context-specific metadata to a JavaScript
* module. It contains the module's URL.
* - url.fileURLToPath() converts a URL into a OS-independent absolute path
* - path.dirname() returns the directory of a path without a trailing slash
*/
const correctPathToDB = path.dirname( url.fileURLToPath(
import.meta.url ) ) + '/' + 'perfect.db'; |
If you were able to reproduce the problem with a non-empty path argument, it sounds like this is a bug in your operating system. The |
Woha! Thanks for the clear-up of the logic behind the Meanwhile, would you be interested to add an |
@JoshuaWise would love this to be solved? i'm using better-sqlite3 with docker & its very confusing which folder does not exist. i would love a better description of the error message than this one. having seen this error 100x today. |
// Make sure the specified directory exists
if (!anonymous && !fs.existsSync(path.dirname(filename))) {
throw new TypeError('Cannot open database because the directory ' + path.dirname(filename) + 'does not exist');
} wouldn't this be enough here? edit: i guess i can log the console.log(url)
const client = sqlite(url, { verbose: console.log }) |
Unless I’m too sleep deprived and missing something obvious, I can’t find any logic to throw an error on incorrect DB file path (there’s only a partial check for directory existence "Cannot open database because the directory does not exist").
So the new class inst code proceeds to attempt to create a temp DB and look for a non-existent table inside. No error caught:
Does not work — no error caught
Proceeding to:
Works — error caught
using the:
Related to:
#220
The text was updated successfully, but these errors were encountered: