The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Error::Pure::Output::JSON - Output JSON subroutines for Error::Pure.

SYNOPSIS

 use Error::Pure::Output::JSON qw(err_json);
 print err_json(@errors);

SUBROUTINES

err_json(@errors)
 JSON print of backtrace.
 When is set global variable $PRETTY, print pretty output.
 Returns JSON serialization of backtrace.

VARIABLES

$PRETTY
 JSON pretty output flag. Possible values are 0 or 1.
 Default value is 0.

EXAMPLE1

 use strict;
 use warnings;

 use Error::Pure::Output::JSON qw(err_json);

 # Fictional error structure.
 my $err_hr = {
         'msg' => [
                 'FOO',
                 'KEY',
                 'VALUE',
         ],
         'stack' => [
                 {
                         'args' => '(2)',
                         'class' => 'main',
                         'line' => 1,
                         'prog' => 'script.pl',
                         'sub' => 'err',
                 }, {
                         'args' => '',
                         'class' => 'main',
                         'line' => 20,
                         'prog' => 'script.pl',
                         'sub' => 'eval {...}',
                 }
         ],
 };

 # Print out.
 print err_json($err_hr);

 # Output:
 # {"msg":["FOO","KEY","VALUE"],"stack":[{"sub":"err","prog":"script.pl","args":"(2)","class":"main","line":1},{"sub":"eval {...}","prog":"script.pl","args":"","class":"main","line":20}]}

EXAMPLE2

 use strict;
 use warnings;

 use Error::Pure::Output::JSON qw(err_json);

 # Set pretty output.
 $Error::Pure::Output::JSON::PRETTY = 1;

 # Fictional error structure.
 my $err_hr = {
         'msg' => [
                 'FOO',
                 'KEY',
                 'VALUE',
         ],
         'stack' => [
                 {
                         'args' => '(2)',
                         'class' => 'main',
                         'line' => 1,
                         'prog' => 'script.pl',
                         'sub' => 'err',
                 }, {
                         'args' => '',
                         'class' => 'main',
                         'line' => 20,
                         'prog' => 'script.pl',
                         'sub' => 'eval {...}',
                 }
         ],
 };

 # Print out.
 print err_json($err_hr);

 # Output:
 # {
 #    "msg" : [
 #       "FOO",
 #       "KEY",
 #       "VALUE"
 #    ],
 #    "stack" : [
 #       {
 #          "sub" : "err",
 #          "prog" : "script.pl",
 #          "args" : "(2)",
 #          "class" : "main",
 #          "line" : 1
 #       },
 #       {
 #          "sub" : "eval {...}",
 #          "prog" : "script.pl",
 #          "args" : "",
 #          "class" : "main",
 #          "line" : 20
 #       }
 #    ]
 # }

DEPENDENCIES

Exporter, JSON, Readonly.

SEE ALSO

Task::Error::Pure

Install the Error::Pure modules.

Error::Pure::Output::Text

Output subroutines for Error::Pure.

REPOSITORY

https://github.com/michal-josef-spacek/Error-Pure-Output-JSON

AUTHOR

Michal Josef Špaček mailto:skim@cpan.org

http://skim.cz

LICENSE AND COPYRIGHT

© Michal Josef Špaček 2013-2023

BSD 2-Clause License

VERSION

0.11