UNPKG

@zestic/oauth-core

Version:

Framework-agnostic OAuth authentication library with support for multiple OAuth flows

80 lines 3.6 kB
"use strict"; /** * Standardized error handling for OAuth operations */ Object.defineProperty(exports, "__esModule", { value: true }); exports.ErrorHandler = void 0; const OAuthTypes_1 = require("../types/OAuthTypes"); class ErrorHandler { static createError(message, code, originalError) { return new OAuthTypes_1.OAuthError(message, code, originalError); } static handleNetworkError(error) { return new OAuthTypes_1.OAuthError(`Network error: ${error.message}`, OAuthTypes_1.OAUTH_ERROR_CODES.NETWORK_ERROR, error); } static handleTokenExchangeError(error, response) { let message = 'Token exchange failed'; if (response && typeof response === 'object' && response !== null) { const errorResponse = response; if (errorResponse.error_description) { message = `Token exchange failed: ${String(errorResponse.error_description)}`; } else if (errorResponse.error) { message = `Token exchange failed: ${String(errorResponse.error)}`; } } return new OAuthTypes_1.OAuthError(message, OAuthTypes_1.OAUTH_ERROR_CODES.TOKEN_EXCHANGE_FAILED, error); } static handleInvalidState(expectedState, receivedState) { const message = expectedState && receivedState ? `Invalid state parameter. Expected: ${expectedState}, Received: ${receivedState}` : 'Invalid or missing state parameter'; return new OAuthTypes_1.OAuthError(message, OAuthTypes_1.OAUTH_ERROR_CODES.INVALID_STATE); } static handleMissingParameter(parameterName) { return new OAuthTypes_1.OAuthError(`Missing required parameter: ${parameterName}`, OAuthTypes_1.OAUTH_ERROR_CODES.MISSING_REQUIRED_PARAMETER); } static handleInvalidConfiguration(message) { return new OAuthTypes_1.OAuthError(`Invalid configuration: ${message}`, OAuthTypes_1.OAUTH_ERROR_CODES.INVALID_CONFIGURATION); } static handleUnknownFlow(flowName) { return new OAuthTypes_1.OAuthError(`Unknown flow: ${flowName}`, OAuthTypes_1.OAUTH_ERROR_CODES.UNKNOWN_FLOW); } static handleNoFlowHandler() { return new OAuthTypes_1.OAuthError('No suitable flow handler found for the provided parameters', OAuthTypes_1.OAUTH_ERROR_CODES.NO_FLOW_HANDLER); } static handleFlowValidationFailed(flowName, reason) { const message = reason ? `Flow validation failed for ${flowName}: ${reason}` : `Flow validation failed for ${flowName}`; return new OAuthTypes_1.OAuthError(message, OAuthTypes_1.OAUTH_ERROR_CODES.FLOW_VALIDATION_FAILED); } static isOAuthError(error) { return error instanceof OAuthTypes_1.OAuthError; } static getErrorCode(error) { if (this.isOAuthError(error)) { return error.code; } return undefined; } static formatError(error) { if (this.isOAuthError(error)) { return `[${error.code}] ${error.message}`; } if (error instanceof Error) { return error.message; } return String(error); } static logError(error, context) { const formattedError = this.formatError(error); const logMessage = context ? `${context}: ${formattedError}` : formattedError; console.error(logMessage); if (this.isOAuthError(error) && error.originalError) { console.error('Original error:', error.originalError); } } } exports.ErrorHandler = ErrorHandler; //# sourceMappingURL=ErrorHandler.js.map