Skip to content

Commit 3a3cf8e

Browse files
committed
Fix to_hash called on errors raises an error
1 parent 8170a37 commit 3a3cf8e

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

lib/inertia_rails/controller.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,14 @@ def inertia_location(url)
157157

158158
def capture_inertia_errors(options)
159159
if (inertia_errors = options.dig(:inertia, :errors))
160-
session[:inertia_errors] = inertia_errors.to_hash
160+
if inertia_errors.respond_to?(:to_hash)
161+
session[:inertia_errors] = inertia_errors.to_hash
162+
else
163+
InertiaRails.deprecator.warn(
164+
"Object passed to `inertia: { errors: ... }` must respond to `to_hash`. Pass a hash-like object instead."
165+
)
166+
session[:inertia_errors] = inertia_errors
167+
end
161168
end
162169
end
163170
end

spec/dummy/app/controllers/inertia_test_controller.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ def redirect_with_inertia_errors
4747
redirect_to empty_test_path, inertia: { errors: { uh: 'oh' } }
4848
end
4949

50+
def redirect_with_non_hash_inertia_errors
51+
redirect_to empty_test_path, inertia: { errors: 'uh oh' }
52+
end
53+
5054
def redirect_with_inertia_error_object
5155
redirect_to empty_test_path, inertia: { errors: MyError.new }
5256
end

spec/dummy/config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
get 'redirect_with_inertia_errors' => 'inertia_test#redirect_with_inertia_errors'
2525
post 'redirect_with_inertia_errors' => 'inertia_test#redirect_with_inertia_errors'
2626
post 'redirect_with_inertia_error_object' => 'inertia_test#redirect_with_inertia_error_object'
27+
post 'redirect_with_non_hash_inertia_errors' => 'inertia_test#redirect_with_non_hash_inertia_errors'
2728
post 'redirect_back_with_inertia_errors' => 'inertia_test#redirect_back_with_inertia_errors'
2829
post 'redirect_back_or_to_with_inertia_errors' => 'inertia_test#redirect_back_or_to_with_inertia_errors'
2930
get 'error_404' => 'inertia_test#error_404'

spec/inertia/error_sharing_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@
1717
expect(session[:inertia_errors]).not_to be
1818
end
1919

20+
it 'accepts a non-hash error object' do
21+
expect { post redirect_with_non_hash_inertia_errors_path, headers: headers }
22+
.to output(/Object passed to `inertia: { errors: ... }` must respond to `to_hash`/).to_stderr
23+
expect(response.headers['Location']).to eq(empty_test_url)
24+
expect(session[:inertia_errors]).to eq('uh oh')
25+
26+
# Follow the redirect
27+
get response.headers['Location'], headers: headers
28+
expect(response.body).to include({ errors: 'uh oh' }.to_json)
29+
expect(session[:inertia_errors]).not_to be
30+
end
31+
2032
it 'keeps errors around when the post has a stale version' do
2133
post redirect_with_inertia_errors_path, headers: headers
2234
expect(response.headers['Location']).to eq(empty_test_url)

0 commit comments

Comments
 (0)