Exception: JMA::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/jma/error.rb

Overview

Custom error class for rescuing from all API errors Inspired by github.com/octokit/octokit.rb

Direct Known Subclasses

ClientError, ServerError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response = nil) ⇒ Error

Returns a new instance of Error.



78
79
80
81
# File 'lib/jma/error.rb', line 78

def initialize(response = nil)
  @response = response
  super(build_error_message)
end

Class Method Details

.error_for_401Object

Returns most appropriate error for 401 HTTP status code



85
86
87
# File 'lib/jma/error.rb', line 85

def self.error_for_401
  JMA::Unauthorized
end

.error_for_403(body) ⇒ Object

Returns most appropriate error for 403 HTTP status code



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/jma/error.rb', line 91

def self.error_for_403(body)
  case body
  when /rate limit exceeded/i
    JMA::TooManyRequests
  when /login attempts exceeded/i
    JMA::TooManyLoginAttempts
  when /abuse/i
    JMA::AbuseDetected
  else
    JMA::Forbidden
  end
end

.error_for_404Object

Return most appropriate error for 404 HTTP status code



106
107
108
# File 'lib/jma/error.rb', line 106

def self.error_for_404
  JMA::NotFound
end

.from_response(response) ⇒ JMA::Error

Returns the appropriate JMA::Error subclass based on status and response message

Parameters:

  • response (Net::HTTPResponse)

    HTTP response

Returns:



12
13
14
15
16
17
18
19
20
21
# File 'lib/jma/error.rb', line 12

def self.from_response(response)
  if response.methods.include? :code
    return unless (klass = match_status response.code.to_i, response.body.to_s)
  else
    return unless (klass = match_status response[:status].to_i,
                                        response[:body].to_s)

  end
  klass.new(response)
end

.match_status(status, body) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/jma/error.rb', line 23

def self.match_status(status, body)
  case status
  when 400
    JMA::BadRequest
  when 401
    error_for_401
  when 403
    error_for_403 body
  when 404
    error_for_404 body
  when 405
    JMA::MethodNotAllowed
  when 406
    JMA::NotAcceptable
  when 409
    JMA::Conflict
  when 400..499
    match_status_410_500 status
  when 500..599
    match_status_over_500 status
  end
end

.match_status_410_500(status) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/jma/error.rb', line 46

def self.match_status_410_500(status)
  case status
  when 415
    JMA::UnsupportedMediaType
  when 422
    JMA::UnprocessableEntity
  when 451
    JMA::UnavailableForLegalReasons
  when 400..499
    JMA::ClientError
  else
    JMA::ClientError
  end
end

.match_status_over_500(status) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/jma/error.rb', line 61

def self.match_status_over_500(status)
  case status
  when 500
    JMA::InternalServerError
  when 501
    JMA::NotImplemented
  when 502
    JMA::BadGateway
  when 503
    JMA::ServiceUnavailable
  when 500..599
    JMA::ServerError
  else
    JMA::ServerError
  end
end

Instance Method Details

#build_error_messageObject



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/jma/error.rb', line 110

def build_error_message
  return nil if @response.nil?

  if @response.methods.include? :code
    "#{@response.code} - #{@response.message}"
  else
    message = "#{@response[:method].to_s.upcase} ".dup
    message << "#{@response[:url]}: "
    message << (@response[:status]).to_s
    message
  end
end