Class: RailsAi::Security::ErrorHandler
- Inherits:
-
Object
- Object
- RailsAi::Security::ErrorHandler
- Defined in:
- lib/rails_ai/security/error_handler.rb
Class Method Summary collapse
- .handle_security_error(error, context = {}) ⇒ Object
- .log_and_raise(error, context = {}) ⇒ Object
- .sanitize_error_message(error) ⇒ Object
Class Method Details
.handle_security_error(error, context = {}) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/rails_ai/security/error_handler.rb', line 6 def self.handle_security_error(error, context = {}) # Log security error AuditLogger.log_security_event(:security_error, { error_class: error.class.name, error_message: error., context: context, timestamp: Time.now }) # Return sanitized error message = (error) # In production, don't expose internal details if Rails.env.production? case error when ValidationError "Invalid input provided" when RateLimitError "Rate limit exceeded. Please try again later" when SecurityError "Security error occurred" else "An error occurred while processing your request" end else end end |
.log_and_raise(error, context = {}) ⇒ Object
56 57 58 59 |
# File 'lib/rails_ai/security/error_handler.rb', line 56 def self.log_and_raise(error, context = {}) handle_security_error(error, context) raise error end |
.sanitize_error_message(error) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/rails_ai/security/error_handler.rb', line 35 def self.(error) = error..to_s # Remove sensitive information = .gsub(/api[_-]?key/i, 'API_KEY') = .gsub(/password/i, 'PASSWORD') = .gsub(/secret/i, 'SECRET') = .gsub(/token/i, 'TOKEN') # Remove file paths = .gsub(%r{/[a-zA-Z0-9_/.-]+}, '[PATH]') # Remove IP addresses = .gsub(/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/, '[IP]') # Remove URLs = .gsub(%r{https?://[^\s]+}, '[URL]') end |