-
Notifications
You must be signed in to change notification settings - Fork 44
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
title = nil | ||
end | ||
|
||
[title, detail].compact.join(": ") | ||
end | ||
|
||
class TransactionFailed < self | ||
def make_message(detail:) | ||
detail.sub!( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As far as I know, There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just noticed
.instance_of?
existedinstance_of?(HorizonError)