Class: Aspera::Rest::ErrorAnalyzer
- Inherits:
-
Object
- Object
- Aspera::Rest::ErrorAnalyzer
- Includes:
- Singleton
- Defined in:
- lib/aspera/rest/error_analyzer.rb
Overview
analyze error codes returned by REST calls and raise ruby exception
Instance Attribute Summary collapse
-
#log_file ⇒ Object
Returns the value of attribute log_file.
Class Method Summary collapse
-
.add_error(context, type, message) ⇒ Object
Used by handler to add an error description to list of errors For logging and tracing : collect error descriptions (create file to activate).
-
.instance ⇒ ErrorAnalyzer
Returns the singleton instance of ErrorAnalyzer.
Instance Method Summary collapse
-
#add_handler(name, before: nil, &block) ⇒ Object
Add a new error handler (done at application initialization) name is the one provided here context is built in method raise_on_error.
-
#add_simple_handler(name:, always: false, path:) ⇒ Object
Add a simple error handler Check that key exists and is string under specified path (hash) Adds other keys as secondary information.
-
#initialize ⇒ ErrorAnalyzer
constructor
the singleton object is registered with application specific handlers.
-
#raise_on_error(req, data, http) ⇒ Object
Use this method to analyze a EST result and raise an exception Analyzes REST call response and raises a CallError exception if HTTP result code is not 2XX.
Constructor Details
#initialize ⇒ ErrorAnalyzer
the singleton object is registered with application specific handlers
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/aspera/rest/error_analyzer.rb', line 28 def initialize # list of handlers @error_handlers = [] @log_file = nil add_handler('Type Generic') do |type, context| if !context[:response].code.start_with?('2') # add generic information ErrorAnalyzer.add_error(context, type, "#{context[:request]['host']} #{context[:response].code} #{context[:response].}") end end end |
Instance Attribute Details
#log_file ⇒ Object
Returns the value of attribute log_file.
25 26 27 |
# File 'lib/aspera/rest/error_analyzer.rb', line 25 def log_file @log_file end |
Class Method Details
.add_error(context, type, message) ⇒ Object
Used by handler to add an error description to list of errors For logging and tracing : collect error descriptions (create file to activate)
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/aspera/rest/error_analyzer.rb', line 110 def add_error(context, type, ) # A server in Ruby (e.g. Orchestrator) can return the backtrace of the exception: in debug log only if .is_a?(String) && .match?(BACKTRACE_LINE) Log.log.debug { "Error with backtrace: #{}" } = .lines.grep_v(BACKTRACE_LINE).join.strip end context[:messages].push() Log.log.trace1 { "Found error: #{type}: #{}" } log_file = instance.log_file # log error for further analysis (file must exist to activate) return if log_file.nil? || !File.exist?(log_file) File.open(log_file, 'a+') do |f| f.write("\n=#{type}=====\n#{context[:request].method} #{context[:request].path}\n#{context[:response].code}\n" \ "#{JSON.generate(context[:data])}\n#{context[:messages].join("\n")}") end end |
.instance ⇒ ErrorAnalyzer
Returns the singleton instance of ErrorAnalyzer
18 19 20 21 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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/aspera/rest/error_analyzer.rb', line 18 class ErrorAnalyzer include Singleton # Line of the backtrace of a Ruby exception (Exception#full_message), e.g. `\tfrom /app/x.rb:12:in 'y'` BACKTRACE_LINE = /^\s+from \S.*:\d+:in / private_constant :BACKTRACE_LINE attr_accessor :log_file # the singleton object is registered with application specific handlers def initialize # list of handlers @error_handlers = [] @log_file = nil add_handler('Type Generic') do |type, context| if !context[:response].code.start_with?('2') # add generic information ErrorAnalyzer.add_error(context, type, "#{context[:request]['host']} #{context[:response].code} #{context[:response].}") end end end # Use this method to analyze a EST result and raise an exception # Analyzes REST call response and raises a CallError exception # if HTTP result code is not 2XX # @param req [Net::HTTPRequest] the HTTP request # @param data [Object] the parsed response body # @param http [Net::HTTPResponse] the raw HTTP response def raise_on_error(req, data, http) Log.log.debug { "raise_on_error #{req.method} #{req.path} #{http.code}" } context = { messages: [], request: req, response: http, data: data } # multiple error messages can be found # analyze errors from provided handlers # note that there can be an error even if code is 2XX @error_handlers.each do |handler| handler[:block].call(handler[:name], context) rescue StandardError => e Log.log.error { "ERROR in handler:\n#{e.}\n#{e.backtrace}" } end raise CallError, context unless context[:messages].empty? end # Add a new error handler (done at application initialization) # @param name [String] name of error handler (for logs) # @param block [Proc] processing of response: takes two parameters: `name`, `context` # name is the one provided here # context is built in method raise_on_error # @param before [String, nil] Name of a handler: this one is placed just before it (so that the messages come in a given order). `nil`: placed first. # @raise [ArgumentError] if `before` is not the name of a handler def add_handler(name, before: nil, &block) index = before.nil? ? 0 : @error_handlers.index { |handler| handler[:name].eql?(before) } raise ArgumentError, "no error handler named #{before}" if index.nil? @error_handlers.insert(index, {name: name, block: block}) nil end # Add a simple error handler # Check that key exists and is string under specified path (hash) # Adds other keys as secondary information # @param name [String] name of error handler (for logs) # @param always [Boolean] if true, always add error message, even if response code is 2XX # @param path [Array] path to error message in response def add_simple_handler(name:, always: false, path:) path.freeze add_handler(name) do |type, context| if context[:data].is_a?(Hash) && (!context[:response].code.start_with?('2') || always) # Log.log.debug{"simple_handler: #{type} #{path} #{path.last}"} # dig and find hash containing error message error_struct = path.length.eql?(1) ? context[:data] : context[:data].dig(*path[0..-2]) # Log.log.debug{"found: #{error_struct.class} #{error_struct}"} if error_struct.is_a?(Hash) && error_struct[path.last].is_a?(String) ErrorAnalyzer.add_error(context, type, error_struct[path.last]) error_struct.each do |k, v| next if k.eql?(path.last) ErrorAnalyzer.add_error(context, "#{type}(sub)", "#{k}: #{v}") if [String, Integer].include?(v.class) end end end end end class << self # Used by handler to add an error description to list of errors # For logging and tracing : collect error descriptions (create file to activate) # @param context [Hash] the result context, provided to handler # @param type [String] type of exception, for logging purpose # @param message [String] one error message to add to list def add_error(context, type, ) # A server in Ruby (e.g. Orchestrator) can return the backtrace of the exception: in debug log only if .is_a?(String) && .match?(BACKTRACE_LINE) Log.log.debug { "Error with backtrace: #{}" } = .lines.grep_v(BACKTRACE_LINE).join.strip end context[:messages].push() Log.log.trace1 { "Found error: #{type}: #{}" } log_file = instance.log_file # log error for further analysis (file must exist to activate) return if log_file.nil? || !File.exist?(log_file) File.open(log_file, 'a+') do |f| f.write("\n=#{type}=====\n#{context[:request].method} #{context[:request].path}\n#{context[:response].code}\n" \ "#{JSON.generate(context[:data])}\n#{context[:messages].join("\n")}") end end end end |
Instance Method Details
#add_handler(name, before: nil, &block) ⇒ Object
Add a new error handler (done at application initialization) name is the one provided here context is built in method raise_on_error
72 73 74 75 76 77 |
# File 'lib/aspera/rest/error_analyzer.rb', line 72 def add_handler(name, before: nil, &block) index = before.nil? ? 0 : @error_handlers.index { |handler| handler[:name].eql?(before) } raise ArgumentError, "no error handler named #{before}" if index.nil? @error_handlers.insert(index, {name: name, block: block}) nil end |
#add_simple_handler(name:, always: false, path:) ⇒ Object
Add a simple error handler Check that key exists and is string under specified path (hash) Adds other keys as secondary information
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/aspera/rest/error_analyzer.rb', line 85 def add_simple_handler(name:, always: false, path:) path.freeze add_handler(name) do |type, context| if context[:data].is_a?(Hash) && (!context[:response].code.start_with?('2') || always) # Log.log.debug{"simple_handler: #{type} #{path} #{path.last}"} # dig and find hash containing error message error_struct = path.length.eql?(1) ? context[:data] : context[:data].dig(*path[0..-2]) # Log.log.debug{"found: #{error_struct.class} #{error_struct}"} if error_struct.is_a?(Hash) && error_struct[path.last].is_a?(String) ErrorAnalyzer.add_error(context, type, error_struct[path.last]) error_struct.each do |k, v| next if k.eql?(path.last) ErrorAnalyzer.add_error(context, "#{type}(sub)", "#{k}: #{v}") if [String, Integer].include?(v.class) end end end end end |
#raise_on_error(req, data, http) ⇒ Object
Use this method to analyze a EST result and raise an exception Analyzes REST call response and raises a CallError exception if HTTP result code is not 2XX
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/aspera/rest/error_analyzer.rb', line 46 def raise_on_error(req, data, http) Log.log.debug { "raise_on_error #{req.method} #{req.path} #{http.code}" } context = { messages: [], request: req, response: http, data: data } # multiple error messages can be found # analyze errors from provided handlers # note that there can be an error even if code is 2XX @error_handlers.each do |handler| handler[:block].call(handler[:name], context) rescue StandardError => e Log.log.error { "ERROR in handler:\n#{e.}\n#{e.backtrace}" } end raise CallError, context unless context[:messages].empty? end |