lib/tickethub/exceptions.rb in tickethub-0.3.51 vs lib/tickethub/exceptions.rb in tickethub-0.3.52

- old
+ new

@@ -1,46 +1,67 @@ module Tickethub - class ConnectionError < StandardError # :nodoc: + class Error < StandardError + attr_reader :request + + def initialize(request = nil, message = nil) + super(message) + @request = request + end + end + + ConnectionError = Error + RequestError = Error + + class ResponseError < Error attr_reader :response - def initialize(response, message = nil) + def initialize(request, response, message = nil) + super(request, message) @response = response - @message = message end def to_s message = "Failed." message << " Response code = #{response.code}." if response.respond_to?(:code) message << " Response message = #{response.message}." if response.respond_to?(:message) - message << " Response Body = #{response.body}." if response.respond_to?(:body) + + if response.respond_to?(:body) + # Error messages need to be in UTF-8 + body = response.body.dup.to_s + body = body.encode('UTF-8', :invalid => :replace, :undef => :replace, :replace => '?') + message << " Response Body = #{body}." + end + message end end # Raised when a Timeout::Error occurs. - class TimeoutError < ConnectionError - def initialize(message) - @message = message - end - def to_s; @message ;end + class TimeoutError < RequestError end # Raised when a OpenSSL::SSL::SSLError occurs. - class SSLError < ConnectionError - def initialize(message) - @message = message - end - def to_s; @message ;end + class SSLError < RequestError end + class ErrnoError < RequestError + end + + class ZlibError < RequestError + end + # 3xx Redirection - class Redirection < ConnectionError # :nodoc: + class Redirection < ResponseError # :nodoc: def to_s; response['Location'] ? "#{super} => #{response['Location']}" : super; end end + class RedirectionLoop < ResponseError # :nodoc: + def to_s; response['Location'] ? "#{super} => #{response['Location']}" : super; end + end + # 4xx Client Error - class ClientError < ConnectionError; end # :nodoc: + class ClientError < ResponseError; end # :nodoc: # 400 Bad Request class BadRequest < ClientError; end # :nodoc # 401 Unauthorized @@ -50,14 +71,20 @@ class ForbiddenAccess < ClientError; end # :nodoc # 404 Not Found class ResourceNotFound < ClientError; end # :nodoc: + # 409 Conflict + class ResourceConflict < ClientError; end # :nodoc: + + # 410 Gone + class ResourceGone < ClientError; end # :nodoc: + # 422 Invalid class ResourceInvalid < ClientError; end # :nodoc: # 5xx Server Error - class ServerError < ConnectionError; end # :nodoc: + class ServerError < ResponseError; end # :nodoc: # 405 Method Not Allowed class MethodNotAllowed < ClientError # :nodoc: def allowed_methods @response['Allow'].split(',').map { |verb| verb.strip.downcase.to_sym } \ No newline at end of file