Skip to content

Commit 91386c1

Browse files
committed
Fix behavior of JSON encoding for Exception
1 parent e88d63e commit 91386c1

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

activesupport/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Fix behavior of JSON encoding for `Exception`.
2+
3+
*namusyaka*
4+
15
* Make `number_to_phone` format number with regexp pattern.
26

37
number_to_phone(18812345678, pattern: /(\d{3})(\d{4})(\d{4})/)

activesupport/lib/active_support/core_ext/object/json.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,9 @@ def as_json(options = nil)
197197
{ :exitstatus => exitstatus, :pid => pid }
198198
end
199199
end
200+
201+
class Exception
202+
def as_json(options = nil)
203+
to_s
204+
end
205+
end

activesupport/test/json/encoding_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,11 @@ def test_twz_to_json_when_wrapping_a_date_time
422422
assert_equal '"1999-12-31T19:00:00.000-05:00"', ActiveSupport::JSON.encode(time)
423423
end
424424

425+
def test_exception_to_json
426+
exception = Exception.new("foo")
427+
assert_equal '"foo"', ActiveSupport::JSON.encode(exception)
428+
end
429+
425430
protected
426431

427432
def object_keys(json_object)

0 commit comments

Comments
 (0)