Repository navigation
Custom types can set a message to be used in the response when invalid - #2157
Conversation
1e739a2 to
3d1760d
Compare
Just return an instance of `Grape::Types::InvalidValue` with the message:
class Color
def self.parse(value)
return value if %w[blue red green].include?(value)
Grape::Types::InvalidValue.new('Invalid color')
end
end
Any raised exception will be treated as an invalid value as it was before.
f8e509c to
54b2e11
Compare
dblock
left a comment
There was a problem hiding this comment.
This is great, but I think we should rely on an exception to separate valid returns from errors in the flow. We could similarly rescue StandardError and convert it into something with more detail like a ParseError that would help the caller identify the actual parameter that could not be parsed. WDYT?
| new(value) | ||
| return new(value) if %w[blue red green]).include?(value) | ||
|
|
||
| Grape::Types::InvalidValue.new('Unsupported color') |
There was a problem hiding this comment.
I feel like this should be a raise that gets handled, not a return.
There was a problem hiding this comment.
How will we know that an exception message should be returned to the user if we rescue StandardError? I would like to avoid guessing 🙂 We already return the invalid value there and there and there.
In general, if there is an easy way to return a value indicating a failure, I prefer to do that. It is cheaper:
require 'benchmark/ips'
def return_value
:invalid
end
def raise_exception
raise StandardError
end
Benchmark.ips do |x|
x.report('return value') do
return_value
end
x.report('raise error') do
begin
raise_exception
rescue StandardError
end
end
x.compare!
endWarming up --------------------------------------
return value 1.990M i/100ms
raise error 149.703k i/100ms
Calculating -------------------------------------
return value 19.948M (± 0.4%) i/s - 101.470M in 5.086841s
raise error 1.513M (± 0.5%) i/s - 7.635M in 5.044907s
Comparison:
return value: 19947800.7 i/s
raise error: 1513417.9 i/s - 13.18x (± 0.00) slower
There was a problem hiding this comment.
Ok, I clearly didn't do my homework, especially on the existing code.
Just return an instance of
Grape::Types::InvalidValuewith the message:Any raised exception will be treated as an invalid value as it was before.
Implements an idea listed in #2093