Skip to content
This repository has been archived by the owner on Dec 2, 2024. It is now read-only.

How to get the exception Inner database is not open? #109

Closed
CMCDragonkai opened this issue Jan 12, 2022 · 2 comments
Closed

How to get the exception Inner database is not open? #109

CMCDragonkai opened this issue Jan 12, 2022 · 2 comments
Labels
stale This issue or pull request is old

Comments

@CMCDragonkai
Copy link

I've noticed in my codebase when creating the sublevel, internally this error Inner database is not open gets thrown.

It's immediately applied to the cb inside the _open.

When using subleveldown, the call itself appears asynchronous.

So I have to use the open callback to know when the subleveldown is fully established.

However there doesn't seem to be a way to catch this exception of Inner database is not open.

I'm also not sure what is causing this exception.

@CMCDragonkai
Copy link
Author

I found a way to do this:

      return await new Promise<DBLevel>((resolve, reject) => {
        const dbLevelNew = subleveldown(dbLevel, domain, {
          keyEncoding: 'binary',
          valueEncoding: 'binary',
          open: (cb) => {
            // This `cb` is defaulted (hardcoded) to a function that emits an error event
            // When using `level`, we are able to provide a callback that overrides this `cb`
            // However `subleveldown` does not provide a callback parameter
            // It provides this `open` option, which requires us to call `cb` to finish
            // If we provide an exception as a parameter, it will be received by the `error` event handler
            cb(undefined);
            resolve(dbLevelNew);
          },
        });
        // @ts-ignore error event for subleveldown
        dbLevelNew.on('error', (e) => {
          // Errors during construction of the sublevel will be emitted as events
          reject(e);
        });
      });

It would preferable that subleveldown matches the API of level, and level provides the ability to just pass the callback.

Atm even the types think that error is not a legitimate event. But it is indeed emitted.

Now I can actually handle the error properly:

(node:4099911) UnhandledPromiseRejectionWarning: OpenError: Inner database is not open
    at /home/cmcdragonkai/Projects/js-db/node_modules/subleveldown/node_modules/levelup/lib/levelup.js:119:23
    at /home/cmcdragonkai/Projects/js-db/node_modules/deferred-leveldown/node_modules/abstract-leveldown/abstract-leveldown.js:38:14
    at /home/cmcdragonkai/Projects/js-db/node_modules/deferred-leveldown/deferred-leveldown.js:31:21
    at /home/cmcdragonkai/Projects/js-db/node_modules/encoding-down/node_modules/abstract-leveldown/abstract-leveldown.js:38:14
    at /home/cmcdragonkai/Projects/js-db/node_modules/subleveldown/node_modules/abstract-leveldown/abstract-leveldown.js:38:14
    at onopen (/home/cmcdragonkai/Projects/js-db/node_modules/subleveldown/leveldown.js:142:50)
    at processTicksAndRejections (internal/process/task_queues.js:77:11)

@vweevers
Copy link
Member

It would preferable that subleveldown matches the API of level

On that point: https://github.com/Level/abstract-level

@vweevers vweevers added the stale This issue or pull request is old label Dec 2, 2024
@vweevers vweevers closed this as completed Dec 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
stale This issue or pull request is old
Projects
None yet
Development

No branches or pull requests

2 participants