Skip to content

Commit b0d8991

Browse files
author
Pedro Belo
committed
change Error interface, take options hash
1 parent 1ae7248 commit b0d8991

File tree

2 files changed

+30
-25
lines changed

2 files changed

+30
-25
lines changed

lib/pliny/errors.rb

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@ def self.render(error)
99
[error.status, headers, [MultiJson.encode(data)]]
1010
end
1111

12-
def initialize(message, id)
13-
@id = id
14-
super(message)
12+
def initialize(options={})
13+
@id = options[:id]
14+
super(options[:message])
1515
end
1616
end
1717

1818
class HTTPStatusError < Error
1919
attr_accessor :status
2020

21-
def initialize(message = nil, id = nil, status = nil)
22-
meta = Pliny::Errors::META[self.class]
23-
message = message || meta[1] + "."
24-
id = id || meta[1].downcase.tr(' ', '_').to_sym
25-
@status = status || meta[0]
26-
super(message, id)
21+
def initialize(options={})
22+
meta = Pliny::Errors::META[self.class]
23+
options[:message] ||= "#{meta[1]}."
24+
options[:id] ||= meta[1].downcase.tr(' ', '_').to_sym
25+
@status = options[:status] || meta[0]
26+
super(options)
2727
end
2828
end
2929

@@ -108,6 +108,7 @@ class GatewayTimeout < HTTPStatusError; end # 504
108108
UnprocessableEntity => [422, 'Unprocessable entity'],
109109
TooManyRequests => [429, 'Too many requests'],
110110
InternalServerError => [500, 'Internal server error'],
111+
HTTPStatusError => [500, 'Unspecified'],
111112
NotImplemented => [501, 'Not implemented'],
112113
BadGateway => [502, 'Bad gateway'],
113114
ServiceUnavailable => [503, 'Service unavailable'],

spec/errors_spec.rb

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
require "spec_helper"
22

33
describe Pliny::Errors do
4-
it "includes a general error that requires an identifier" do
5-
e = Pliny::Errors::Error.new("General error.", :general_error)
6-
assert_equal "General error.", e.message
7-
assert_equal :general_error, e.id
4+
describe Pliny::Errors::Error do
5+
it "takes a message" do
6+
e = Pliny::Errors::Error.new(message: "Fail.")
7+
assert_equal "Fail.", e.message
8+
end
9+
it "takes an identifier" do
10+
e = Pliny::Errors::Error.new(id: :fail)
11+
assert_equal :fail, e.id
12+
end
813
end
914

10-
it "includes an HTTP error that will take generic parameters" do
11-
e = Pliny::Errors::HTTPStatusError.new(
12-
"Custom HTTP error.", :custom_http_error, 499)
13-
assert_equal "Custom HTTP error.", e.message
14-
assert_equal :custom_http_error, e.id
15-
assert_equal 499, e.status
16-
end
15+
describe Pliny::Errors::HTTPStatusError do
16+
it "includes an HTTP error that will take generic parameters" do
17+
e = Pliny::Errors::HTTPStatusError.new(status: 499)
18+
assert_equal 499, e.status
19+
end
1720

18-
it "includes pre-defined HTTP error templates" do
19-
e = Pliny::Errors::NotFound.new
20-
assert_equal "Not found.", e.message
21-
assert_equal :not_found, e.id
22-
assert_equal 404, e.status
21+
it "includes pre-defined HTTP error templates" do
22+
e = Pliny::Errors::NotFound.new
23+
assert_equal "Not found.", e.message
24+
assert_equal :not_found, e.id
25+
assert_equal 404, e.status
26+
end
2327
end
2428
end

0 commit comments

Comments
 (0)