Skip to content

Commit 87102e8

Browse files
committed
Rescue coerce errors and return an error hash
1 parent 18301ac commit 87102e8

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

lib/sinatra/browse/route.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ def process(params)
4848
next
4949
end
5050

51-
params[name] = pd.coerce(params[name])
51+
begin
52+
params[name] = pd.coerce(params[name])
53+
rescue
54+
return false, pd.build_error_hash(:invalid, params[name])
55+
end
5256

5357
success, error_hash = pd.validate(params)
5458
return false, error_hash unless success

spec/date_time_validation_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@
1717
end
1818
end
1919

20+
context "with an invalid date" do
21+
it "validates the date and returns an error" do
22+
get("features/date_time_validation", string_min: "2019/02/29")
23+
expect(status).to eq 400
24+
end
25+
end
26+
2027
[
2128
[:max, {string_max: '2005-1-1'}],
2229
[:min, {string_min: '2014-2-4'}]

0 commit comments

Comments
 (0)