Class: Percy::Client::Connection::NiceErrorMiddleware

Inherits:
Faraday::Response::Middleware
  • Object
show all
Defined in:
lib/percy/client/connection.rb

Constant Summary collapse

CLIENT_ERROR_STATUS_RANGE =
400...600

Instance Method Summary collapse

Instance Method Details

#on_complete(env) ⇒ Object

Raises:

  • (error_class)


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
# File 'lib/percy/client/connection.rb', line 11

def on_complete(env)
  error_class = nil

  case env[:status]
  when 400
    error_class = Percy::Client::BadRequestError
  when 401
    error_class = Percy::Client::UnauthorizedError
  when 402
    error_class = Percy::Client::PaymentRequiredError
  when 403
    error_class = Percy::Client::ForbiddenError
  when 404
    error_class = Percy::Client::NotFoundError
  when 409
    error_class = Percy::Client::ConflictError
  when 500
    error_class = Percy::Client::InternalServerError
  when 502
    error_class = Percy::Client::BadGatewayError
  when 503
    error_class = Percy::Client::ServiceUnavailableError
  when 504
    error_class = Percy::Client::GatewayTimeoutError
  when 520..530
    error_class = Percy::Client::CloudflareError
  when CLIENT_ERROR_STATUS_RANGE # Catchall.
    error_class = Percy::Client::HttpError
  end

  return unless error_class

  raise error_class.new(
    env.status, env.method.upcase, env.url, env.body,
    "Got #{env.status} (#{env.method.upcase} #{env.url}):\n#{env.body}",
  )
end