Class: Librato::Metrics::Middleware::ExpectsStatus

Inherits:
Faraday::Response::Middleware
  • Object
show all
Defined in:
lib/librato/metrics/middleware/expects_status.rb

Instance Method Summary collapse

Instance Method Details

#on_complete(env) ⇒ Object


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/librato/metrics/middleware/expects_status.rb', line 7

def on_complete(env)
  sanitized = sanitize_request(env)
  case env[:status]
  when 401
    raise Unauthorized.new(sanitized.to_s, sanitized)
  when 403
    raise Forbidden.new(sanitized.to_s, sanitized)
  when 404
    raise NotFound.new(sanitized.to_s, sanitized)
  when 422
    raise EntityAlreadyExists.new(sanitized.to_s, sanitized)
  when 400..499
    raise ClientError.new(sanitized.to_s, sanitized)
  when 500..599
    raise ServerError.new(sanitized.to_s, sanitized)
  end
end

#sanitize_request(env) ⇒ Object


25
26
27
28
29
30
31
32
33
34
# File 'lib/librato/metrics/middleware/expects_status.rb', line 25

def sanitize_request(env)
  {
    status: env.status,
    url: env.url.to_s,
    user_agent: env.request_headers["User-Agent"],
    request_body: env[:request_body],
    response_headers: env.response_headers,
    response_body: env.body
  }
end