import { WebServiceClientError, WebServiceErrorCode } from './types.js'; /* tslint:disable:max-classes-per-file */ /** * An error returned by the GeoIP2 web service or encountered while * communicating with it. * * In addition to the standard `Error` properties (including `cause`, which * holds the underlying error when one exists, such as the network error * behind a `FETCH_ERROR`), it exposes the `code`, `status`, and `url` * associated with the failure. */ export class WebServiceError extends Error implements WebServiceClientError { /** * The error code returned by the web service or generated by this client. */ public readonly code: WebServiceErrorCode; /** * A human-readable description of the error. This is an alias of `message`, * retained for backward compatibility. */ public readonly error: string; /** * The HTTP status code, when the error originated from an HTTP response. * * Declared with `declare` so that no class field is emitted: the property is * absent (rather than set to `undefined`) when no status applies, e.g. on * network-level errors such as `FETCH_ERROR` and `NETWORK_TIMEOUT`. */ declare public readonly status?: number; /** * The URL that was being requested when the error occurred. */ public readonly url: string; constructor( properties: { code: WebServiceErrorCode; error: string; status?: number; url: string; }, options?: { cause?: unknown } ) { super(properties.error, options); this.code = properties.code; this.error = properties.error; // Only assign `status` when present so it stays genuinely optional: on // network-level errors (e.g. FETCH_ERROR, NETWORK_TIMEOUT) the instance // carries no `status` property at all, rather than `status: undefined`. if (properties.status !== undefined) { this.status = properties.status; } this.url = properties.url; } } // Set `name` on the prototype rather than in the constructor. Using a string // literal keeps the name correct under minification, and keeping it off the // instance means it stays out of `JSON.stringify` output. WebServiceError.prototype.name = 'WebServiceError'; /** * This error is thrown when the IP address is not found in the database. * This generally means that the address was a private or reserved address. */ export class AddressNotFoundError extends Error { constructor(message: string, options?: { cause?: unknown }) { super(message, options); this.name = this.constructor.name; } } /** * This error is thrown if the database file and the reader method do not match. * e.g. `reader.city` is used with a Country database */ export class BadMethodCallError extends Error { constructor(message: string, options?: { cause?: unknown }) { super(message, options); this.name = this.constructor.name; } } /** * This error is thrown if a database buffer is not a valid database */ export class InvalidDbBufferError extends Error { constructor(message: string, options?: { cause?: unknown }) { super(message, options); this.name = this.constructor.name; } } /** * This error is thrown when an argument is invalid, such as an invalid IP * address or an invalid `WebServiceClient` option. */ export class ValueError extends Error { constructor(message: string, options?: { cause?: unknown }) { super(message, options); this.name = this.constructor.name; } }