Class: Ably::Rest::Middleware::Exceptions

Inherits:
Faraday::Response::Middleware
  • Object
show all
Defined in:
lib/submodules/ably-ruby/lib/ably/rest/middleware/exceptions.rb

Overview

HTTP exceptions raised by Ably due to an error status code Ably returns JSON/Msgpack error codes and messages so include this if possible in the exception messages

Instance Method Summary collapse

Instance Method Details

#on_complete(env) ⇒ Object



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
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/submodules/ably-ruby/lib/ably/rest/middleware/exceptions.rb', line 10

def on_complete(env)
  if env.status >= 400
    error_status_code = env.status
    error_code = nil

    if env.body.kind_of?(Hash)
      error = env.body.fetch('error', {})
      error_status_code = error['statusCode'].to_i if error['statusCode']
      error_code = error['code'].to_i if error['code']

      if error
        message = "#{error['message']} (status: #{error_status_code}, code: #{error_code})"
      else
        message = env.body
      end
    else
      message = env.body
    end

    message = 'Unknown server error' if message.to_s.strip == ''
    request_id = env.request.context[:request_id] if env.request.context
    exception_args = [message, error_status_code, error_code, nil, { request_id: request_id }]

    if env.status >= 500
      raise Ably::Exceptions::ServerError.new(*exception_args)
    elsif env.status == 401
      if Ably::Exceptions::TOKEN_EXPIRED_CODE.include?(error_code)
        raise Ably::Exceptions::TokenExpired.new(*exception_args)
      else
        raise Ably::Exceptions::UnauthorizedRequest.new(*exception_args)
      end
    elsif env.status == 403
      raise Ably::Exceptions::ForbiddenRequest.new(*exception_args)
    elsif env.status == 404
      raise Ably::Exceptions::ResourceMissing.new(*exception_args)
    else
      raise Ably::Exceptions::InvalidRequest.new(*exception_args)
    end
  end
end