Exception: Toolhound::Error
- Inherits:
-
StandardError
- Object
- StandardError
- Toolhound::Error
- Defined in:
- lib/toolhound-ruby/error.rb
Overview
Custom error class for rescuing from all GitHub errors
Direct Known Subclasses
Class Method Summary collapse
-
.from_response(response) ⇒ Nearmiss::Error
Returns the appropriate Nearmiss::Error sublcass based on status and response message.
Instance Method Summary collapse
- #build_error_message ⇒ Object
-
#data ⇒ Object
private.
-
#errors ⇒ Array<Hash>
Array of validation errors.
-
#initialize(response = nil) ⇒ Error
constructor
A new instance of Error.
- #redact_url(url_string) ⇒ Object
- #response_error ⇒ Object
- #response_error_summary ⇒ Object
- #response_message ⇒ Object
Constructor Details
#initialize(response = nil) ⇒ Error
Returns a new instance of Error.
38 39 40 41 |
# File 'lib/toolhound-ruby/error.rb', line 38 def initialize(response=nil) @response = response super() end |
Class Method Details
.from_response(response) ⇒ Nearmiss::Error
Returns the appropriate Nearmiss::Error sublcass based on status and response message
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/toolhound-ruby/error.rb', line 10 def self.from_response(response) status = response[:status].to_i body = response[:body].to_s headers = response[:response_headers] if klass = case status when 400 then Nearmiss::BadRequest when 401 then Nearmiss::Unauthorized when 403 then Nearmiss::Forbidden when 404 then Nearmiss::NotFound when 405 then Nearmiss::MethodNotAllowed when 406 then Nearmiss::NotAcceptable when 409 then Nearmiss::Conflict when 415 then Nearmiss::UnsupportedMediaType when 422 then Nearmiss::UnprocessableEntity when 400..499 then Nearmiss::ClientError when 500 then Nearmiss::InternalServerError when 501 then Nearmiss::NotImplemented when 502 then Nearmiss::BadGateway when 503 then Nearmiss::ServiceUnavailable when 500..599 then Nearmiss::ServerError end klass.new(response) end end |
Instance Method Details
#build_error_message ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/toolhound-ruby/error.rb', line 95 def return nil if @response.nil? = "#{@response[:method].to_s.upcase} " << redact_url(@response[:url].to_s) + ": " << "#{@response[:status]} - " << "#{}" unless .nil? << "#{response_error}" unless response_error.nil? << "#{response_error_summary}" unless response_error_summary.nil? # message << MultiJson.dump(@response) end |
#data ⇒ Object
private
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/toolhound-ruby/error.rb', line 55 def data @data ||= if (body = @response[:body]) && !body.empty? if body.is_a?(String) && @response[:response_headers] && @response[:response_headers][:content_type] =~ /json/ # Sawyer::Agent.serializer.decode(body) else body end else nil end end |
#errors ⇒ Array<Hash>
Array of validation errors
45 46 47 48 49 50 51 |
# File 'lib/toolhound-ruby/error.rb', line 45 def errors if data && data.is_a?(Hash) data[:errors] || [] else [] end end |
#redact_url(url_string) ⇒ Object
110 111 112 113 114 115 |
# File 'lib/toolhound-ruby/error.rb', line 110 def redact_url(url_string) %w[client_secret access_token].each do |token| url_string.gsub!(/#{token}=\S+/, "#{token}=(redacted)") if url_string.include? token end url_string end |
#response_error ⇒ Object
80 81 82 |
# File 'lib/toolhound-ruby/error.rb', line 80 def response_error "Error: #{data[:error]}" if data.is_a?(Hash) && data[:error] end |
#response_error_summary ⇒ Object
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/toolhound-ruby/error.rb', line 84 def response_error_summary return nil unless data.is_a?(Hash) && !Array(data[:errors]).empty? summary = "\nError summary:\n" # summary << data[:errors].map do |hash| # hash.map { |k,v| " #{k}: #{v}" } # end.join("\n") summary << data[:errors].join("\n") summary end |
#response_message ⇒ Object
71 72 73 74 75 76 77 78 |
# File 'lib/toolhound-ruby/error.rb', line 71 def case data when Hash data[:message] when String data end end |