Module: Bugsage::HttpErrorCapture

Defined in:
lib/bugsage/http_error_capture.rb

Class Method Summary collapse

Class Method Details

.bugsage_path?(env) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
# File 'lib/bugsage/http_error_capture.rb', line 77

def bugsage_path?(env)
  path = env["PATH_INFO"].to_s
  path.start_with?("/bugsage")
end

.build_exception(env, status, body) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/bugsage/http_error_capture.rb', line 17

def build_exception(env, status, body)
  body_text = body.to_s.strip
  location = location_for(env)
  message = build_message(status, body_text, location)

  HttpResponseError.new(
    status: status.to_i,
    message: message,
    response_body: body_text.empty? ? nil : body_text,
    location: location
  )
end

.build_message(status, body, location) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/bugsage/http_error_capture.rb', line 47

def build_message(status, body, location)
  status_label = http_status_label(status)
  detail = extract_detail(body)
  base = Bugsage.t("http_errors.message.base", status: status, status_label: status_label)
  unknown = Bugsage.t("common.unknown")
  base += Bugsage.t("http_errors.message.at_location", location: location) unless location == unknown
  detail.empty? ? base : "#{base}#{Bugsage.t("http_errors.message.with_detail", detail: detail)}"
end

.capture?(status, env) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
13
14
15
# File 'lib/bugsage/http_error_capture.rb', line 9

def capture?(status, env)
  return false unless status.to_i >= 400
  return false if bugsage_path?(env)
  return false if ExceptionSupport.extract(env)

  true
end

.confidence_for_status(status) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/bugsage/http_error_capture.rb', line 96

def confidence_for_status(status)
  case status.to_i
  when 400, 401, 422 then 86
  when 403, 404 then 84
  else 80
  end
end

.extract_detail(body) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/bugsage/http_error_capture.rb', line 56

def extract_detail(body)
  return "" if body.to_s.strip.empty?

  payload = JSON.parse(body)
  return body.strip unless payload.is_a?(Hash)

  detail = payload["error"] || payload["message"] || payload["errors"]
  case detail
  when String
    detail.strip
  when Hash
    detail.map { |key, value| "#{key}: #{value}" }.join(", ")
  when Array
    detail.join(", ")
  else
    body.strip
  end
rescue JSON::ParserError
  body.strip
end

.fixes_for_status(status) ⇒ Object



90
91
92
93
94
# File 'lib/bugsage/http_error_capture.rb', line 90

def fixes_for_status(status)
  fixes = Bugsage.t("http_errors.fixes.#{status.to_i}", default: nil)
  fixes = Bugsage.t("http_errors.fixes.default", default: nil) if fixes.nil?
  Array(fixes)
end

.format_controller_name(controller) ⇒ Object



41
42
43
44
45
# File 'lib/bugsage/http_error_capture.rb', line 41

def format_controller_name(controller)
  controller.to_s.split("/").map do |segment|
    segment.split("_").map(&:capitalize).join
  end.join("::")
end

.http_status_label(status) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/bugsage/http_error_capture.rb', line 82

def http_status_label(status)
  if defined?(Rack::Utils)
    Rack::Utils::HTTP_STATUS_CODES[status.to_i] || Bugsage.t("http_errors.status_label_fallback", status: status)
  else
    Bugsage.t("http_errors.status_label_fallback", status: status)
  end
end

.location_for(env) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/bugsage/http_error_capture.rb', line 30

def location_for(env)
  params = env["action_dispatch.request.path_parameters"]
  return Bugsage.t("common.unknown") unless params.is_a?(Hash)

  controller = params[:controller] || params["controller"]
  action = params[:action] || params["action"]
  return Bugsage.t("common.unknown") if controller.to_s.strip.empty?

  "#{format_controller_name(controller)}Controller##{action}"
end