lib/sapience/extensions/grape/middleware/logging.rb in sapience-2.0.2 vs lib/sapience/extensions/grape/middleware/logging.rb in sapience-2.0.3

- old
+ new

@@ -16,36 +16,62 @@ def initialize(app, options = {}) super @logger = @options[:logger] end + protected + + def call!(env) + @env = env + before + error = catch(:error) do + begin + @app_response = @app.call(@env) + rescue => e + after_exception(e) + raise e + end + nil + end + if error + after_failure(error) + throw(:error, error) + else + @status, _, _ = *@app_response + after + end + @app_response + end + def before reset_db_runtime start_time end def after stop_time @logger.info(parameters) - nil end - protected + def after_exception(e) + Sapience.push_tags(e.class.name, e.message) + @status = 500 + after + end - # TODO: Is NoMethodError :[] equal to `404`? - def response - super - rescue - nil + def after_failure(error) + Sapience.push_tags(error[:message]) + @status = error[:status] + after end def parameters # rubocop:disable AbcSize { method: request.request_method, request_path: request.path, format: request_format(request.env), - status: response.try(:status) || 404, + status: @status, class_name: env["api.endpoint"].options[:for].to_s, action: "index", host: request.host, ip: (request.env["HTTP_X_FORWARDED_FOR"] || request.env["REMOTE_ADDR"]), ua: request.env["HTTP_USER_AGENT"], @@ -88,9 +114,10 @@ end def stop_time @stop_time ||= Time.now end + end end end end end