Skip to content

Commit 3eb8fc8

Browse files
committed
Update BedrockErrors.
1 parent f9d2b46 commit 3eb8fc8

File tree

5 files changed

+30
-30
lines changed

5 files changed

+30
-30
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# bedrock-mongodb ChangeLog
22

3+
## 11.0.0 - 2025-mm-dd
4+
5+
### Changed
6+
- **BREAKING**: Use mongodb driver 6.x.
7+
- **BREAKING**: Update error names to match bedrock best practice.
8+
39
## 10.2.0 - 2024-02-28
410

511
### Changed

lib/authn.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,13 @@ function _checkServerVersion({serverInfo, config}) {
174174
});
175175
if(config.requirements.serverVersion &&
176176
!semver.satisfies(version, config.requirements.serverVersion)) {
177-
throw new BedrockError(
178-
'Unsupported database version.',
179-
'DatabaseError', {
177+
throw new BedrockError('Unsupported database version.', {
178+
name: 'VersionError',
179+
details: {
180180
url: urls.sanitize(config.url),
181181
version,
182182
required: config.requirements.serverVersion
183-
});
183+
}
184+
});
184185
}
185186
}

lib/helpers.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* Copyright 2012 - 2024 Digital Bazaar, Inc.
2+
* Copyright 2012 - 2025 Digital Bazaar, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,8 +25,6 @@ import {
2525
MDBE_DUPLICATE_ON_UPDATE
2626
} from './exceptions.js';
2727

28-
const {util: {BedrockError}} = bedrock;
29-
3028
// load config defaults
3129
import './config.js';
3230

@@ -44,13 +42,9 @@ export const writeOptions = bedrock.config.mongodb.writeOptions;
4442
*/
4543
export function hash(key) {
4644
if(typeof key !== 'string') {
47-
throw new BedrockError(
48-
'Invalid key given to database hash method.',
49-
'InvalidKey', {key});
45+
throw new TypeError('"key" must be a string.');
5046
}
51-
const md = crypto.createHash('sha256');
52-
md.update(key, 'utf8');
53-
return md.digest('base64');
47+
return crypto.createHash('sha256').update(key, 'utf8').digest('base64');
5448
}
5549

5650
/**

lib/index.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,11 @@ async function _init() {
158158
}
159159
} catch(error) {
160160
logger.error('could not initialize database', {error});
161-
throw new BedrockError(
162-
'Could not initialize database.',
163-
'DatabaseError', {
164-
url: urls.sanitize(config.url)
165-
}, error);
161+
throw new BedrockError('Could not initialize database.', {
162+
name: 'OperationError',
163+
details: {url: urls.sanitize(config.url)},
164+
cause: error
165+
});
166166
}
167167
}
168168

@@ -185,22 +185,21 @@ async function _initDatabase() {
185185
' username=' + config.username +
186186
' url=' + urls.sanitize(config.url));
187187
}
188-
throw new BedrockError(
189-
'Could not initialize database.',
190-
'DatabaseError', {
191-
url: urls.sanitize(config.url),
192-
}, e);
188+
throw new BedrockError('Could not initialize database.', {
189+
name: 'OperationError',
190+
details: {url: urls.sanitize(config.url)},
191+
cause: e
192+
});
193193
}
194194
} finally {
195195
// force client to close connections (do not reuse connections used to init
196196
// database as other connections will be used later that may have different
197197
// credentials)
198198
if(client) {
199199
const force = true;
200-
client.close(force).catch(error => {
201-
logger.error(
202-
'failed to close client used to initialize database', {error});
203-
});
200+
client.close(force).catch(
201+
error => logger.error(
202+
'failed to close client used to initialize database', {error}));
204203
}
205204
}
206205
}
@@ -210,7 +209,7 @@ async function _dropCollections() {
210209
throw new BedrockError(
211210
'If bedrock.config.mongodb.dropCollection.onInit is specified, ' +
212211
'bedrock.config.mongodb.dropCollection.collections must also ' +
213-
'be specified.', 'InvalidConfiguration');
212+
'be specified.', {name: 'DataError'});
214213
}
215214
// if collectionsToDrop array is empty, all collections should be dropped
216215
const cArray = bedrock.config.mongodb.dropCollections.collections;

test/mocha/10-api.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ describe('api', function() {
5757
should.exist(result);
5858
result.should.be.a('string');
5959
});
60-
it('should throw InvalidKey error if key is not a string',
60+
it('should throw TypeError error if key is not a string',
6161
async function() {
6262
let error;
6363
let result;
@@ -68,7 +68,7 @@ describe('api', function() {
6868
}
6969
should.not.exist(result);
7070
should.exist(error);
71-
error.name.should.equal('InvalidKey');
71+
error.name.should.equal('TypeError');
7272
});
7373
});
7474
describe('buildUpdate', function() {

0 commit comments

Comments
 (0)