Commit 7b9f2754 authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Add exception details to GraphQL request logs

We rescue StandardError in the controller so we need to manually add the
exception to the ActiveSupport instrumentation payload so that it gets
logged
parent 01dd30cc
...@@ -50,6 +50,8 @@ class GraphqlController < ApplicationController ...@@ -50,6 +50,8 @@ class GraphqlController < ApplicationController
end end
rescue_from StandardError do |exception| rescue_from StandardError do |exception|
@exception_object = exception
log_exception(exception) log_exception(exception)
if Rails.env.test? || Rails.env.development? if Rails.env.test? || Rails.env.development?
...@@ -197,7 +199,9 @@ class GraphqlController < ApplicationController ...@@ -197,7 +199,9 @@ class GraphqlController < ApplicationController
# Merging to :metadata will ensure these are logged as top level keys # Merging to :metadata will ensure these are logged as top level keys
payload[:metadata] ||= {} payload[:metadata] ||= {}
payload[:metadata].merge!(graphql: logs) payload[:metadata][:graphql] = logs
payload[:exception_object] = @exception_object if @exception_object
end end
def logs def logs
......
...@@ -262,5 +262,16 @@ RSpec.describe GraphqlController do ...@@ -262,5 +262,16 @@ RSpec.describe GraphqlController do
expect(controller).to have_received(:append_info_to_payload) expect(controller).to have_received(:append_info_to_payload)
expect(log_payload.dig(:metadata, :graphql)).to match_array(expected_logs) expect(log_payload.dig(:metadata, :graphql)).to match_array(expected_logs)
end end
it 'appends the exception in case of errors' do
exception = StandardError.new('boom')
expect(controller).to receive(:execute).and_raise(exception)
post :execute, params: { _json: graphql_queries }
expect(controller).to have_received(:append_info_to_payload)
expect(log_payload.dig(:exception_object)).to eq(exception)
end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment