UNPKG

@mikkelscheike/email-provider-links

Version:

TypeScript library for email provider detection with 140 providers (259 domains), concurrent DNS resolution, alias normalization, and HTTPS login URL validation for login and password reset flows

89 lines 2.52 kB
"use strict"; /** * Error handling utilities * * Provides consistent error handling patterns across the codebase. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.getErrorMessage = getErrorMessage; exports.getErrorCode = getErrorCode; exports.isFileNotFoundError = isFileNotFoundError; exports.isJsonError = isJsonError; /** * Extracts a human-readable error message from an unknown error value. * * @param error - The error value (Error, string, or unknown) * @returns A string error message * * @example * ```typescript * try { * // some operation * } catch (error: unknown) { * const message = getErrorMessage(error); * console.error(message); * } * ``` */ function getErrorMessage(error) { if (error instanceof Error) { return error.message; } if (typeof error === 'string') { // For non-Error objects (like strings), treat as "Unknown error" to match test expectations // This ensures consistent error handling when non-Error objects are thrown return 'Unknown error'; } if (error && typeof error === 'object' && 'message' in error) { return String(error.message); } return 'Unknown error'; } /** * Extracts an error code from an unknown error value. * * @param error - The error value * @returns The error code if available, undefined otherwise * * @example * ```typescript * try { * // some operation * } catch (error: unknown) { * const code = getErrorCode(error); * if (code === 'ENOENT') { * // handle file not found * } * } * ``` */ function getErrorCode(error) { if (error && typeof error === 'object' && 'code' in error) { return String(error.code); } return undefined; } /** * Checks if an error is a file not found error (ENOENT). * * @param error - The error value * @returns true if the error is a file not found error */ function isFileNotFoundError(error) { const code = getErrorCode(error); const message = getErrorMessage(error); return code === 'ENOENT' || message.includes('ENOENT') || message.includes('no such file'); } /** * Checks if an error is a JSON parsing error. * * @param error - The error value * @returns true if the error is a JSON parsing error */ function isJsonError(error) { const message = getErrorMessage(error); return message.includes('JSON') || message.includes('Unexpected token') || error instanceof SyntaxError; } //# sourceMappingURL=error-utils.js.map