Created
May 9, 2016 04:39
-
-
Save kazimzaidi/059ce37349e277c817141f3b11008206 to your computer and use it in GitHub Desktop.
Monkey patch airbrake to include response from Facebook API calls, when Facebook throws a 400 or 500 (RestClient::BadRequest)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# When FB throws a 400 (e.g. while calling Graph API), the real error is inside the response body. | |
# This initializer intends to report that to Airbrake in a way that this response doesn't get lost. | |
# params would include __rest_client key that contains JSON-parsed response body of the error | |
# Also, error message would include the actual error message from FB, instead of just a boring "400: Bad Request" | |
module Airbrake | |
class Notifier | |
def send_notice_with_extra_logging(*args) | |
exception = args[0] | |
params = args[1] | |
if exception.is_a?(RestClient::Exception) | |
params[:__rest_client] = JSON.parse(exception.response) rescue nil | |
end | |
send_notice_without_extra_logging(*args) | |
end | |
alias_method_chain :send_notice, :extra_logging | |
end | |
end | |
Airbrake.add_filter do |notice| | |
# Add custom key and value if specified error class | |
if err = notice[:errors].find { |error| error[:type] == 'RestClient::BadRequest' || errors[:type] == 'RestClient::InternalServerError' } | |
rc = notice[:params][:__rest_client] | |
if rc | |
rc_message = rc.try(:[], 'error').try(:[], 'message') | |
if rc_message | |
err[:message] += ": #{rc_message}" | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment