Skip to content

Commit 55f2f10

Browse files
committed
Adds error tests & method to get response object from Error class
1 parent ad09367 commit 55f2f10

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

lib/layer/api/error.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ def initialize(response)
2020
super(build_error_message)
2121
end
2222

23+
def response
24+
@response
25+
end
26+
2327
private
2428

2529
def build_error_message

lib/layer/api/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Layer
22
module Api
3-
VERSION = "0.1.1"
3+
VERSION = "0.2.0"
44
end
55
end

spec/layer/error_spec.rb

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
require 'spec_helper'
2+
3+
describe Layer::Api::Error do
4+
describe ".from_response" do
5+
it 'should raise NotFound error when status is 404' do
6+
VCR.use_cassette('conversation') do
7+
layer = Layer::Api::Client.new
8+
9+
expect {
10+
layer.get_conversation("notfound")
11+
}.to raise_error(Layer::Api::NotFound)
12+
end
13+
end
14+
15+
it "should raise BadRequest error when status is 400" do
16+
response = {status: 400}
17+
expect {
18+
raise Layer::Api::Error.from_response(response)
19+
}.to raise_error(Layer::Api::BadRequest)
20+
end
21+
22+
(500..599).each do |error|
23+
it "should raise ServerError when status is #{error}" do
24+
response = {status: error}
25+
expect {
26+
raise Layer::Api::Error.from_response(response)
27+
}.to raise_error(Layer::Api::ServerError)
28+
end
29+
end
30+
31+
it "should return Error when status isn't found/handled" do
32+
expect {
33+
raise Layer::Api::Error.from_response({})
34+
}.to raise_error(Layer::Api::Error)
35+
end
36+
end
37+
38+
describe ".initialize" do
39+
it "should set @response instance variable" do
40+
begin
41+
raise Layer::Api::Error.new({})
42+
rescue => e
43+
expect(e.response).to eq({})
44+
end
45+
end
46+
47+
it "should build correct error message containing status & body" do
48+
status = '200'
49+
body = 'random_test_body'
50+
args = {
51+
status: status,
52+
body: body
53+
}
54+
error_msg = Layer::Api::Error.new(args).send(:build_error_message)
55+
expect(error_msg).to include(status)
56+
expect(error_msg).to include(body)
57+
end
58+
end
59+
end

0 commit comments

Comments
 (0)