Skip to content

Commit

Permalink
Rescue coerce errors and return an error hash
Browse files Browse the repository at this point in the history
  • Loading branch information
renatolond committed Jul 30, 2020
1 parent 18301ac commit 000810b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
All notable changes to sinatra-browse will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## Next version

* `Changed` Incoercible params, such as an invalid `DateTime` param, will give back a 400 with reason `invalid` instead of raising an exception.

## [0.6.1] - 2015-07-07

* `Added` Support for Ruby 2.2.2. That means the unit tests are now run on that version. It was probably working already anyway.
Expand Down
6 changes: 5 additions & 1 deletion lib/sinatra/browse/route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ def process(params)
next
end

params[name] = pd.coerce(params[name])
begin
params[name] = pd.coerce(params[name])
rescue
return false, pd.build_error_hash(:invalid, params[name])
end

success, error_hash = pd.validate(params)
return false, error_hash unless success
Expand Down
7 changes: 7 additions & 0 deletions spec/date_time_validation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
end
end

context "with an invalid date" do
it "validates the date and returns an error" do
get("features/date_time_validation", string_min: "2019/02/29")
expect(status).to eq 400
end
end

[
[:max, {string_max: '2005-1-1'}],
[:min, {string_min: '2014-2-4'}]
Expand Down

0 comments on commit 000810b

Please sign in to comment.