lib/ably/exceptions.rb in ably-0.8.15 vs lib/ably/exceptions.rb in ably-1.0.0

- old
+ new

@@ -1,7 +1,10 @@ module Ably module Exceptions + TOKEN_EXPIRED_CODE = 40140..40149 + INVALID_CLIENT_ID = 40012 + # Base Ably exception class that contains status and code values used by Ably # Refer to https://github.com/ably/ably-common/blob/master/protocol/errors.json # # @!attribute [r] message # @return [String] Error message from Ably @@ -9,26 +12,42 @@ # @return [String] HTTP status code of error # @!attribute [r] code # @return [String] Ably specific error code class BaseAblyException < StandardError attr_reader :status, :code - def initialize(message, status = nil, code = nil) + + def initialize(message, status = nil, code = nil, base_exception = nil, options = {}) super message + + @base_exception = base_exception @status = status + @status ||= base_exception.status if base_exception && base_exception.respond_to?(:status) + @status ||= options[:fallback_status] @code = code + @code ||= base_exception.code if base_exception && base_exception.respond_to?(:code) + @code ||= options[:fallback_code] end def to_s message = [super] if status || code additional_info = [] additional_info << "code: #{code}" if code additional_info << "http status: #{status}" if status + additional_info << "base exception: #{@base_exception.class}" if @base_exception message << "(#{additional_info.join(', ')})" end message.join(' ') end + + def as_json + { + message: "#{self.class}: #{message}", + status: @status, + code: @code + }.delete_if { |key, val| val.nil? } + end end # An invalid request was received by Ably class InvalidRequest < BaseAblyException; end @@ -50,20 +69,19 @@ # Encoding or decoding failure class EncoderError < BaseAblyException; end # Connection error from Realtime or REST service class ConnectionError < BaseAblyException - def initialize(message, status = nil, code = nil, base_error = nil) - super message, status, code - @base_error = base_error + def initialize(message, status = nil, code = nil, base_exception = nil, options = {}) + super message, status, code, base_exception, options end def to_s message = [super] - if @base_error - message << "#{@base_error}" - if @base_error.respond_to?(:message) && @base_error.message.match(/certificate verify failed/i) + if @base_exception + message << "#{@base_exception}" + if @base_exception.respond_to?(:message) && @base_exception.message.match(/certificate verify failed/i) message << "See https://goo.gl/eKvfcR to resolve this issue." end end message.join(' < ') end @@ -82,13 +100,17 @@ class ConnectionSuspended < ConnectionError; end # Connection failed class ConnectionFailed < ConnectionError; end + class AuthenticationFailed < ConnectionError; end + # Invalid State Change error on a {https://github.com/gocardless/statesman Statesman State Machine} class InvalidStateChange < BaseAblyException; end + class InvalidState < BaseAblyException; end + # A generic Ably exception taht supports a status & code. # See https://github.com/ably/ably-common/blob/master/protocol/errors.json for a list of Ably errors class Standard < BaseAblyException; end # The HTTP request has returned a 500 error @@ -119,8 +141,15 @@ class UnsupportedDataType < BaseAblyException; end # When a channel is detached / failed, certain operations are not permitted such as publishing messages class ChannelInactive < BaseAblyException; end - class IncompatibleClientId < BaseAblyException; end + class IncompatibleClientId < BaseAblyException + def initialize(messages, status = 400, code = INVALID_CLIENT_ID, *args) + super(message, status, code, *args) + end + end + + # Token request has missing or invalid attributes + class InvalidTokenRequest < BaseAblyException; end end end