lib/rorvswild.rb in rorvswild-0.0.8 vs lib/rorvswild.rb in rorvswild-0.0.9

- old
+ new

@@ -24,10 +24,18 @@ def self.measure_block(name, &block) default_client ? default_client.measure_block(name , &block) : block.call end + def self.catch_error(extra_details = nil, &block) + default_client.catch_error(extra_details, &block) if default_client + end + + def self.record_error(exception, extra_details = nil) + default_client.record_error(exception, extra_details) if default_client + end + class Client def self.default_config { api_url: "http://www.rorvswild.com/api", explain_sql_threshold: 500, @@ -102,19 +110,14 @@ end def after_exception(exception, controller) if !exception.is_a?(ActionController::RoutingError) file, line = exception.backtrace.first.split(":") - @error = { - line: line.to_i, - file: relative_path(file), - message: exception.message, - backtrace: exception.backtrace, - exception: exception.class.to_s, + @error = exception_to_hash(exception).merge( session: controller.session.to_hash, environment_variables: filter_sensitive_data(filter_environment_variables(controller.request.env)) - } + ) end raise exception end def around_delayed_job(job, &block) @@ -135,25 +138,31 @@ @job = {name: name} started_at = Time.now cpu_time_offset = cpu_time block.call rescue => exception - file, line = exception.backtrace.first.split(":") - job[:error] = { - line: line.to_i, - file: relative_path(file), - message: exception.message, - backtrace: exception.backtrace, - exception: exception.class.to_s, - } + job[:error] = exception_to_hash(exception) raise ensure job[:runtime] = (Time.now - started_at) * 1000 job[:cpu_runtime] = cpu_time - cpu_time_offset post_job end + def catch_error(extra_details = nil, &block) + begin + block.call + rescue => exception + record_error(exception, extra_details) + exception + end + end + + def record_error(exception, extra_details = nil) + post_error(exception_to_hash(exception, extra_details)) + end + def cpu_time time = Process.times time.utime + time.stime + time.cutime + time.cstime end @@ -212,10 +221,14 @@ post("/jobs", job: job.merge(queries: slowest_queries)) rescue => exception log_error(exception) end + def post_error(hash) + post("/errors", error: hash) + end + def extract_file_and_line_from_call_stack(stack) return unless location = stack.find { |str| str.include?(Rails.root.to_s) } file, line, method = location.split(":") method = cleanup_method_name(method) file.sub!(Rails.root.to_s, "") @@ -233,9 +246,21 @@ ((finish - start) * 1000) end def relative_path(path) path.sub(Rails.root.to_s, "") + end + + def exception_to_hash(exception, extra_details = nil) + file, line = exception.backtrace.first.split(":") + { + line: line.to_i, + file: relative_path(file), + message: exception.message, + backtrace: exception.backtrace, + exception: exception.class.to_s, + extra_details: extra_details, + } end def post(path, data) uri = URI(api_url + path) Net::HTTP.start(uri.host, uri.port) do |http|