Skip to content

Commit d301495

Browse files
authored
chore: PouchDB-find adapters/http update (#8912)
* chore: refactor dbFetch to async function * feat: dbFetch throw instance of PouchError
1 parent acc12f6 commit d301495

File tree

2 files changed

+97
-34
lines changed

2 files changed

+97
-34
lines changed

packages/node_modules/pouchdb-find/src/adapters/http/index.js

Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,72 @@
1-
import { generateErrorFromResponse } from 'pouchdb-errors';
1+
import { createError, generateErrorFromResponse } from 'pouchdb-errors';
22
import { Headers } from 'pouchdb-fetch';
33
import massageCreateIndexRequest from '../../massageCreateIndexRequest';
44
import validateSelector from '../../validateSelector';
55

6-
function dbFetch(db, path, opts, callback) {
7-
var status, ok;
8-
opts.headers = new Headers({'Content-type': 'application/json'});
9-
db.fetch(path, opts).then(function (response) {
10-
status = response.status;
11-
ok = response.ok;
12-
return response.json();
13-
}).then(function (json) {
14-
if (!ok) {
15-
json.status = status;
16-
var err = generateErrorFromResponse(json);
17-
callback(err);
18-
} else {
19-
callback(null, json);
20-
}
21-
}).catch(callback);
6+
async function dbFetch(db, path, opts) {
7+
if (opts.body) {
8+
opts.body = JSON.stringify(opts.body);
9+
opts.headers = new Headers({ 'Content-type': 'application/json' });
10+
}
11+
12+
const response = await db.fetch(path, opts);
13+
const json = await response.json();
14+
if (!response.ok) {
15+
json.status = response.status;
16+
const pouchError = createError(json);
17+
throw generateErrorFromResponse(pouchError);
18+
}
19+
return json;
2220
}
2321

2422
function createIndex(db, requestDef, callback) {
25-
requestDef = massageCreateIndexRequest(requestDef);
2623
dbFetch(db, '_index', {
2724
method: 'POST',
28-
body: JSON.stringify(requestDef)
29-
}, callback);
25+
body: massageCreateIndexRequest(requestDef)
26+
})
27+
.then((result) => {
28+
callback(null, result);
29+
})
30+
.catch(callback);
3031
}
3132

3233
function find(db, requestDef, callback) {
3334
validateSelector(requestDef.selector, true);
3435
dbFetch(db, '_find', {
3536
method: 'POST',
36-
body: JSON.stringify(requestDef)
37-
}, callback);
37+
body: requestDef
38+
})
39+
.then((result) => {
40+
callback(null, result);
41+
})
42+
.catch(callback);
3843
}
3944

4045
function explain(db, requestDef, callback) {
4146
dbFetch(db, '_explain', {
4247
method: 'POST',
43-
body: JSON.stringify(requestDef)
44-
}, callback);
48+
body: requestDef
49+
})
50+
.then((result) => {
51+
callback(null, result);
52+
})
53+
.catch(callback);
4554
}
4655

4756
function getIndexes(db, callback) {
48-
dbFetch(db, '_index', {
49-
method: 'GET'
50-
}, callback);
57+
return dbFetch(db, '_index', {
58+
method: 'GET',
59+
})
60+
.then((result) => {
61+
callback(null, result);
62+
})
63+
.catch(callback);
5164
}
5265

5366
function deleteIndex(db, indexDef, callback) {
54-
55-
56-
var ddoc = indexDef.ddoc;
57-
var type = indexDef.type || 'json';
58-
var name = indexDef.name;
67+
const ddoc = indexDef.ddoc;
68+
const type = indexDef.type || 'json';
69+
const name = indexDef.name;
5970

6071
if (!ddoc) {
6172
return callback(new Error('you must provide an index\'s ddoc'));
@@ -65,9 +76,13 @@ function deleteIndex(db, indexDef, callback) {
6576
return callback(new Error('you must provide an index\'s name'));
6677
}
6778

68-
var url = '_index/' + [ddoc, type, name].map(encodeURIComponent).join('/');
79+
const url = '_index/' + [ddoc, type, name].map(encodeURIComponent).join('/');
6980

70-
dbFetch(db, url, {method: 'DELETE'}, callback);
81+
dbFetch(db, url, { method: 'DELETE' })
82+
.then((result) => {
83+
callback(null, result);
84+
})
85+
.catch(callback);
7186
}
7287

7388
export {

tests/find/test-suite-1/test.errors.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,52 @@ describe('test.errors.js', function () {
163163
});
164164
});
165165
});
166+
167+
it('should throw an instance of error if createIndex throws', async () => {
168+
const db = context.db;
169+
170+
try {
171+
await db.createIndex({});
172+
} catch (exception) {
173+
//! FIXME check for instance of PouchError if available
174+
exception.should.be.instanceOf(Error);
175+
}
176+
});
177+
178+
it('should throw an instance of error if find throws', async () => {
179+
const db = context.db;
180+
181+
try {
182+
await db.find({ selector: [] });
183+
} catch (exception) {
184+
//! FIXME check for instance of PouchError if available
185+
exception.should.be.instanceOf(Error);
186+
}
187+
});
188+
189+
it('should throw an instance of error if explain throws', async () => {
190+
const db = context.db;
191+
192+
try {
193+
await db.explain({});
194+
} catch (exception) {
195+
//! FIXME check for instance of PouchError if available
196+
exception.should.be.instanceOf(Error);
197+
}
198+
});
199+
200+
it('should throw an instance of error if deleteIndex throws', async () => {
201+
const db = context.db;
202+
203+
try {
204+
await db.deleteIndex({
205+
ddoc: "foo",
206+
name: "bar"
207+
});
208+
}
209+
catch (exception) {
210+
//! FIXME check for instance of PouchError if available
211+
exception.should.be.instanceOf(Error);
212+
}
213+
});
166214
});

0 commit comments

Comments
 (0)