Skip to content

Commit

Permalink
Better: Add fbtrace_id, x-fb-rev, x-fb-debug to error messages and er…
Browse files Browse the repository at this point in the history
…ror class (#686)
  • Loading branch information
pri1012 committed May 1, 2024
1 parent 12d0dc5 commit 64d17a8
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ New features:

Updated features:

* Add fbtrace_id, x-fb-rev, x-fb-debug to error messages and error class ([#668](https://github.com/arsduo/koala/pull/686))

Removed features:

Internal improvements:
Expand Down
6 changes: 4 additions & 2 deletions lib/koala/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class APIError < ::Koala::KoalaError
:fb_error_user_msg,
:fb_error_user_title,
:fb_error_trace_id,
:fb_error_debug_trace_id,
:fb_error_debug,
:fb_error_rev,
:fb_buc_usage,
Expand Down Expand Up @@ -65,16 +66,17 @@ def initialize(http_status, response_body, error_info = nil)
self.fb_error_message = error_info["message"]
self.fb_error_user_msg = error_info["error_user_msg"]
self.fb_error_user_title = error_info["error_user_title"]
self.fb_error_trace_id = error_info["fbtrace_id"]

self.fb_error_trace_id = error_info["x-fb-trace-id"]
self.fb_error_debug_trace_id = error_info["x-fb-trace-id"]
self.fb_error_debug = error_info["x-fb-debug"]
self.fb_error_rev = error_info["x-fb-rev"]
self.fb_buc_usage = json_parse_for(error_info, "x-business-use-case-usage")
self.fb_ada_usage = json_parse_for(error_info, "x-ad-account-usage")
self.fb_app_usage = json_parse_for(error_info, "x-app-usage")

error_array = []
%w(type code error_subcode message error_user_title error_user_msg x-fb-trace-id).each do |key|
%w(type code error_subcode message error_user_title error_user_msg fbtrace_id x-fb-trace-id x-fb-debug x-fb-rev).each do |key|
error_array << "#{key}: #{error_info[key]}" if error_info[key]
end

Expand Down
6 changes: 4 additions & 2 deletions spec/cases/error_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
'error_subcode' => 'subcode',
'error_user_msg' => 'error user message',
'error_user_title' => 'error user title',
'x-fb-trace-id' => 'fb trace id',
'fbtrace_id' => 'fb trace id',
'x-fb-trace-id' => 'x-fb trace id',
'x-fb-debug' => 'fb debug token',
'x-fb-rev' => 'fb revision',
'x-business-use-case-usage' => BUC_USAGE_JSON,
Expand All @@ -52,6 +53,7 @@
:fb_error_user_msg => 'error user message',
:fb_error_user_title => 'error user title',
:fb_error_trace_id => 'fb trace id',
:fb_error_debug_trace_id => 'x-fb trace id',
:fb_error_debug => 'fb debug token',
:fb_error_rev => 'fb revision',
:fb_buc_usage => JSON.parse(BUC_USAGE_JSON),
Expand All @@ -64,7 +66,7 @@
end

it "sets the error message appropriately" do
expect(error.message).to eq("type: type, code: 1, error_subcode: subcode, message: message, error_user_title: error user title, error_user_msg: error user message, x-fb-trace-id: fb trace id [HTTP 400]")
expect(error.message).to eq("type: type, code: 1, error_subcode: subcode, message: message, error_user_title: error user title, error_user_msg: error user message, fbtrace_id: fb trace id, x-fb-trace-id: x-fb trace id, x-fb-debug: fb debug token, x-fb-rev: fb revision [HTTP 400]")
end
end

Expand Down
6 changes: 4 additions & 2 deletions spec/cases/graph_error_checker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,12 @@ module Facebook
"error_subcode" => "FB error subcode",
"message" => "An error occurred!",
"error_user_msg" => "A user msg",
"error_user_title" => "usr title"
"error_user_title" => "usr title",
"fbtrace_id" => "fbtrace_id"
}
body.replace({"error" => error_data}.to_json)

expect(error.fb_error_trace_id).to eq(error_data["fbtrace_id"])
expect(error.fb_error_type).to eq(error_data["type"])
expect(error.fb_error_code).to eq(error_data["code"])
expect(error.fb_error_subcode).to eq(error_data["error_subcode"])
Expand All @@ -97,7 +99,7 @@ module Facebook
"x-ad-account-usage" => { 'c' => 3, 'd' => 4 }.to_json,
"x-app-usage" => { 'e' => 5, 'f' => 6 }.to_json
)
expect(error.fb_error_trace_id).to eq(headers["x-fb-trace-id"])
expect(error.fb_error_debug_trace_id).to eq(headers["x-fb-trace-id"])
expect(error.fb_error_debug).to eq(headers["x-fb-debug"])
expect(error.fb_error_rev).to eq(headers["x-fb-rev"])
expect(error.fb_buc_usage).to eq({ 'a' => 1, 'b' => 2 })
Expand Down

0 comments on commit 64d17a8

Please sign in to comment.