Sha256: 22328d57ca60e29857ac2f08a4fd52def373a55ed68f52e18d2392c7c3356bd6
Contents?: true
Size: 1.17 KB
Versions: 1
Compression:
Stored size: 1.17 KB
Contents
module JsonApiClient module Middleware class Status < Faraday::Middleware def call(environment) @app.call(environment).on_complete do |env| handle_status(env[:status], env) # look for meta[:status] if env[:body].is_a?(Hash) code = env[:body].fetch("meta", {}).fetch("status", 200).to_i handle_status(code, env) end end rescue Faraday::ConnectionFailed, Faraday::TimeoutError => e raise Errors::ConnectionError.new environment, e.to_s end protected def handle_status(code, env) case code when 200..399 when 401 raise Errors::NotAuthorized, env when 403 raise Errors::AccessDenied, env when 404 raise Errors::NotFound, env[:url] when 409 raise Errors::Conflict, env when 422 # Allow to proceed as resource errors will be populated when 400..499 raise Errors::ClientError, env when 500..599 raise Errors::ServerError, env else raise Errors::UnexpectedStatus.new(code, env[:url]) end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
json_api_client-1.8.0 | lib/json_api_client/middleware/status.rb |