Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise useful Horizon errors #95

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sdk/lib/stellar-sdk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Stellar
autoload :Amount
autoload :Client
autoload :SEP10
autoload :HorizonError

module Horizon
extend ActiveSupport::Autoload
Expand Down
3 changes: 3 additions & 0 deletions sdk/lib/stellar/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ def submit_transaction(tx_envelope:, options: {skip_memo_required_check: false})
check_memo_required(tx_envelope)
end
@horizon.transactions._post(tx: tx_envelope.to_xdr(:base64))

rescue Faraday::BadRequestError => ex
raise HorizonError.make(ex.response[:body])
end

# Required by SEP-0029
Expand Down
53 changes: 53 additions & 0 deletions sdk/lib/stellar/horizon_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
module Stellar
class HorizonError < StandardError
attr_reader :original_response, :extras

def self.make(original_response)
klass = class_for(original_response["title"])
klass.new(original_response)
end

# Uses specialized class if it is defined
def self.class_for(title)
const_get title.delete(" ").to_sym
rescue NameError
self
end

def initialize(original_response)
message = make_message(
detail: original_response["detail"],
title: original_response["title"]
)

# Superclass StandardError has different argument signature
super(message)

@original_response = original_response
@extras = original_response["extras"]
end

private

def make_message(detail:, title:)
# Include title only on generic class
# Condition passes only for child classes
if self.class != HorizonError
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noticed .instance_of? existed

instance_of?(HorizonError)

title = nil
end

[title, detail].compact.join(": ")
end

class TransactionFailed < self
def make_message(detail:)
detail.sub!(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't such substitution be useful on the generic level? In other words, can we show extras in the message for every error? If they exist, of course

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I know, extras.result_codes is only provided for TransactionFailed's. That said, I only checked 2 or 3 types.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A cursory search seems to confirm.

"The `extras.result_codes` field on this response contains further details.",
"extras.result_codes contained: `#{hash.dig("extras", "result_codes")}`"
)
end
end

class TransactionMalformed < self; end
end
end