Module: Strata::CLI::ErrorReporter
- Defined in:
- lib/strata/cli/error_reporter.rb
Constant Summary collapse
- LOG_RELATIVE_PATH =
".strata_cli/logs/cli.log"- CONNECTION_ERROR_CLASS_NAMES =
%w[ Faraday::ConnectionFailed Faraday::TimeoutError Errno::ECONNREFUSED SocketError Timeout::Error Net::OpenTimeout Net::ReadTimeout ].freeze
Class Method Summary collapse
- .connection_error?(error) ⇒ Boolean
- .log_error(error, context: nil) ⇒ Object
- .log_relative_path ⇒ Object
- .user_message_for(error) ⇒ Object
Class Method Details
.connection_error?(error) ⇒ Boolean
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/strata/cli/error_reporter.rb', line 27 def connection_error?(error) current_error = error while current_error return true if CONNECTION_ERROR_CLASS_NAMES.include?(current_error.class.name) = current_error..to_s.downcase if .include?("connection refused") || .include?("failed to open tcp connection") || .include?("timed out") return true end current_error = current_error.cause end false end |
.log_error(error, context: nil) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/strata/cli/error_reporter.rb', line 55 def log_error(error, context: nil) log_path = File.join(Dir.pwd, LOG_RELATIVE_PATH) FileUtils.mkdir_p(File.dirname(log_path)) File.open(log_path, "a") do |f| f.puts("[#{Time.now.utc.iso8601}] #{context || "Unhandled CLI error"}") f.puts("#{error.class}: #{error.}") f.puts(error.backtrace.join("\n")) if error.backtrace f.puts("") end rescue => e warn "Failed to write CLI log: #{e.}" if ENV["DEBUG"] end |
.log_relative_path ⇒ Object
23 24 25 |
# File 'lib/strata/cli/error_reporter.rb', line 23 def log_relative_path LOG_RELATIVE_PATH end |
.user_message_for(error) ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/strata/cli/error_reporter.rb', line 44 def (error) if connection_error?(error) return "Connection failed while reaching datasource. Please check network and datasource settings." end msg = error..to_s.strip return msg unless msg.empty? "Unexpected error: #{error.class}" end |