diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index a654e6a..61367b5 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -50,7 +50,7 @@ Lint/UselessAssignment: # Configuration parameters: CountComments, ExcludedMethods. # ExcludedMethods: refine Metrics/BlockLength: - Max: 123 + Max: 128 # Offense count: 3 # Configuration parameters: CountComments, ExcludedMethods. @@ -60,7 +60,7 @@ Metrics/MethodLength: # Offense count: 1 # Configuration parameters: CountComments. Metrics/ModuleLength: - Max: 101 + Max: 105 # Offense count: 6 # Configuration parameters: CountKeywordArgs. diff --git a/fixtures/vcr_cassettes/create_agreement_failure.yml b/fixtures/vcr_cassettes/create_agreement_failure.yml new file mode 100644 index 0000000..b33d591 --- /dev/null +++ b/fixtures/vcr_cassettes/create_agreement_failure.yml @@ -0,0 +1,46 @@ +--- +http_interactions: +- request: + method: post + uri: https://api.eu1.echosign.com/api/rest/v5/agreements + body: + encoding: UTF-8 + string: '{"documentCreationInfo":{"fileInfos":[[{"documentURL":{"url":"http://findgoplayers.com/resume.pdf","mimeType":"application/pdf","name":"resume.pdf"}}]],"recipientSetInfos":[{"recipientSetMemberInfos":{"email":"goishi.san@gmail.com"},"recipientSetRole":"SIGNER"}],"signatureFlow":"SENDER_SIGNS_LAST","signatureType":"ESIGN","name":"Rumplestiltskin + Contract"}}' + headers: + Access-Token: + - 3AAABLblqZhAN-cxVlpFIUv3XdlgqlyWF8qVbIWnUmmvkAB4u6yPBE50XAqTqzLNCjbWS8QKAZPdgYOaqZHv6EE5LEJOc5NOK + Content-Type: + - application/json + response: + status: + code: 401 + message: Unauthorized + headers: + Date: + - Thu, 07 Feb 2019 02:01:30 GMT + Server: + - Apache + X-Request-Id: + - 597be404-e6ab-496b-88f2-57ee62fadb34 + Content-Length: + - '91' + Strict-Transport-Security: + - max-age=31536000; + X-Xss-Protection: + - 1; mode=block + X-Content-Type-Options: + - nosniff + X-Robots-Tag: + - none + P3p: + - CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT" + Content-Type: + - application/json;charset=UTF-8 + body: + encoding: UTF-8 + string: '{"code":"INVALID_ACCESS_TOKEN","message":"Access token provided is + invalid or has expired"}' + http_version: + recorded_at: Thu, 07 Feb 2019 02:01:30 GMT +recorded_with: VCR 4.0.0 diff --git a/lib/echosign/request.rb b/lib/echosign/request.rb index 1004b7e..78b314f 100644 --- a/lib/echosign/request.rb +++ b/lib/echosign/request.rb @@ -130,10 +130,11 @@ def self.get(endpoint, headers) endpoint, headers: headers ) - response rescue Exception => error raise_error(error) end + + check_response(response) end def self.post(endpoint, body, headers, options = {}) @@ -146,20 +147,22 @@ def self.post(endpoint, body, headers, options = {}) body = body.to_json if body.is_a?(Hash) end response = HTTParty.post(endpoint, body: body, headers: headers) - response rescue Exception => error raise_error(error) end + + check_response(response) end def self.put(endpoint, query, headers) begin headers['Content-Type'] = 'application/json' response = HTTParty.put(endpoint, body: query, headers: headers) - response rescue Exception => error raise_error(error) end + + check_response(response) end def self.add_query(url, query) @@ -172,4 +175,9 @@ def self.raise_error(error) "https://secure.echosign.com/public/docs/restapi/v5" raise Failure.new message, error end + + def self.check_response(response) + raise_error(response) unless response.success? + response + end end diff --git a/spec/lib/agreement/client_spec.rb b/spec/lib/agreement/client_spec.rb index 6f1f873..3492bc8 100644 --- a/spec/lib/agreement/client_spec.rb +++ b/spec/lib/agreement/client_spec.rb @@ -45,6 +45,12 @@ expect(agreement_id).to_not be_nil end end + + it 'raises a specific exception on failure' do + VCR.use_cassette('create_agreement_failure', record: :once) do + expect { client.create_agreement(agreement) }.to raise_error(Echosign::Request::Failure) + end + end end describe '.get_agreements' do