Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when rescue Grape::Exceptions::ValidationErrors in json format with backtrance and original_exception #2472

Closed
ericproulx opened this issue Jul 15, 2024 · 4 comments

Comments

@ericproulx
Copy link
Contributor

Following #2471, I found that this case is raising an error.

describe 'rescue Grape::Exceptions::ValidationErrors in json format with backtrace and original_exception' do
  let(:app) do
    Class.new(Grape::API) do
      format :json

      rescue_from Grape::Exceptions::ValidationErrors, backtrace: true, original_exception: true do |e|
        error!(e, 418, {}, e.backtrace, e)
      end

      params do
        requires :beer
      end

      get '/' do
      end
    end
  end

  before { get '/' }

  context 'with json response type format' do
    subject { last_response.status }

    it { is_expected.to eq(418) }
  end
end

Our README shows the same kind of example but without original_exception and backtrace.

Nonetheless, It works for XML and TXT because of the simple ternary condition while JSON is doing something different when Grape::Exceptions::ValidationErrors

If we want to add original_exception and backtrace I think we'll need to enhance Grape::Exceptions::ValidationErrors with a new function to include its when dumping JSON.

@ericproulx
Copy link
Contributor Author

@numbata if you want to take a look

@ericproulx
Copy link
Contributor Author

Unfortunately, since Grape::Exceptions::ValidationErrors generates an array when formatted in JSON, we won't be able to add the properties at the root level. Also, adding them to each element of the array might be to much.

@numbata
Copy link
Contributor

numbata commented Jul 15, 2024

Sure, would be happy to take it for tomorrow, but it feels as if you are already deep into it.

@numbata
Copy link
Contributor

numbata commented Jul 23, 2024

Unfortunately, since Grape::Exceptions::ValidationErrors generates an array when formatted in JSON, we won't be able to add the properties at the root level. Also, adding them to each element of the array might be to much.

Agreed! And what if we could just skip the backtrace and the original exception filling if the message structure is not suitable for it? Something like #2480

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants