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

Fix to_hash called on errors raises an error #168

Merged
merged 3 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/generators.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ jobs:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true

- name: Run generator tests
run: bundle exec rspec --tag type:generator

- name: Set up Node
uses: actions/setup-node@v4
with:
Expand Down
18 changes: 15 additions & 3 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,17 @@ jobs:
strategy:
fail-fast: false
matrix:
ruby: ['3.1', '3.2', '3.3']
rails: ['6.1', '7.0', '7.1', '7.2']
ruby: ['3.0', '3.1', '3.2', '3.3']
rails: ['6.1', '7.0', '7.1', '7.2', '8.0']
exclude:
- ruby: '3.0'
rails: '8.0'
- ruby: '3.1'
rails: '8.0'
- ruby: '3.0'
rails: '7.2'
- ruby: '3.1'
rails: '7.2'

runs-on: ubuntu-latest
name: Test against Ruby ${{ matrix.ruby }} / Rails ${{ matrix.rails }}
Expand All @@ -54,11 +63,14 @@ jobs:
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
# Use the latest version of RubyGems with Ruby 3.0 to avoid:
# https://bugs.ruby-lang.org/issues/19371
rubygems: ${{ startsWith(matrix.ruby-version, '3.0') && 'latest' || 'default' }}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i thought i was going crazy for a second because I was sure I'd already merged something like this in... but that was in #162 that targeted the v2 branch 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup yup I decided it's good to have it in master as well 😄

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea i forgot that you added Ruby 3.0 CI support as part of V2! definitely worth supporting that in master

bundler-cache: true
env:
RAILS_VERSION: ${{ matrix.rails }}

- name: Run tests
run: bundle exec rspec
run: bundle exec rspec --tag ~type:generator
env:
RAILS_VERSION: ${{ matrix.rails }}
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ source 'https://rubygems.org'
# Specify your gem's dependencies in inertia_rails.gemspec
gemspec

version = ENV['RAILS_VERSION'] || '7.2'
version = ENV['RAILS_VERSION'] || '8.0'
gem 'rails', "~> #{version}.0"

gem 'bundler', '~> 2.0'
Expand Down
9 changes: 8 additions & 1 deletion lib/inertia_rails/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,14 @@ def inertia_location(url)

def capture_inertia_errors(options)
if (inertia_errors = options.dig(:inertia, :errors))
session[:inertia_errors] = inertia_errors.to_hash
if inertia_errors.respond_to?(:to_hash)
session[:inertia_errors] = inertia_errors.to_hash
else
InertiaRails.deprecator.warn(
"Object passed to `inertia: { errors: ... }` must respond to `to_hash`. Pass a hash-like object instead."
)
session[:inertia_errors] = inertia_errors
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions spec/dummy/app/controllers/inertia_test_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ def redirect_with_inertia_errors
redirect_to empty_test_path, inertia: { errors: { uh: 'oh' } }
end

def redirect_with_non_hash_inertia_errors
redirect_to empty_test_path, inertia: { errors: 'uh oh' }
end

def redirect_with_inertia_error_object
redirect_to empty_test_path, inertia: { errors: MyError.new }
end
Expand Down
1 change: 1 addition & 0 deletions spec/dummy/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
get 'redirect_with_inertia_errors' => 'inertia_test#redirect_with_inertia_errors'
post 'redirect_with_inertia_errors' => 'inertia_test#redirect_with_inertia_errors'
post 'redirect_with_inertia_error_object' => 'inertia_test#redirect_with_inertia_error_object'
post 'redirect_with_non_hash_inertia_errors' => 'inertia_test#redirect_with_non_hash_inertia_errors'
post 'redirect_back_with_inertia_errors' => 'inertia_test#redirect_back_with_inertia_errors'
post 'redirect_back_or_to_with_inertia_errors' => 'inertia_test#redirect_back_or_to_with_inertia_errors'
get 'error_404' => 'inertia_test#error_404'
Expand Down
12 changes: 12 additions & 0 deletions spec/inertia/error_sharing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@
expect(session[:inertia_errors]).not_to be
end

it 'accepts a non-hash error object' do
expect { post redirect_with_non_hash_inertia_errors_path, headers: headers }
.to output(/Object passed to `inertia: { errors: ... }` must respond to `to_hash`/).to_stderr
expect(response.headers['Location']).to eq(empty_test_url)
expect(session[:inertia_errors]).to eq('uh oh')

# Follow the redirect
get response.headers['Location'], headers: headers
expect(response.body).to include({ errors: 'uh oh' }.to_json)
expect(session[:inertia_errors]).not_to be
end

it 'keeps errors around when the post has a stale version' do
post redirect_with_inertia_errors_path, headers: headers
expect(response.headers['Location']).to eq(empty_test_url)
Expand Down