Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

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

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

CMCDragonkai opened this issue Jan 12, 2022 · 2 comments

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants