diff --git a/src/DB.ts b/src/DB.ts index e1210cbf..4941b42d 100644 --- a/src/DB.ts +++ b/src/DB.ts @@ -126,44 +126,39 @@ class DB { recursive: true, }); } catch (e) { - throw new errors.ErrorDBDelete(e.message, { - errno: e.errno, - syscall: e.syscall, - code: e.code, - path: e.path, - }); + throw new errors.ErrorDBDelete(e.message, undefined, e); } } try { await this.fs.promises.mkdir(this.dbPath); } catch (e) { if (e.code !== 'EEXIST') { - throw new errors.ErrorDBCreate(e.message, { - errno: e.errno, - syscall: e.syscall, - code: e.code, - path: e.path, - }); + throw new errors.ErrorDBCreate(e.message, undefined, e); } } - const dbLevel = await new Promise>( - (resolve, reject) => { - const db = level( - this.dbPath, - { - keyEncoding: 'binary', - valueEncoding: 'binary', - }, - (e) => { - if (e) { - reject(e); - } else { - resolve(db); - } - }, - ); - }, - ); + let dbLevel; + try { + dbLevel = await new Promise>( + (resolve, reject) => { + const db = level( + this.dbPath, + { + keyEncoding: 'binary', + valueEncoding: 'binary', + }, + (e) => { + if (e) { + reject(e); + } else { + resolve(db); + } + }, + ); + }, + ); + } catch (e) { + throw new errors.ErrorDBCreate(e.message, undefined, e); + } this._db = dbLevel; this._running = true; this.logger.info(`Started ${this.constructor.name}`); diff --git a/src/errors.ts b/src/errors.ts index 08e00af7..7dd88930 100644 --- a/src/errors.ts +++ b/src/errors.ts @@ -2,11 +2,15 @@ import type { POJO } from './types'; import { CustomError } from 'ts-custom-error'; +type ErrorChain = Error & { chain?: ErrorChain }; + class ErrorDB extends CustomError { data: POJO; - constructor(message: string = '', data: POJO = {}) { + cause?: ErrorChain; + constructor(message: string = '', data: POJO = {}, cause?: ErrorChain) { super(message); this.data = data; + this.cause = cause; } }