ruby-grape / ruby-grape/grape

using `coerce_with:` lambda without root `Exception` difficult to *debug* unless we help Grape `coerce_value`

Open
#2,194 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
10k
Forks
1.2k
Avg merge
14h 38m
Merged PRs (30d)
92

Description

in method Grape::Validations::CoerceValidator#coerce_value we loose the root cause of Exception leading to a Types::InvalidValue

Consequence is that the dry-validate errors is somewhat not very helpful

example
I got a params validator that read

coerce_with: ->(val) {
                   (val.nil? || val.empty?) ? nil : Types::PrimaryKey.(val) },

which was obvious to debug after changing

def coerce_value(val)
        converter.call(val)
      # Some custom types might fail, so it should be treated as an invalid value
      rescue StandardError
        Types::InvalidValue.new
      end

to

def coerce_value(val)
        converter.call(val)
      # Some custom types might fail, so it should be treated as an invalid value
      rescue StandardError => e
        Types::InvalidValue.new(e.message)
      end

as indeed Integer#empty? does not exists.

Do you think its reasonable to add this for calling Types::InvalidValue initialiser ?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Locate Grape::Validations::CoerceValidator#coerce_value and inspect how Types::InvalidValue is initialized. Reproduce the shown coerce_with lambda failure, then verify that the resulting validation error preserves the original exception message without changing successful coercion behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.