Skip to content

Commit

Permalink
Add the system for exception
Browse files Browse the repository at this point in the history
  • Loading branch information
jue58 committed Feb 2, 2017
1 parent 4172b61 commit 0cbd230
Show file tree
Hide file tree
Showing 24 changed files with 944 additions and 157 deletions.
7 changes: 5 additions & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Style/Documentation:
Enabled: false

# Configuration parameters: AllowURI.
Style/LineLength:
Metrics/LineLength:
Max: 1_00

# Cop supports --auto-correct.
Expand All @@ -40,4 +40,7 @@ Style/FrozenStringLiteralComment:

Style/SymbolProc:
Exclude:
- 'lib/kintone/query.rb'
- 'lib/kintone/query.rb'

Style/SignalException:
EnforcedStyle: only_raise
28 changes: 10 additions & 18 deletions lib/kintone/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,16 @@
require 'kintone/command/accessor'
require 'kintone/api/guest'
require 'kintone/query'
require 'kintone/kintone_error'

class Kintone::Api
BASE_PATH = '/k/v1/'.freeze
COMMAND = '%s.json'.freeze
ACCESSIBLE_COMMAND = [
:record,
:records,
:form,
:app_acl,
:record_acl,
:field_acl,
:template_space,
:space,
:space_body,
:space_thread,
:space_members,
:guests,
:app,
:apps,
:apis,
:bulk_request,
:bulk,
:file
:record, :records, :form, :app_acl, :record_acl,
:field_acl, :template_space, :space, :space_body, :space_thread,
:space_members, :guests, :app, :apps, :apis,
:bulk_request, :bulk, :file
].freeze

def initialize(domain, user, password = nil)
Expand All @@ -54,6 +41,7 @@ def get(url, params = {})
request.url url
request.params = params
end
raise Kintone::KintoneError.new(response.body, response.status) if response.status != 200
response.body
end

Expand All @@ -64,6 +52,7 @@ def post(url, body)
request.headers['Content-Type'] = 'application/json'
request.body = body.to_json
end
raise Kintone::KintoneError.new(response.body, response.status) if response.status != 200
response.body
end

Expand All @@ -74,6 +63,7 @@ def put(url, body)
request.headers['Content-Type'] = 'application/json'
request.body = body.to_json
end
raise Kintone::KintoneError.new(response.body, response.status) if response.status != 200
response.body
end

Expand All @@ -84,6 +74,7 @@ def delete(url, body = nil)
request.headers['Content-Type'] = 'application/json'
request.body = body.to_json
end
raise Kintone::KintoneError.new(response.body, response.status) if response.status != 200
response.body
end

Expand All @@ -94,6 +85,7 @@ def post_file(url, path, content_type, original_filename)
request.headers['Content-Type'] = 'multipart/form-data'
request.body = { file: Faraday::UploadIO.new(path, content_type, original_filename) }
end
raise Kintone::KintoneError.new(response.body, response.status) if response.status != 200
response.body['fileKey']
end

Expand Down
11 changes: 11 additions & 0 deletions lib/kintone/kintone_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Kintone::KintoneError < StandardError
attr_reader :message_text, :id, :code, :http_status

def initialize(messages, http_status)
@message_text = messages['message']
@id = messages['id']
@code = messages['code']
@http_status = http_status
super(format('%s [%s] %s(%s)', @http_status, @code, @message_text, @id))
end
end
76 changes: 76 additions & 0 deletions spec/kintone/api/guest_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,25 @@
let(:params) { { p1: 'abc', p2: 'def' } }

it { expect(subject).to eq 'abc' => 'def' }

context 'fail to request' do
before(:each) do
stub_request(
:get,
'https://example.cybozu.com/k/guest/1/v1/path.json'
)
.with(
headers: { 'X-Cybozu-Authorization' => 'QWRtaW5pc3RyYXRvcjpjeWJvenU=' },
query: params
)
.to_return(
body: '{"message":"不正なJSON文字列です。","id":"1505999166-897850006","code":"CB_IJ01"}',
status: 500
)
end

it { expect { subject }.to raise_error Kintone::KintoneError }
end
end

describe '#post' do
Expand All @@ -63,6 +82,25 @@
let(:body) { { 'p1' => 'abc', 'p2' => 'def' } }

it { expect(subject).to eq 'abc' => 'def' }

context 'fail to request' do
before(:each) do
stub_request(
:post,
'https://example.cybozu.com/k/guest/1/v1/path.json'
)
.with(
headers: { 'X-Cybozu-Authorization' => 'QWRtaW5pc3RyYXRvcjpjeWJvenU=' },
body: '{"p1":"abc","p2":"def"}'
)
.to_return(
body: '{"message":"不正なJSON文字列です。","id":"1505999166-897850006","code":"CB_IJ01"}',
status: 500
)
end

it { expect { subject }.to raise_error Kintone::KintoneError }
end
end

describe '#put' do
Expand All @@ -88,6 +126,25 @@
let(:body) { { 'p1' => 'abc', 'p2' => 'def' } }

it { expect(subject).to eq 'abc' => 'def' }

context 'fail to request' do
before(:each) do
stub_request(
:put,
'https://example.cybozu.com/k/guest/1/v1/path.json'
)
.with(
headers: { 'X-Cybozu-Authorization' => 'QWRtaW5pc3RyYXRvcjpjeWJvenU=' },
body: '{"p1":"abc","p2":"def"}'
)
.to_return(
body: '{"message":"不正なJSON文字列です。","id":"1505999166-897850006","code":"CB_IJ01"}',
status: 500
)
end

