Skip to content

Commit

Permalink
feat(errors): expose in yf.errors; improve docs (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
gadicc committed Jun 5, 2021
1 parent aba051d commit a3a7da6
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 8 deletions.
47 changes: 43 additions & 4 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ try {
result = await yahooFinance.quote(symbol);
} catch (error) {
// Inspect error and decide what to do; often, you may want to just abort:
console.warn(`Skipping yf.quote("${symbol}"): [${error.name}] ${error.message})`);
return;
}

Expand All @@ -80,11 +81,49 @@ the result you receive will be in an expected format and structure, that
is safe to use, put in your database, perform calculations with, etc
(but please do let us know if you come across any edge cases).

There is a list of specific errors at [lib/errors.ts](../src/lib/errors.ts)
but generally we'll prevent you from making bad requests with invalid option,
etc.
There is a list of specific errors at [lib/errors.ts](../src/lib/errors.ts),
accessible via `yahooFinance.errors`, but many of these will require further
inspection at runtime. For example:

See also: [Validation](./validation.md)
* `FailedYahooValidationError` - see the [Validation](./validation.md) section
on how to handle these correctly.

* `HTTPError` - the `message` property will be the HTTP Response statusText.

* `Error` - thrown after a "successful" HTTP request that returns JSON with an
`{ error: { name: "ErrorName", description: "string" } }` shape, and where
we don't have an "ErrorName" class. The `message` property will be the
`description`.

Example:

```js
import yahooFinance from 'yahoo-finance2';

let result;
try {
result = await yahooFinance.quote(symbol);
} catch (error) {
if (error instanceof yahooFinance.errors.FailedYahooValidationError) {
// See the validation docs for examples of how to handle this
// error.result will be a partially validated / coerced result.
} else if (error instanceof yahooFinance.errors.HTTPError) {
// Probably you just want to log and skip these
console.warn(`Skipping yf.quote("${symbol}"): [${error.name}] ${error.message})`);
return;
} else {
// Same here
console.warn(`Skipping yf.quote("${symbol}"): [${error.name}] ${error.message})`);
return;
}
}

doSomethingWith(result); // safe to use in the way you expect
```


If you run into any problems with error handling, feel free to open an issue
so we can make these docs clearer.

## Validation

Expand Down
4 changes: 4 additions & 0 deletions src/index-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import yahooFinanceFetch from "./lib/yahooFinanceFetch.js";
import moduleExec from "./lib/moduleExec.js";
import options from "./lib/options.js";
import errors from "./lib/errors.js";

// modules
import autoc from "./modules/autoc.js";
Expand All @@ -24,6 +25,9 @@ export default {
_moduleExec: moduleExec,
_opts: options,

// errors
errors,

// modules,
autoc,
historical,
Expand Down
19 changes: 15 additions & 4 deletions src/lib/errors.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import type { ErrorObject } from "ajv/dist/types";

// Yahoo's servers returned an HTTP 400 for this request.
export class BadRequestError extends Error {
name = "BadRequestError";
}

// Yahoo's servers returned a 'not-ok' status for this request.
// https://developer.mozilla.org/en-US/docs/Web/API/Response/ok
export class HTTPError extends Error {
name = "HTTPError";
}

// A YahooFinance method was called with invalid options.
export class InvalidOptionsError extends Error {
name = "InvalidOptionsError";
}

// An internal method yahooFinanceFetch() was called without this._env set.
export class NoEnvironmentError extends Error {
name = "NoEnvironmentError";
}
Expand All @@ -28,14 +36,17 @@ export class FailedYahooValidationError extends Error {
}
}

interface ErrorsIndex {
// Index necessary to allow things like: const ErrorClass = errors[errorName];
type ErrorsIndex = {
[key: string]: any;
}
};

export default {
const errors: ErrorsIndex = {
BadRequestError,
HTTPError,
InvalidOptionsError,
NoEnvironmentError,
FailedYahooValidationError,
} as ErrorsIndex;
};

export default errors;

0 comments on commit a3a7da6

Please sign in to comment.