File tree Expand file tree Collapse file tree 4 files changed +25
-1
lines changed Expand file tree Collapse file tree 4 files changed +25
-1
lines changed Original file line number Diff line number Diff line change @@ -157,7 +157,14 @@ def inertia_location(url)
157
157
158
158
def capture_inertia_errors ( options )
159
159
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
161
168
end
162
169
end
163
170
end
Original file line number Diff line number Diff line change @@ -47,6 +47,10 @@ def redirect_with_inertia_errors
47
47
redirect_to empty_test_path , inertia : { errors : { uh : 'oh' } }
48
48
end
49
49
50
+ def redirect_with_non_hash_inertia_errors
51
+ redirect_to empty_test_path , inertia : { errors : 'uh oh' }
52
+ end
53
+
50
54
def redirect_with_inertia_error_object
51
55
redirect_to empty_test_path , inertia : { errors : MyError . new }
52
56
end
Original file line number Diff line number Diff line change 24
24
get 'redirect_with_inertia_errors' => 'inertia_test#redirect_with_inertia_errors'
25
25
post 'redirect_with_inertia_errors' => 'inertia_test#redirect_with_inertia_errors'
26
26
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'
27
28
post 'redirect_back_with_inertia_errors' => 'inertia_test#redirect_back_with_inertia_errors'
28
29
post 'redirect_back_or_to_with_inertia_errors' => 'inertia_test#redirect_back_or_to_with_inertia_errors'
29
30
get 'error_404' => 'inertia_test#error_404'
Original file line number Diff line number Diff line change 17
17
expect ( session [ :inertia_errors ] ) . not_to be
18
18
end
19
19
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
+
20
32
it 'keeps errors around when the post has a stale version' do
21
33
post redirect_with_inertia_errors_path , headers : headers
22
34
expect ( response . headers [ 'Location' ] ) . to eq ( empty_test_url )
You can’t perform that action at this time.
0 commit comments