Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix JSONizing exceptions, spotted by Garland_g[m]++
When running with RAKUDO_EXCEPTIONS_HANDLER=JSON, any Exception that
contains another Exception as one of the attributes, would throw.  And
since we apparently don't have a CATCHer there, would give a very LTA
error message.  Fixed by moving the JSONizing of an Exception to
Rakudo::Internals::JSON, so it can recursively handle itself.  And
adapt the exception handling in Exception accordingly.
  • Loading branch information
lizmat committed Nov 14, 2017
1 parent 0378292 commit 3cba620
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
29 changes: 17 additions & 12 deletions src/core/Exception.pm
Expand Up @@ -2904,19 +2904,24 @@ my class X::CompUnit::UnsatisfiedDependency is Exception {
}

my class Exceptions::JSON {
sub jsonify($ex) {
Rakudo::Internals::JSON.to-json( $ex.^name => Hash.new(
(message => $ex.?message),
$ex.^attributes.grep(*.has_accessor).map: {
with .name.substr(2) -> $attr {
$attr => (
(.defined and not $_ ~~ Real|Positional|Associative)
?? .Str
!! $_ ~~ Exception
?? jsonify($_)
!! $_
) given $ex."$attr"()
}
}
))
}
method process($ex) {
$*ERR.print:
Rakudo::Internals::JSON.to-json( $ex.^name => Hash.new(
(message => $ex.?message),
$ex.^attributes.grep(*.has_accessor).map: {
with .name.substr(2) -> $attr {
$attr => (
(.defined and not $_ ~~ Real|Positional|Associative)
?? .Str !! $_
) given $ex."$attr"()
}
}
));
$*ERR.print: Rakudo::Internals::JSON.to-json($ex);
False # done processing
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/core/Rakudo/Internals/JSON.pm
Expand Up @@ -120,6 +120,20 @@ my class Rakudo::Internals::JSON {
~ '}';
}

multi sub to-json(Exception:D $ex, :$indent = 0; :$first = 0) {
to-json( $ex.^name => Hash.new(
(message => $ex.?message),
$ex.^attributes.grep(*.has_accessor).map: {
with .name.substr(2) -> $attr {
$attr => (
(.defined and not $_ ~~ Real|Positional|Associative)
?? .Str !! $_
) given $ex."$attr"()
}
}
), :$indent, :$first )
}

multi sub to-json(Mu:U $, :$indent = 0, :$first = 0) { 'null' }
multi sub to-json(Mu:D $s, :$indent = 0, :$first = 0) {
die "Can't serialize an object of type " ~ $s.WHAT.perl
Expand Down

0 comments on commit 3cba620

Please sign in to comment.