Class: Fbe::Middleware::Formatter

Inherits:
Faraday::Logging::Formatter
  • Object
show all
Defined in:
lib/fbe/middleware/formatter.rb

Overview

Faraday logging formatter show verbose log for only error response

Author

Yegor Bugayenko (yegor256@gmail.com)

Copyright

Copyright © 2024 Zerocracy

License

MIT

Instance Method Summary collapse

Instance Method Details

#request(http) ⇒ Object

Log HTTP request.

Parameters:

  • http (Hash)

    The hash with data about HTTP request

[View source]

38
39
40
# File 'lib/fbe/middleware/formatter.rb', line 38

def request(http)
  @req = http
end

#response(http) ⇒ Object

Log HTTP response.

Parameters:

  • http (Hash)

    The hash with data about HTTP response

[View source]

45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/fbe/middleware/formatter.rb', line 45

def response(http)
  return if http.status < 400
  if http.status == 403 && http.response_headers['content-type'].start_with?('application/json')
    warn(
      [
        "#{@req.method.upcase} #{apply_filters(@req.url.to_s)}",
        '->',
        http.status,
        '/',
        JSON.parse(http.response_body)['message']
      ].join(' ')
    )
    return
  end
  error(
    [
      "#{@req.method.upcase} #{apply_filters(@req.url.to_s)} HTTP/1.1",
      shifted(apply_filters(dump_headers(@req.request_headers))),
      '',
      shifted(apply_filters(@req.request_body)),
      "HTTP/1.1 #{http.status}",
      shifted(apply_filters(dump_headers(http.response_headers))),
      '',
      shifted(apply_filters(http.response_body))
    ].join("\n")
  )
end