Skip to content

Commit 1312c03

Browse files
committed
Use inheritance for Error messages. Allow passing a message into an Error, like StandardError allows
1 parent 6d12f68 commit 1312c03

File tree

2 files changed

+20
-30
lines changed

2 files changed

+20
-30
lines changed

lib/jsonapi/consumer/errors.rb

+14-24
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,45 @@ module JSONAPI::Consumer
22
module Errors
33
class ApiError < StandardError
44
attr_reader :env
5-
def initialize(env)
5+
def initialize(message, env=nil)
6+
super(message)
67
@env = env
78
end
89
end
910

10-
class ClientError < ApiError
11-
end
12-
13-
class AccessDenied < ClientError
14-
end
15-
16-
class NotAuthorized < ClientError
17-
end
18-
19-
class ConnectionError < ApiError
20-
end
11+
class ClientError < ApiError; end
12+
class AccessDenied < ClientError; end
13+
class NotAuthorized < ClientError; end
14+
class ConnectionError < ApiError; end
2115

2216
class ServerError < ApiError
23-
def message
24-
"Internal server error"
17+
def initialize(message, env=nil)
18+
super("Internal server error", env)
2519
end
2620
end
2721

2822
class Conflict < ServerError
29-
def message
30-
"Resource already exists"
23+
def initialize(message, env=nil)
24+
super("Resource already exists", env)
3125
end
3226
end
3327

3428
class NotFound < ServerError
3529
attr_reader :uri
36-
def initialize(uri)
30+
31+
def initialize(message, uri=nil)
32+
super("Couldn't find resource at: #{uri.to_s}")
3733
@uri = uri
3834
end
39-
def message
40-
"Couldn't find resource at: #{uri.to_s}"
41-
end
4235
end
4336

4437
class UnexpectedStatus < ServerError
4538
attr_reader :code, :uri
4639
def initialize(code, uri)
40+
super("Unexpected response status: #{code} from: #{uri.to_s}")
4741
@code = code
4842
@uri = uri
4943
end
50-
def message
51-
"Unexpected response status: #{code} from: #{uri.to_s}"
52-
end
5344
end
54-
5545
end
5646
end

lib/jsonapi/consumer/middleware/status.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def call(environment)
1212
end
1313
end
1414
rescue Faraday::ConnectionFailed, Faraday::TimeoutError
15-
raise Errors::ConnectionError, environment
15+
raise Errors::ConnectionError.new(nil, environment)
1616
end
1717

1818
protected
@@ -21,17 +21,17 @@ def handle_status(code, env)
2121
case code
2222
when 200..399
2323
when 401
24-
raise Errors::NotAuthorized, env
24+
raise Errors::NotAuthorized.new(nil, env)
2525
when 403
26-
raise Errors::AccessDenied, env
26+
raise Errors::AccessDenied.new(nil, env)
2727
when 404
28-
raise Errors::NotFound, env[:url]
28+
raise Errors::NotFound.new(nil, env[:url])
2929
when 409
30-
raise Errors::Conflict, env
30+
raise Errors::Conflict.new(nil, env)
3131
when 400..499
3232
# some other error
3333
when 500..599
34-
raise Errors::ServerError, env
34+
raise Errors::ServerError.new(nil, env)
3535
else
3636
raise Errors::UnexpectedStatus.new(code, env[:url])
3737
end

0 commit comments

Comments
 (0)