github_api 0.14.3 → 0.14.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7fe5afce457fa73fed135b79c28fc1083e1d0f9d
4
- data.tar.gz: fc77be5cbda3f746328797d7e1b1e9de5c5fdafb
3
+ metadata.gz: 4c2d3c322449a1a15a1f353db2b5c97a018d2967
4
+ data.tar.gz: e06e3b114460915e78d86c8a8156cd5289502329
5
5
  SHA512:
6
- metadata.gz: 6c37cb56444c0a0a02546eb7960bbfdfb18308afad58a31f32da34fdf103e99cb25a7de3dc127066dd199d45e52e7703b0fd2e1b56162d40b341d53bd7e22a22
7
- data.tar.gz: 9f7a7b7daabd4ff6094e2cc222522f8ad19c3403f024c071deb7880f86fb171b5ea86b5f21e73f2d47c244f949a38e29de349e41b945a26335613f313ee70875
6
+ metadata.gz: 6e08c74ed99c5a5120c69f900bfe7cf6ce1d7ead14a2c60ffc6fdc79bd21ec52c47fc95461f9b0d1bfb4f8fae419020d300838f7dadc70d8785dc870fd844c4a
7
+ data.tar.gz: e525db532cc637908073c49fadd7c4c102c0810f3eb949117fa2c863997b3b40c778a1ed8d58a4b2d385bae3c5d0717547b45f8d4efa62aa9803b9b5f6c4d385
data/README.md CHANGED
@@ -77,6 +77,10 @@ gem "github_api"
77
77
  * [4. Pagination](#4-pagination)
78
78
  * [4.1 Auto pagination](#41-auto-pagination)
79
79
  * [5. Error Handling](#5-error-handling)
80
+ * [5.1 Client Error](#51-client-error)
81
+ * [5.2 Service Error](#52-service-error)
82
+ * [5.2.1 Data](#521-data)
83
+ * [5.2.2 Error Messages](#522-error-messages)
80
84
  * [6. Examples](#6-examples)
81
85
  * [6.1 Rails](#61-rails)
82
86
  * [6.2 Manipulating Files](#62-manipulating-files)
@@ -582,14 +586,15 @@ Github::Client::Repos.new.list user: '...', auto_pagination: true
582
586
 
583
587
  ## 5 Error Handling
584
588
 
585
- The generic error class `Github::Error::GithubError` will handle both the client (`Github::Error::ClientError`) and service (`Github::Error::ServiceError`) side errors. For instance in your code you can catch errors like
589
+ The generic error class `Github::Error::GithubError` will handle both the client (`Github::Error::ClientError`) and service (`Github::Error::ServiceError`) side errors.
590
+
591
+ For instance in your code you can catch errors like
586
592
 
587
593
  ```ruby
588
594
  begin
589
595
  # Do something with github_api gem
590
596
  rescue Github::Error::GithubError => e
591
597
  puts e.message
592
-
593
598
  if e.is_a? Github::Error::ServiceError
594
599
  # handle GitHub service errors such as 404
595
600
  elsif e.is_a? Github::Error::ClientError
@@ -598,6 +603,24 @@ rescue Github::Error::GithubError => e
598
603
  end
599
604
  ```
600
605
 
606
+ ### 5.1 Client Error
607
+
608
+ Any time **Github** client has a problem sending request a `Github::Error::ClientError` is raised that will provide a summary of the problem and possible solutions.
609
+
610
+ ### 5.2 Service Error
611
+
612
+ When the **Github** client receives a HTTP response from GitHub service that indicates error then `Github::Error::ServiceError` is raised.
613
+
614
+ There are number of specific error types such as `Github::Error::NotAcceptable` when `406` status code is returned.
615
+
616
+ #### 5.2.1 Data
617
+
618
+ When `Github::Error::ServiceError` is raised you can call `data` to access it payload in JSON format.
619
+
620
+ #### 5.2.2 Error messages
621
+
622
+ Anytime there are error messages provided with `Github::Error::ServiceError` you can access them by calling `error_messages` helper.
623
+
601
624
  ## 6 Examples
602
625
 
603
626
  ### 6.1 Rails
@@ -11,7 +11,7 @@ module Github
11
11
 
12
12
  # Initialize a new Github error object.
13
13
  #
14
- def initialize(message=$!)
14
+ def initialize(message = $!)
15
15
  if message.respond_to?(:backtrace)
16
16
  super(message.message)
17
17
  @response_message = message
@@ -21,7 +21,11 @@ module Github
21
21
  end
22
22
 
23
23
  def backtrace
24
- @response_message ? @response_message.backtrace : super
24
+ if @response_message && @response_message.respond_to?(:backtrace)
25
+ @response_message.backtrace
26
+ else
27
+ super
28
+ end
25
29
  end
26
30
  end # GithubError
27
31
  end # Error
@@ -21,9 +21,11 @@ module Github
21
21
  # @return [Hash[Integer, Object]]
22
22
  #
23
23
  # @api public
24
- def self.errors
25
- @errors ||= Hash[
26
- descendants.map { |klass| [klass.new({}).http_status_code, klass] }
24
+ def self.error_mapping
25
+ @error_mapping ||= Hash[
26
+ descendants.map do |klass|
27
+ [klass.new({}).http_status_code, klass]
28
+ end
27
29
  ]
28
30
  end
29
31
 
@@ -39,9 +41,33 @@ module Github
39
41
  @body = response[:body]
40
42
  @status = response[:status]
41
43
 
44
+ @response_headers = @headers
45
+ @response_message = @body
46
+
42
47
  super(create_message(response))
43
48
  end
44
49
 
50
+ # Expose response payload as JSON object if possible
51
+ #
52
+ # @return [Hash[Symbol]|String]
53
+ #
54
+ # @api public
55
+ def data
56
+ @data ||= decode_data(@body)
57
+ end
58
+
59
+ # Stores error message(s) returned in response body
60
+ #
61
+ # @return [Array[Hash[Symbol]]]
62
+ # the array of hash error objects
63
+ #
64
+ # @api public
65
+ def error_messages
66
+ @error_messages ||= begin
67
+ data[:errors] ? data[:errors] : [data]
68
+ end
69
+ end
70
+
45
71
  private
46
72
 
47
73
  # Create full error message
@@ -58,7 +84,7 @@ module Github
58
84
 
59
85
  message = "#{response[:method].to_s.upcase} "
60
86
  message << "#{response[:url]}: "
61
- message << "#{@status} - #{parse_body(@body)}"
87
+ message << "#{@status} - #{format_response}"
62
88
  message
63
89
  end
64
90
 
@@ -79,17 +105,19 @@ module Github
79
105
  end
80
106
  end
81
107
 
108
+ # Read response body and convert to human friendly format
109
+ #
110
+ # @return [String]
111
+ #
82
112
  # @api private
83
- def parse_body(body)
84
- data = decode_data(body)
85
-
113
+ def format_response
86
114
  return '' if data.nil? || data.empty?
87
115
 
88
116
  case data
89
117
  when Hash
90
118
  message = data[:message] ? data[:message] : ' '
91
119
  docs = data[:documentation_url]
92
- error = create_error_summary(data)
120
+ error = create_error_summary
93
121
  message << error if error
94
122
  message << "\nSee: #{docs}" if docs
95
123
  message
@@ -103,9 +131,9 @@ module Github
103
131
  # @return [String]
104
132
  #
105
133
  # @api private
106
- def create_error_summary(data)
134
+ def create_error_summary
107
135
  if data[:error]
108
- return "\nError: #{data[:error]}"
136
+ "\nError: #{data[:error]}"
109
137
  elsif data[:errors]
110
138
  message = "\nErrors:\n"
111
139
  message << data[:errors].map do |error|
@@ -5,14 +5,17 @@ require 'github_api/error'
5
5
 
6
6
  module Github
7
7
  class Response::RaiseError < Faraday::Response::Middleware
8
-
8
+ # Check if status code requires raising a ServiceError
9
+ #
10
+ # @api private
9
11
  def on_complete(env)
10
12
  status_code = env[:status].to_i
11
13
  service_error = Github::Error::ServiceError
12
- error_class = service_error.errors[status_code]
13
- error_class = service_error if !error_class and (400...600) === status_code
14
+ error_class = service_error.error_mapping[status_code]
15
+ if !error_class and (400...600) === status_code
16
+ error_class = service_error
17
+ end
14
18
  raise error_class.new(env) if error_class
15
19
  end
16
-
17
20
  end # Response::RaiseError
18
21
  end # Github
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module Github
4
- VERSION = "0.14.3"
4
+ VERSION = "0.14.4"
5
5
  end # Github
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: github_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.14.3
4
+ version: 0.14.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Murach
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-02 00:00:00.000000000 Z
11
+ date: 2016-07-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable