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

Cannot import BadRequestError from lib/errors #202

Closed
okomarov opened this issue Jun 4, 2021 · 7 comments
Closed

Cannot import BadRequestError from lib/errors #202

okomarov opened this issue Jun 4, 2021 · 7 comments
Labels
bug Something isn't working released

Comments

@okomarov
Copy link

okomarov commented Jun 4, 2021

Bug Report

Describe the bug

Can't import specific errors from the lib/errors

Minimal Reproduction

import { BadRequestError } from 'yahoo-finance2/dist/esm/src/lib/errors';

try {
  throw Error('Something');
} catch (e) {
  if (e instanceof BadRequestError) {
    console.log('aloha');
  }
  throw e;
}

then running it with:

yarn ts-node-dev src/test.ts

throws:

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './dist/esm/src/lib/errors' is not defined by "exports" in /Users/okomarov/Documents/repos/myrepo/api/node_modules/yahoo-finance2/package.json
at throwExportsNotFound (internal/modules/esm/resolve.js:290:9)
at packageExportsResolve (internal/modules/esm/resolve.js:513:3)
at resolveExports (internal/modules/cjs/loader.js:437:36)
at Function.Module._findPath (internal/modules/cjs/loader.js:477:31)
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:872:27)
at Function.Module._load (internal/modules/cjs/loader.js:730:27)
at Module.require (internal/modules/cjs/loader.js:957:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object. (/Users/okomarov/Documents/repos/myrepo/api/src/test.ts:1:1)
at Module._compile (internal/modules/cjs/loader.js:1068:30)

Environment

Node version (if applicable): 14.17.0
yarn version: 1.22.10
Library version (e.g. 1.10.1): 1.12.1

@okomarov okomarov added the bug Something isn't working label Jun 4, 2021
@advaiyalad
Copy link
Contributor

@okomarov This isn't a bug. The error is just node saying that you can't import it, since the exports field prohibits it. This is 100% intended behavior.

@gadicc Is it possible to make the errors public? This pattern does seem to be what MDN recommends.

@okomarov
Copy link
Author

okomarov commented Jun 4, 2021

@okomarov This isn't a bug. The error is just node saying that you can't import it, since the exports field prohibits it. This is 100% intended behavior.

@gadicc Is it possible to make the errors public? This pattern does seem to be what MDN recommends.

Thanks for the quick reply! Feel free to change bug -> feature request or close.

I was led to think I should be able to import them from the docs under Error Handling where it says "there are specific errors at..."

@advaiyalad
Copy link
Contributor

advaiyalad commented Jun 4, 2021

I see the issue now. To solve this, you could switch over the name property (I really haven't tried it out). The name of a BadRequestError would be "BadRequestError".

import yf from 'yahoo-finance2';
try {
    await yf.quote('AAPL');
} catch (err) {
    if (err.name === "BadRequestError") {
        // do something
    }
    // Check for more things...
}

Again, this code has not been tested, so no guarantees.

@gadicc
Copy link
Owner

gadicc commented Jun 5, 2021

Thanks @okomarov for reporting and yes, @PythonCreator27, I agree that the errors should be public. Watch this space.

@gadicc
Copy link
Owner

gadicc commented Jun 5, 2021

I exposed the errors in yahooFinance.errors, and improved the error handling docs.

You're welcome to check this out in a local devel copy but unless anything else comes up, I'll release this pretty soon.

gadicc pushed a commit that referenced this issue Jun 6, 2021
# [1.13.0](v1.12.1...v1.13.0) (2021-06-06)

### Features

* **errors:** expose in yf.errors; improve docs ([#202](#202)) ([a3a7da6](a3a7da6))
@gadicc
Copy link
Owner

gadicc commented Jun 6, 2021

🎉 This feature is included in version 1.13.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@gadicc
Copy link
Owner

gadicc commented Jun 6, 2021

Thanks, @okomarov! 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working released
Projects
None yet
Development

No branches or pull requests

3 participants