diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index bf1ffe8992049ea816dcb5904b3f64d56ecd2ed1..5729a98b994fb19194f9aad98750ca0946bc8c93 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,7 @@ +* Fix behavior of JSON encoding for `Exception`. + + *namusyaka* + * Make `number_to_phone` format number with regexp pattern. number_to_phone(18812345678, pattern: /(\d{3})(\d{4})(\d{4})/) diff --git a/activesupport/lib/active_support/core_ext/object/json.rb b/activesupport/lib/active_support/core_ext/object/json.rb index 0db787010c81a80b2e6eeafb78b270ca0001fca4..d49b2fbe546e5a490c38c6e47b1f9cbbc18033bd 100644 --- a/activesupport/lib/active_support/core_ext/object/json.rb +++ b/activesupport/lib/active_support/core_ext/object/json.rb @@ -197,3 +197,9 @@ def as_json(options = nil) { :exitstatus => exitstatus, :pid => pid } end end + +class Exception + def as_json(options = nil) + to_s + end +end diff --git a/activesupport/test/json/encoding_test.rb b/activesupport/test/json/encoding_test.rb index 9f4b62fd8bea3c4cb4737ab51621d6a2b93139b9..5fc2e1633630a61e4c1d8bc859753e5d6253c5c2 100644 --- a/activesupport/test/json/encoding_test.rb +++ b/activesupport/test/json/encoding_test.rb @@ -422,6 +422,11 @@ def test_twz_to_json_when_wrapping_a_date_time assert_equal '"1999-12-31T19:00:00.000-05:00"', ActiveSupport::JSON.encode(time) end + def test_exception_to_json + exception = Exception.new("foo") + assert_equal '"foo"', ActiveSupport::JSON.encode(exception) + end + protected def object_keys(json_object)