Skip to content

Commit cd6680f

Browse files
Copilotmathiasrw
andauthored
Handle errors for async IndexedDB operations to fix #845 (#2314)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: mathiasrw <[email protected]>
1 parent ace837b commit cd6680f

File tree

2 files changed

+129
-11
lines changed

2 files changed

+129
-11
lines changed

src/91indexeddb.js

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,29 @@ IDB.showDatabases = function (like, cb) {
7979

8080
IDB.createDatabase = async function (ixdbid, args, ifnotexists, dbid, cb) {
8181
const found = await _databaseExists(ixdbid).catch(err => {
82-
if (cb) cb(null, err);
82+
if (cb) {
83+
cb(null, err);
84+
return null;
85+
}
8386
throw err;
8487
});
8588

89+
if (found === null) {
90+
return; // Error already handled via callback
91+
}
92+
8693
if (found) {
8794
if (ifnotexists) {
8895
cb && cb(0);
8996
} else {
9097
const err = new Error(
9198
`IndexedDB: Cannot create new database "${ixdbid}" because it already exists`
9299
);
93-
if (cb) cb(null, err);
100+
if (cb) {
101+
cb(null, err);
102+
return;
103+
}
104+
throw err;
94105
}
95106
} else {
96107
const request = indexedDB.open(ixdbid, 1);
@@ -103,10 +114,17 @@ IDB.createDatabase = async function (ixdbid, args, ifnotexists, dbid, cb) {
103114

104115
IDB.dropDatabase = async function (ixdbid, ifexists, cb) {
105116
const found = await _databaseExists(ixdbid).catch(err => {
106-
if (cb) cb(null, err);
117+
if (cb) {
118+
cb(null, err);
119+
return null;
120+
}
107121
throw err;
108122
});
109123

124+
if (found === null) {
125+
return; // Error already handled via callback
126+
}
127+
110128
if (found) {
111129
const request = indexedDB.deleteDatabase(ixdbid);
112130
request.onsuccess = () => {
@@ -116,26 +134,39 @@ IDB.dropDatabase = async function (ixdbid, ifexists, cb) {
116134
if (ifexists) {
117135
cb && cb(0);
118136
} else {
119-
cb &&
137+
if (cb) {
120138
cb(
121139
null,
122-
new Error(`IndexedDB: Cannot drop new database "${ixdbid}" because it does not exist'`)
140+
new Error(`IndexedDB: Cannot drop database "${ixdbid}" because it does not exist`)
123141
);
142+
return;
143+
}
144+
throw new Error(`IndexedDB: Cannot drop database "${ixdbid}" because it does not exist`);
124145
}
125146
}
126147
};
127148

128149
IDB.attachDatabase = async function (ixdbid, dbid, args, params, cb) {
129150
const found = await _databaseExists(ixdbid).catch(err => {
130-
if (cb) cb(null, err);
151+
if (cb) {
152+
cb(null, err);
153+
return null;
154+
}
131155
throw err;
132156
});
133157

158+
if (found === null) {
159+
return; // Error already handled via callback
160+
}
161+
134162
if (!found) {
135163
const err = new Error(
136164
`IndexedDB: Cannot attach database "${ixdbid}" because it does not exist`
137165
);
138-
if (cb) cb(null, err);
166+
if (cb) {
167+
cb(null, err);
168+
return;
169+
}
139170
throw err;
140171
}
141172

@@ -172,15 +203,25 @@ IDB.attachDatabase = async function (ixdbid, dbid, args, params, cb) {
172203
IDB.createTable = async function (databaseid, tableid, ifnotexists, cb) {
173204
const ixdbid = alasql.databases[databaseid].ixdbid;
174205
const found = await _databaseExists(ixdbid).catch(err => {
175-
if (cb) cb(null, err);
206+
if (cb) {
207+
cb(null, err);
208+
return null;
209+
}
176210
throw err;
177211
});
178212

213+
if (found === null) {
214+
return; // Error already handled via callback
215+
}
216+
179217
if (!found) {
180218
const err = new Error(
181219
'IndexedDB: Cannot create table in database "' + ixdbid + '" because it does not exist'
182220
);
183-
if (cb) cb(null, err);
221+
if (cb) {
222+
cb(null, err);
223+
return;
224+
}
184225
throw err;
185226
}
186227

@@ -206,15 +247,25 @@ IDB.createTable = async function (databaseid, tableid, ifnotexists, cb) {
206247
IDB.dropTable = async function (databaseid, tableid, ifexists, cb) {
207248
const ixdbid = alasql.databases[databaseid].ixdbid;
208249
const found = await _databaseExists(ixdbid).catch(err => {
209-
if (cb) cb(null, err);
250+
if (cb) {
251+
cb(null, err);
252+
return null;
253+
}
210254
throw err;
211255
});
212256

257+
if (found === null) {
258+
return; // Error already handled via callback
259+
}
260+
213261
if (!found) {
214262
const err = new Error(
215263
'IndexedDB: Cannot drop table in database "' + ixdbid + '" because it does not exist'
216264
);
217-
if (cb) cb(null, err);
265+
if (cb) {
266+
cb(null, err);
267+
return;
268+
}
218269
throw err;
219270
}
220271

test/test845-B.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
if (typeof exports === 'object') {
2+
var assert = require('assert');
3+
var alasql = require('..');
4+
} else {
5+
__dirname = '.';
6+
}
7+
8+
/*
9+
Test for issue #845 - Problem catching errors using the simple notation
10+
*/
11+
12+
var test = '845-B';
13+
14+
if (typeof exports != 'object') {
15+
describe('Test ' + test + ' - Error handling with ATTACH IndexedDB', function () {
16+
it('1. Should catch error when attaching non-existent database with errorlog enabled', function (done) {
17+
alasql.options.errorlog = true;
18+
alasql('ATTACH INDEXEDDB DATABASE nonExistentDB845;', function (data, err) {
19+
assert(err, 'Error should be passed to callback');
20+
assert(
21+
err.message.includes('does not exist'),
22+
'Error message should indicate database does not exist'
23+
);
24+
alasql.options.errorlog = false;
25+
done();
26+
});
27+
});
28+
29+
it('2. Should catch error using errorlog function', function (done) {
30+
var errorCaught = false;
31+
alasql.options.errorlog = function (err) {
32+
errorCaught = true;
33+
assert(err, 'Error should be passed to errorlog function');
34+
};
35+
alasql('ATTACH INDEXEDDB DATABASE nonExistentDB845b;', function (data, err) {
36+
assert(err, 'Error should also be passed to callback');
37+
alasql.options.errorlog = false;
38+
done();
39+
});
40+
});
41+
42+
it('3. Should work when database exists', async function () {
43+
// First create a database
44+
await alasql.promise('DROP INDEXEDDB DATABASE IF EXISTS testDB845');
45+
await alasql.promise('CREATE INDEXEDDB DATABASE testDB845');
46+
47+
// Now attach it with error handling enabled
48+
alasql.options.errorlog = true;
49+
const result = await new Promise((resolve, reject) => {
50+
alasql('ATTACH INDEXEDDB DATABASE testDB845;', function (data, err) {
51+
if (err) {
52+
reject(err);
53+
} else {
54+
resolve(data);
55+
}
56+
});
57+
});
58+
59+
assert.equal(result, 1, 'Should successfully attach existing database');
60+
alasql.options.errorlog = false;
61+
62+
// Cleanup
63+
await alasql.promise('DETACH DATABASE testDB845');
64+
await alasql.promise('DROP INDEXEDDB DATABASE testDB845');
65+
});
66+
});
67+
}

0 commit comments

Comments
 (0)