it { expect { subject }.to raise_error Kintone::KintoneError }
end
end

describe '#delete' do
Expand All @@ -113,6 +170,25 @@
let(:params) { { 'p1' => 'abc', 'p2' => 'def' } }

it { expect(subject).to eq 'abc' => 'def' }

context 'fail to request' do
before(:each) do
stub_request(
:delete,
'https://example.cybozu.com/k/guest/1/v1/path.json'
)
.with(
body: { 'p1' => 'abc', 'p2' => 'def' }.to_json,
headers: { 'X-Cybozu-Authorization' => 'QWRtaW5pc3RyYXRvcjpjeWJvenU=' }
)
.to_return(
body: '{"message":"不正なJSON文字列です。","id":"1505999166-897850006","code":"CB_IJ01"}',
status: 500
)
end

it { expect { subject }.to raise_error Kintone::KintoneError }
end
end

describe '#record' do
Expand Down
108 changes: 104 additions & 4 deletions spec/kintone/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
describe '#get_url' do
subject { target.get_url(command) }

context '' do
let(:command) { 'path' }
it { is_expected.to eq('/k/v1/path.json') }
end
let(:command) { 'path' }

it { is_expected.to eq('/k/v1/path.json') }
end

describe '#guest' do
Expand Down Expand Up @@ -91,6 +90,28 @@

it { is_expected.to eq 'abc' => 'def' }
end

context 'fail to request' do
before(:each) do
stub_request(
:get,
'https://www.example.com/k/v1/path'
)
.with(
query: query,
headers: { 'X-Cybozu-Authorization' => 'QWRtaW5pc3RyYXRvcjpjeWJvenU=' }
)
.to_return(
body: '{"message":"不正なJSON文字列です。","id":"1505999166-897850006","code":"CB_IJ01"}',
status: 500
)
end

let(:params) { {} }
let(:query) { nil }

it { expect { subject }.to raise_error Kintone::KintoneError }
end
end

describe '#post' do
Expand Down Expand Up @@ -118,6 +139,28 @@
let(:body) { { 'p1' => 'abc', 'p2' => 'def' } }

it { is_expected.to eq 'abc' => 'def' }

context 'fail to request' do
before(:each) do
stub_request(
:post,
'https://www.example.com/k/v1/path'
)
.with(
headers: {
'X-Cybozu-Authorization' => 'QWRtaW5pc3RyYXRvcjpjeWJvenU=',
'Content-Type' => 'application/json'
},
body: '{"p1":"abc","p2":"def"}'
)
.to_return(
body: '{"message":"不正なJSON文字列です。","id":"1505999166-897850006","code":"CB_IJ01"}',
status: 500
)
end

it { expect { subject }.to raise_error Kintone::KintoneError }
end
end

describe '#put' do
Expand Down Expand Up @@ -145,6 +188,28 @@
let(:body) { { 'p1' => 'abc', 'p2' => 'def' } }

it { is_expected.to eq 'abc' => 'def' }

context 'fail to request' do
before(:each) do
stub_request(
:put,
'https://www.example.com/k/v1/path'
)
.with(
headers: {
'X-Cybozu-Authorization' => 'QWRtaW5pc3RyYXRvcjpjeWJvenU=',
'Content-Type' => 'application/json'
},
body: '{"p1":"abc","p2":"def"}'
)
.to_return(
body: '{"message":"不正なJSON文字列です。","id":"1505999166-897850006","code":"CB_IJ01"}',
status: 500
)
end

it { expect { subject }.to raise_error Kintone::KintoneError }
end
end

describe '#delete' do
Expand All @@ -169,6 +234,25 @@
let(:params) { { 'p1' => 'abc', 'p2' => 'def' } }

it { is_expected.to eq 'abc' => 'def' }

context 'fail to request' do
before(:each) do
stub_request(
:delete,
'https://www.example.com/k/v1/path'
)
.with(
body: { 'p1' => 'abc', 'p2' => 'def' }.to_json,
headers: { 'X-Cybozu-Authorization' => 'QWRtaW5pc3RyYXRvcjpjeWJvenU=' }
)
.to_return(
body: '{"message":"不正なJSON文字列です。","id":"1505999166-897850006","code":"CB_IJ01"}',
status: 500
)
end

it { expect { subject }.to raise_error Kintone::KintoneError }
end
end

describe '#post_file' do
Expand All @@ -195,6 +279,22 @@
let(:original_filename) { 'fileName.txt' }

it { is_expected.to eq 'abc' }

context 'fail to request' do
before(:each) do
stub_request(
:post,
'https://www.example.com/k/v1/path'
)
.with(headers: { 'X-Cybozu-Authorization' => 'QWRtaW5pc3RyYXRvcjpjeWJvenU=' }) { attachment } # rubocop:disable Style/LineLength
.to_return(
body: '{"message":"不正なJSON文字列です。","id":"1505999166-897850006","code":"CB_IJ01"}',
status: 500
)
end

it { expect { subject }.to raise_error Kintone::KintoneError }
end
end

describe '#record' do
Expand Down
Loading

0 comments on commit 0cbd230

Please sign in to comment.