bandwidth_error.rb
1# frozen_string_literal: true
2
3class BandwidthError < StandardError
4 attr_reader :code
5
6 def self.for(http_code, body)
7 parsed_body = JSON.parse(body) rescue nil
8 if parsed_body.is_a?(Hash)
9 new(
10 http_code,
11 parsed_body["description"].to_s.strip + " " +
12 (parsed_body.fetch("fieldErrors", []))
13 .map { |err| err["description"] }.join(" ")
14 )
15 else
16 new(http_code, "Error #{http_code}")
17 end
18 end
19
20 def initialize(code, msg)
21 super(msg)
22 @code = code
23 end
24end