Skip to content

Commit

Permalink
refactor handling exception
Browse files Browse the repository at this point in the history
  • Loading branch information
KapustaB committed Jan 25, 2024
1 parent 21b1792 commit 3c67576
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/treblle/interfaces/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def header_to_include?(header)

def parse_request_body
JSON.parse(metadata.raw_post)
rescue JSON::ParserError
rescue JSON::ParserError, NoMethodError
{}
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/treblle/interfaces/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Treblle
module Interfaces
class Response
def initialize(response)
@status, @headers, @response = response
@status, @headers, @response = response || [500, {}, {}]
@body = parse_response_body
@size = response_size
end
Expand All @@ -17,7 +17,7 @@ def response_size

def parse_response_body
JSON.parse(response.body)
rescue JSON::ParserError
rescue JSON::ParserError, NoMethodError
{}
end
end
Expand Down
9 changes: 5 additions & 4 deletions lib/treblle/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,17 @@ def call_with_treblle_monitoring(env)
begin
response = @app.call(env)
rescue StandardError => e
Rails.logger.error(e.message)
handle_monitoring(env, response, started_at, e)
raise e
end

handle_monitoring(env, response, started_at)
handle_monitoring(env, response, started_at) unless env['rack.exception']

response
end

def handle_monitoring(env, response, started_at)
payload = PayloadBuilder.new(env:, response:, started_at:).call
def handle_monitoring(env, response, started_at, exception: nil)
payload = PayloadBuilder.new(env:, response:, started_at:, exception:).call
Dispatcher.new(payload:).call
rescue StandardError => e
Rails.logger.error(e.message)
Expand Down

0 comments on commit 3c67576

Please sign in to comment.