File: error.rb

package info (click to toggle)
ruby-gh 0.21.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,644 kB
  • sloc: ruby: 1,793; makefile: 4
file content (71 lines) | stat: -rw-r--r-- 1,861 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# frozen_string_literal: true

require 'gh'

module GH
  class Error < StandardError
    attr_reader :info

    def initialize(error = nil, payload = nil, info = {})
      super(error)

      info = info.merge(error.info) if error.respond_to?(:info) && error.info.is_a?(Hash)
      error = error.error while error.respond_to? :error
      @info = info.merge(error:, payload:)

      return unless error

      set_backtrace error.backtrace if error.respond_to? :backtrace
      return unless error.respond_to?(:response) && error.response

      case response = error.response
      when Hash
        @info[:response_status] = response[:status]
        @info[:response_headers] = response[:headers]
        @info[:response_body] = response[:body]
      when Faraday::Response
        @info[:response_status] = response.status
        @info[:response_headers] = response.headers
        @info[:response_body] = response.body
      else
        @info[:response] = response
      end
    end

    def payload
      info[:payload]
    end

    def error
      info[:error]
    end

    def message
      (['GH request failed'] + info.map { |k, v| entry(k, v) }).join("\n")
    end

    private

    def entry(key, value)
      value = "#{value.class}: #{value.message}" if value.is_a?(Exception)
      value = value.inspect unless value.is_a?(String)
      value.gsub!(/"Basic .+"|(client_(?:id|secret)=)[^&\s]+/, '\1[removed]')
      "#{key}: ".ljust(20) + value
    end
  end

  class TokenInvalid < Error
  end

  def self.Error(conditions)
    Module.new do
      define_singleton_method(:===) do |exception|
        return false unless exception.is_a?(Error) && !exception.info.nil?

        # rubocop:disable Style/CaseEquality
        conditions.all? { |k, v| v === exception.info[k] }
        # rubocop:enable Style/CaseEquality
      end
    end
  end
end