restify 1.9.0 → 1.10.0

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
  SHA256:
3
- metadata.gz: fabe3a3c7e412889e4fe339d9d422fc5c0c94147ca53c342888d7e5eb00608c2
4
- data.tar.gz: 07b987075ca1987a95574298daf1d89c8cc398e7e95aaf48dde2fa2473d683cb
3
+ metadata.gz: f44b84123c73ffd08cc8668448b03470c06acaf0f693e844fface91ff44179b5
4
+ data.tar.gz: c194b2db48a18a7f234bba20ff4161f98277b1146e6b7bf2572c877c285c3794
5
5
  SHA512:
6
- metadata.gz: 3b83864c86be0e6449d25224f748dcb506d47bbeba47f3587280399c54eb5059aec0a85a8dc85f6d142c262efeab4f01f53595d0840c318507fbfa119dacd3e6
7
- data.tar.gz: a2d06c226182e654966690460768f8bdcb3c9bfbdd2de35da642ab2826e6eddbea871667564872a1635a0be157202c40a48cbf305befdca52cf29430482439e9
6
+ metadata.gz: 89de3a2282568b6cd4b58afdf04e2271abf283721a25535e5420a4b1e52e4ed6b3edf894180d6fd15ee010c7692f99942c047fe7baef81d5bd67ff13b756e189
7
+ data.tar.gz: e6b18bfd819aec933acdb9fbec820ea7e742918eebc795c77f5f7ba0b927ccae85b876b367df8325d90e2899aad77bcf30f691781419669fd7406a156e197f86
@@ -1,5 +1,10 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.10.0
4
+
5
+ * Raise more specific error on a few status codes (#17)
6
+ * Complete promises with an empty list (but a list) of dependencies (#18)
7
+
3
8
  ## 1.9.0
4
9
 
5
10
  * Do not raise error on 3XX responses but return responses
@@ -62,7 +62,7 @@ module Restify
62
62
  if !response.errored?
63
63
  process response
64
64
  else
65
- Context.raise_response_error(response)
65
+ raise ResponseError.from_code(response)
66
66
  end
67
67
  end
68
68
  end
@@ -101,18 +101,5 @@ module Restify
101
101
  def default_headers
102
102
  options.fetch(:headers, {})
103
103
  end
104
-
105
- class << self
106
- def raise_response_error(response)
107
- case response.code
108
- when 400...500
109
- raise ClientError.new(response)
110
- when 500...600
111
- raise ServerError.new(response)
112
- else
113
- raise "Unknown response code: #{response.code}"
114
- end
115
- end
116
- end
117
104
  end
118
105
  end
@@ -20,6 +20,27 @@ module Restify
20
20
  class ResponseError < StandardError
21
21
  attr_reader :response
22
22
 
23
+ def self.from_code(response)
24
+ case response.code
25
+ when 400
26
+ BadRequest.new(response)
27
+ when 401
28
+ Unauthorized.new(response)
29
+ when 404
30
+ NotFound.new(response)
31
+ when 406
32
+ NotAcceptable.new(response)
33
+ when 422
34
+ UnprocessableEntity.new(response)
35
+ when 400...500
36
+ ClientError.new(response)
37
+ when 500...600
38
+ ServerError.new(response)
39
+ else
40
+ raise "Unknown response code: #{response.code}"
41
+ end
42
+ end
43
+
23
44
  def initialize(response)
24
45
  @response = response
25
46
  super "#{response.message} (#{response.code}) for `#{response.uri}':\n" \
@@ -65,4 +86,15 @@ module Restify
65
86
  # A {ServerError} will be raised when a response has a
66
87
  # 5XX status code.
67
88
  class ServerError < ResponseError; end
89
+
90
+ ###
91
+ # CONCRETE SUBCLASSES FOR TYPICAL STATUS CODES
92
+ #
93
+ # This makes it easy to rescue specific expected error types.
94
+
95
+ class BadRequest < ClientError; end
96
+ class Unauthorized < ClientError; end
97
+ class NotFound < ClientError; end
98
+ class NotAcceptable < ClientError; end
99
+ class UnprocessableEntity < ClientError; end
68
100
  end
@@ -7,6 +7,13 @@ module Restify
7
7
  @dependencies = dependencies.flatten
8
8
 
9
9
  super(&nil)
10
+
11
+ # When dependencies were passed in, but none are left after flattening,
12
+ # then we don't have to wait for explicit dependencies or resolution
13
+ # through a writer.
14
+ if !@task && @dependencies.empty? && dependencies.any?
15
+ complete true, [], nil
16
+ end
10
17
  end
11
18
 
12
19
  def wait(timeout = nil)
@@ -3,7 +3,7 @@
3
3
  module Restify
4
4
  module VERSION
5
5
  MAJOR = 1
6
- MINOR = 9
6
+ MINOR = 10
7
7
  PATCH = 0
8
8
  STAGE = nil
9
9
  STRING = [MAJOR, MINOR, PATCH, STAGE].reject(&:nil?).join('.').freeze
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Restify do
6
+ let!(:request_stub) do
7
+ stub_request(:get, 'http://localhost/base')
8
+ .to_return do
9
+ <<-RESPONSE.gsub(/^ {8}/, '')
10
+ HTTP/1.1 #{http_status}
11
+ Content-Length: 333
12
+ Transfer-Encoding: chunked
13
+ Link: <http://localhost/other>; rel="neat"
14
+ RESPONSE
15
+ end
16
+ end
17
+
18
+ let(:http_status) { '200 OK' }
19
+
20
+ describe 'Error handling' do
21
+ subject(:request) { Restify.new('http://localhost/base').get.value! }
22
+
23
+ context 'for 400 status codes' do
24
+ let(:http_status) { '400 Bad Request' }
25
+
26
+ it 'throws a BadRequest exception' do
27
+ expect { request }.to raise_error Restify::BadRequest
28
+ end
29
+ end
30
+
31
+ context 'for 401 status codes' do
32
+ let(:http_status) { '401 Unauthorized' }
33
+
34
+ it 'throws an Unauthorized exception' do
35
+ expect { request }.to raise_error Restify::Unauthorized
36
+ end
37
+ end
38
+
39
+ context 'for 404 status codes' do
40
+ let(:http_status) { '404 Not Found' }
41
+
42
+ it 'throws a ClientError exception' do
43
+ expect { request }.to raise_error Restify::NotFoundError
44
+ end
45
+ end
46
+
47
+ context 'for 406 status codes' do
48
+ let(:http_status) { '406 Not Acceptable' }
49
+
50
+ it 'throws a NotAcceptable exception' do
51
+ expect { request }.to raise_error Restify::NotAcceptable
52
+ end
53
+ end
54
+
55
+ context 'for 422 status codes' do
56
+ let(:http_status) { '422 Unprocessable Entity' }
57
+
58
+ it 'throws a UnprocessableEntity exception' do
59
+ expect { request }.to raise_error Restify::UnprocessableEntity
60
+ end
61
+ end
62
+
63
+ context 'for any other 4xx status codes' do
64
+ let(:http_status) { '415 Unsupported Media Type' }
65
+
66
+ it 'throws a generic ClientError exception' do
67
+ expect { request }.to raise_error Restify::ClientError
68
+ end
69
+ end
70
+
71
+ context 'for any 5xx status codes' do
72
+ let(:http_status) { '500 Internal Server Error' }
73
+
74
+ it 'throws a generic ServerError exception' do
75
+ expect { request }.to raise_error Restify::ServerError
76
+ end
77
+ end
78
+ end
79
+ end
@@ -163,6 +163,13 @@ describe Restify::Promise do
163
163
  expect(subject).to eq 17
164
164
  end
165
165
  end
166
+
167
+ # Nobody does this explicitly, but it can happen when the array of
168
+ # dependencies is built dynamically.
169
+ context 'with an empty array of dependencies and without task' do
170
+ subject { described_class.new([]).value! }
171
+ it { is_expected.to eq [] }
172
+ end
166
173
  end
167
174
 
168
175
  describe '#wait' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: restify
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.9.0
4
+ version: 1.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Graichen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-13 00:00:00.000000000 Z
11
+ date: 2018-12-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -188,6 +188,7 @@ files:
188
188
  - spec/restify/context_spec.rb
189
189
  - spec/restify/features/head_requests_spec.rb
190
190
  - spec/restify/features/request_headers_spec.rb
191
+ - spec/restify/features/response_errors.rb
191
192
  - spec/restify/global_spec.rb
192
193
  - spec/restify/link_spec.rb
193
194
  - spec/restify/processors/base_spec.rb
@@ -229,6 +230,7 @@ test_files:
229
230
  - spec/restify/context_spec.rb
230
231
  - spec/restify/features/head_requests_spec.rb
231
232
  - spec/restify/features/request_headers_spec.rb
233
+ - spec/restify/features/response_errors.rb
232
234
  - spec/restify/global_spec.rb
233
235
  - spec/restify/link_spec.rb
234
236
  - spec/restify/processors/base_spec.rb