|
| 1 | +// Shared error base class for FMC API client modules. |
| 2 | +// Both MarginApiError (lib/margin-api.js) and PositionsError (lib/positions.js) |
| 3 | +// follow the same pattern: extend Error, set this.type (one of FMC_CONSTANTS.ERROR_TYPES), |
| 4 | +// and support Error.cause chaining for stack-trace preservation. |
| 5 | +// Centralised here so adding new properties or changing the constructor signature only |
| 6 | +// requires one edit rather than two identical changes across both API modules. |
| 7 | +'use strict'; |
| 8 | + |
| 9 | +/** |
| 10 | + * Base class for typed API errors thrown by FMC's API client modules. |
| 11 | + * Subclasses (`MarginApiError`, `PositionsError`) inherit the `type` field and |
| 12 | + * Error.cause support; `this.name` is set automatically from the subclass constructor name. |
| 13 | + * Subclasses need no constructor of their own — they inherit this one, which sets |
| 14 | + * `this.name` to the subclass name via `this.constructor.name`. |
| 15 | + * |
| 16 | + * @extends Error |
| 17 | + */ |
| 18 | +class FmcApiError extends Error { |
| 19 | + /** |
| 20 | + * @param {string} message - Human-readable error description. |
| 21 | + * @param {string} type - One of `FMC_CONSTANTS.ERROR_TYPES`. |
| 22 | + * @param {ErrorOptions} [options] - Standard Error options; pass `{ cause: originalError }` |
| 23 | + * to chain the original fetch/parse exception so DevTools shows the full error stack. |
| 24 | + * Supported in Chrome 93+ (well within the manifest's minimum_chrome_version: 111). |
| 25 | + */ |
| 26 | + constructor(message, type, options) { |
| 27 | + super(message, options); |
| 28 | + this.type = type; |
| 29 | + // Set this.name to the concrete subclass name so stack traces read |
| 30 | + // 'MarginApiError' or 'PositionsError' rather than the generic 'Error'. |
| 31 | + // this.constructor.name resolves correctly for named class expressions. |
| 32 | + // No minification is applied (extension loaded unpacked), so names are stable. |
| 33 | + this.name = this.constructor.name; |
| 34 | + } |
| 35 | +} |
0 commit comments