1

buildinfo.pm: Use JSON::XS instead of PP, improve error handling

This commit is contained in:
eleksir
2020-04-12 02:17:09 +03:00
parent 58dbe97064
commit 063a8f5122

View File

@@ -4,7 +4,7 @@ use strict;
use warnings "all"; use warnings "all";
use vars qw/$VERSION/; use vars qw/$VERSION/;
use conf; use conf;
use JSON::PP; use JSON::XS;
use Data::Dumper; use Data::Dumper;
use Exporter qw(import); use Exporter qw(import);
@@ -33,8 +33,20 @@ sub buildinfo($) {
return ($status, $content, $msg); return ($status, $content, $msg);
}; };
my $sep = $/; my $len = (stat($c->{buildinfo}->{$config}))[7];
my $json = <JSON>; my $json;
my $readlen = read(JSON, $json, $len);
unless (defined($readlen)) {
warn "[FATA] Unable to read $c->{buildinfo}->{$config}: $!";
return ('500', $content, "Unable to read $c->{buildinfo}->{$config}: $!\n");
}
if ($len != $readlen)) {
warn "[FATA] Size of $c->{buildinfo}->{$config} $len bytes but actually read $readlen bytes";
return ('500', $content, "Unable to read $c->{buildinfo}->{$config}\n");
}
close JSON; close JSON;
$/ = $sep; $/ = $sep;
@@ -49,7 +61,7 @@ sub buildinfo($) {
return ($status, $content, $msg); return ($status, $content, $msg);
} }
$json = JSON::PP->new->pretty->canonical->indent_length(4)->encode($j->{$package}) or do { $json = JSON::XS->new->pretty->canonical->indent(1)->encode($j->{$package}) or do {
$status = '500'; $status = '500';
$msg = "Unable to encode json.\n"; $msg = "Unable to encode json.\n";
return ($status, $content, $msg); return ($status, $content, $msg);