Module: JwtAuthCognito::ErrorUtils
- Defined in:
- lib/jwt_auth_cognito/error_utils.rb
Constant Summary collapse
- JWT_ERROR_MESSAGES =
{ 'TOKEN_EXPIRED' => 'Token has expired', 'INVALID_TOKEN' => 'Invalid token format', 'TOKEN_NOT_ACTIVE' => 'Token not active yet', 'INVALID_SIGNATURE' => 'Invalid token signature', 'SIGNATURE_VERIFICATION_FAILED' => 'Token signature verification failed', 'INVALID_AUDIENCE' => 'Invalid token audience', 'INVALID_ISSUER' => 'Invalid token issuer', 'VALIDATION_TIMEOUT' => 'Token validation timeout', 'TOKEN_REVOKED' => 'Token has been revoked', 'API_KEY_INVALID' => 'Invalid API key', 'API_KEY_EXPIRED' => 'API key has expired', 'INITIALIZATION_FAILED' => 'Service initialization failed', 'REDIS_CONNECTION_FAILED' => 'Redis connection failed', 'USER_DATA_RETRIEVAL_FAILED' => 'User data retrieval failed' }.freeze
Class Method Summary collapse
- .extract_error_details(error, context = nil) ⇒ Object
- .format_validation_error(error, context = nil) ⇒ Object
- .get_user_friendly_error_message(error) ⇒ Object
- .log_error(error, context = nil) ⇒ Object
Class Method Details
.extract_error_details(error, context = nil) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/jwt_auth_cognito/error_utils.rb', line 22 def self.extract_error_details(error, context = nil) = 'Unknown error occurred' code = nil case error when JwtAuthCognito::TokenExpiredError = 'Token has expired' code = 'TOKEN_EXPIRED' when JwtAuthCognito::TokenNotActiveError = 'Token not active yet' code = 'TOKEN_NOT_ACTIVE' when JwtAuthCognito::TokenFormatError = 'Invalid token format' code = 'INVALID_TOKEN' when JwtAuthCognito::TokenRevokedError = 'Token has been revoked' code = 'TOKEN_REVOKED' when JwtAuthCognito::JWKSError = 'Invalid token signature' code = 'INVALID_SIGNATURE' when JwtAuthCognito::RedisConnectionError = 'Redis connection failed' code = 'REDIS_CONNECTION_FAILED' when StandardError = error. # Check message content for specific error patterns case when /expired/i = 'Token has expired' code = 'TOKEN_EXPIRED' when /invalid.*signature/i, /signature.*verification.*failed/i = 'Invalid token signature' code = 'INVALID_SIGNATURE' when /invalid.*audience/i, /aud/ = 'Invalid token audience' code = 'INVALID_AUDIENCE' when /invalid.*issuer/i, /iss/ = 'Invalid token issuer' code = 'INVALID_ISSUER' when /not.*active/i, /nbf/ = 'Token not active yet' code = 'TOKEN_NOT_ACTIVE' when /invalid.*format/i, /malformed/i = 'Invalid token format' code = 'INVALID_TOKEN' end when String = error else = error.to_s end { message: , code: code, context: context }.compact end |
.format_validation_error(error, context = nil) ⇒ Object
100 101 102 103 104 105 106 107 108 |
# File 'lib/jwt_auth_cognito/error_utils.rb', line 100 def self.format_validation_error(error, context = nil) details = extract_error_details(error, context) { valid: false, error: details[:message], error_code: details[:code] }.compact end |
.get_user_friendly_error_message(error) ⇒ Object
95 96 97 98 |
# File 'lib/jwt_auth_cognito/error_utils.rb', line 95 def self.(error) details = extract_error_details(error) details[:message] end |
.log_error(error, context = nil) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/jwt_auth_cognito/error_utils.rb', line 82 def self.log_error(error, context = nil) details = extract_error_details(error, context) = if details[:context] "#{details[:context]}: #{details[:message]}" else details[:message] end += " (#{details[:code]})" if details[:code] puts "ERROR: #{log_message}" end |