Sha256: 7acdccd3c49cc4cf8d7d45d97ac6210cc86e71d0fa7a0e155acb54e32b23daa1

Contents?: true

Size: 1 KB

Versions: 1

Compression:

Stored size: 1 KB

Contents

module Washbullet
  class BadRequest    < StandardError; end
  class Unauthorized  < StandardError; end
  class RequestFailed < StandardError; end
  class Forbidden     < StandardError; end
  class NotFound      < StandardError; end
  class ServerError   < StandardError; end

  class HttpException < Faraday::Response::Middleware
    def call(env)
      @app.call(env).on_complete do |response|
        exception =
          case response.status
          when 400      then Washbullet::BadRequest
          when 401      then Washbullet::Unauthorized
          when 402      then Washbullet::RequestFailed
          when 403      then Washbullet::Forbidden
          when 404      then Washbullet::NotFound
          when 500..505 then Washbullet::ServerError
          end

        raise exception, error_message(response.body) if exception
      end
    end

    private

    def error_message(response_body)
      hash = JSON.parse(response_body)['error']

      [hash['message'], hash['cat']].join(' ')
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
washbullet-0.4.0 lib/washbullet/http_exception.rb