* [PATCH 4/8] distinguish error messages intended for users vs developers
2016-02-29 1:40 [PATCH 0/8] another round of HTTP-related fixes Eric Wong
` (2 preceding siblings ...)
2016-02-29 1:41 ` [PATCH 3/8] http: avoid needless time2str calls Eric Wong
@ 2016-02-29 1:41 ` Eric Wong
2016-02-29 1:41 ` [PATCH 5/8] favor procedural calls for most private functions Eric Wong
` (3 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Eric Wong @ 2016-02-29 1:41 UTC (permalink / raw)
To: meta
For error messages intended to show user error (e.g. giving
invalid options), we add a newline ("\n") at the end to
polluting the output with location information.
However, for diagnosing non-user-triggered errors, we should
show the location of where the error occured.
---
lib/PublicInbox/Config.pm | 6 +++---
lib/PublicInbox/Daemon.pm | 18 ++++++++----------
lib/PublicInbox/NNTP.pm | 2 +-
lib/PublicInbox/WWW.pm | 2 +-
4 files changed, 13 insertions(+), 15 deletions(-)
diff --git a/lib/PublicInbox/Config.pm b/lib/PublicInbox/Config.pm
index b511638..f84a955 100644
--- a/lib/PublicInbox/Config.pm
+++ b/lib/PublicInbox/Config.pm
@@ -73,7 +73,7 @@ sub git_config_dump {
my @cmd = (qw/git config/, "--file=$file", '-l');
my $cmd = join(' ', @cmd);
my $pid = open(my $fh, '-|', @cmd);
- defined $pid or die "$cmd failed: $!\n";
+ defined $pid or die "$cmd failed: $!";
my %rv;
foreach my $line (<$fh>) {
chomp $line;
@@ -90,8 +90,8 @@ sub git_config_dump {
$rv{$k} = $v;
}
}
- close $fh or die "failed to close ($cmd) pipe: $!\n";
- $? and warn "$$ $cmd exited with: ($pid) $?\n";
+ close $fh or die "failed to close ($cmd) pipe: $!";
+ $? and warn "$$ $cmd exited with: ($pid) $?";
\%rv;
}
diff --git a/lib/PublicInbox/Daemon.pm b/lib/PublicInbox/Daemon.pm
index b8482d3..45c1563 100644
--- a/lib/PublicInbox/Daemon.pm
+++ b/lib/PublicInbox/Daemon.pm
@@ -67,12 +67,12 @@ sub daemon_prepare ($) {
warn "error binding $l: $!\n";
}
}
- die 'No listeners bound' unless @listeners;
+ die "No listeners bound\n" unless @listeners;
}
sub daemonize () {
- chdir '/' or die "chdir failed: $!\n";
- open(STDIN, '+<', '/dev/null') or die "redirect stdin failed: $!\n";
+ chdir '/' or die "chdir failed: $!";
+ open(STDIN, '+<', '/dev/null') or die "redirect stdin failed: $!";
return unless (defined $pid_file || defined $group || defined $user
|| $daemonize);
@@ -238,12 +238,10 @@ sub do_fork () {
my $new = POSIX::SigSet->new;
$new->fillset;
my $old = POSIX::SigSet->new;
- POSIX::sigprocmask(&POSIX::SIG_BLOCK, $new, $old) or
- die "SIG_BLOCK: $!\n";
+ POSIX::sigprocmask(&POSIX::SIG_BLOCK, $new, $old) or die "SIG_BLOCK: $!";
my $pid = fork;
my $err = $!;
- POSIX::sigprocmask(&POSIX::SIG_SETMASK, $old) or
- die "SIG_SETMASK: $!\n";
+ POSIX::sigprocmask(&POSIX::SIG_SETMASK, $old) or die "SIG_SETMASK: $!";
($pid, $err);
}
@@ -254,7 +252,7 @@ sub upgrade_aborted ($) {
return unless $pid_file;
my $file = $pid_file;
- $file =~ s/\.oldbin\z// or die "BUG: no '.oldbin' suffix in $file\n";
+ $file =~ s/\.oldbin\z// or die "BUG: no '.oldbin' suffix in $file";
unlink_pid_file_safe_ish($$, $pid_file);
$pid_file = $file;
eval { write_pid($pid_file) };
@@ -289,8 +287,8 @@ sub unlink_pid_file_safe_ish ($$) {
}
sub master_loop {
- pipe(my ($p0, $p1)) or die "failed to create parent-pipe: $!\n";
- pipe(my ($r, $w)) or die "failed to create self-pipe: $!\n";
+ pipe(my ($p0, $p1)) or die "failed to create parent-pipe: $!";
+ pipe(my ($r, $w)) or die "failed to create self-pipe: $!";
IO::Handle::blocking($w, 0);
my $set_workers = $worker_processes;
my @caught;
diff --git a/lib/PublicInbox/NNTP.pm b/lib/PublicInbox/NNTP.pm
index bcce770..fbf8f7f 100644
--- a/lib/PublicInbox/NNTP.pm
+++ b/lib/PublicInbox/NNTP.pm
@@ -229,7 +229,7 @@ sub parse_time ($$;$) {
use Time::Local qw();
my ($hh, $mm, $ss) = unpack('A2A2A2', $time);
if (defined $gmt) {
- $gmt =~ /\A(?:UTC|GMT)\z/i or die "GM invalid: $gmt\n";
+ $gmt =~ /\A(?:UTC|GMT)\z/i or die "GM invalid: $gmt";
$gmt = 1;
}
my @now = $gmt ? gmtime : localtime;
diff --git a/lib/PublicInbox/WWW.pm b/lib/PublicInbox/WWW.pm
index 2da819b..98e33fd 100644
--- a/lib/PublicInbox/WWW.pm
+++ b/lib/PublicInbox/WWW.pm
@@ -244,7 +244,7 @@ sub get_thread {
sub ctx_get {
my ($ctx, $key) = @_;
my $val = $ctx->{$key};
- (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable\n";
+ (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable";
$val;
}
--
EW
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 5/8] favor procedural calls for most private functions
2016-02-29 1:40 [PATCH 0/8] another round of HTTP-related fixes Eric Wong
` (3 preceding siblings ...)
2016-02-29 1:41 ` [PATCH 4/8] distinguish error messages intended for users vs developers Eric Wong
@ 2016-02-29 1:41 ` Eric Wong
2016-02-29 1:41 ` [PATCH 6/8] git-http-backend: stricter parsing of CRLF Eric Wong
` (2 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Eric Wong @ 2016-02-29 1:41 UTC (permalink / raw)
To: meta
This makes for better compile-time checking and also helps
document which calls are private for HTTP and NNTP.
While we're at it, use IO::Handle::* functions procedurally,
too, since we know we're working with native glob handles.
---
lib/PublicInbox/HTTP.pm | 22 +++++++++++-----------
lib/PublicInbox/NNTP.pm | 28 ++++++++++++++--------------
lib/PublicInbox/Spawn.pm | 3 ++-
script/public-inbox-httpd | 2 +-
4 files changed, 28 insertions(+), 27 deletions(-)
diff --git a/lib/PublicInbox/HTTP.pm b/lib/PublicInbox/HTTP.pm
index 14971f4..aaae7ab 100644
--- a/lib/PublicInbox/HTTP.pm
+++ b/lib/PublicInbox/HTTP.pm
@@ -68,7 +68,7 @@ sub rbuf_process {
# We do not support Trailers in chunked requests, for now
# (they are rarely-used and git (as of 2.7.2) does not use them)
- return $self->quit(400) if $r == -1 || $env{HTTP_TRAILER};
+ return quit($self, 400) if $r == -1 || $env{HTTP_TRAILER};
return $self->watch_read(1) if $r < 0; # incomplete
$self->{rbuf} = substr($self->{rbuf}, $r);
my $len = input_prepare($self, \%env);
@@ -90,7 +90,7 @@ sub event_read_input ($) {
while ($len > 0) {
if ($$rbuf ne '') {
my $w = write_in_full($input, $rbuf, $len);
- return $self->write_err unless $w;
+ return write_err($self) unless $w;
$len -= $w;
die "BUG: $len < 0 (w=$w)" if $len < 0;
if ($len == 0) { # next request may be pipelined
@@ -100,7 +100,7 @@ sub event_read_input ($) {
$$rbuf = '';
}
my $r = sysread($sock, $$rbuf, 8192);
- return $self->recv_err($r, $len) unless $r;
+ return recv_err($self, $r, $len) unless $r;
# continue looping if $r > 0;
}
app_dispatch($self);
@@ -238,7 +238,7 @@ sub write_err {
my $err = $self->{env}->{'psgi.errors'};
my $msg = $! || '(zero write)';
$err->print("error buffering to input: $msg\n");
- $self->quit(500);
+ quit($self, 500);
}
sub recv_err {
@@ -250,7 +250,7 @@ sub recv_err {
}
my $err = $self->{env}->{'psgi.errors'};
$err->print("error reading for input: $! ($len bytes remaining)\n");
- $self->quit(500);
+ quit($self, 500);
}
sub write_in_full {
@@ -278,20 +278,20 @@ sub event_read_input_chunked { # unlikely...
while (1) { # chunk start
if ($len == CHUNK_ZEND) {
return app_dispatch($self) if $$rbuf =~ s/\A\r\n//s;
- return $self->quit(400) if length($$rbuf) > 2;
+ return quit($self, 400) if length($$rbuf) > 2;
}
if ($len == CHUNK_END) {
if ($$rbuf =~ s/\A\r\n//s) {
$len = CHUNK_START;
} elsif (length($$rbuf) > 2) {
- return $self->quit(400);
+ return quit($self, 400);
}
}
if ($len == CHUNK_START) {
if ($$rbuf =~ s/\A([a-f0-9]+).*?\r\n//i) {
$len = hex $1;
} elsif (length($$rbuf) > CHUNK_MAX_HDR) {
- return $self->quit(400);
+ return quit($self, 400);
}
# will break from loop since $len >= 0
}
@@ -299,7 +299,7 @@ sub event_read_input_chunked { # unlikely...
if ($len < 0) { # chunk header is trickled, read more
my $off = length($$rbuf);
my $r = sysread($sock, $$rbuf, 8192, $off);
- return $self->recv_err($r, $len) unless $r;
+ return recv_err($self, $r, $len) unless $r;
# (implicit) goto chunk_start if $r > 0;
}
$len = CHUNK_ZEND if $len == 0;
@@ -308,7 +308,7 @@ sub event_read_input_chunked { # unlikely...
until ($len <= 0) {
if ($$rbuf ne '') {
my $w = write_in_full($input, $rbuf, $len);
- return $self->write_err unless $w;
+ return write_err($self) unless $w;
$len -= $w;
if ($len == 0) {
# we may have leftover data to parse
@@ -324,7 +324,7 @@ sub event_read_input_chunked { # unlikely...
if ($$rbuf eq '') {
# read more of current chunk
my $r = sysread($sock, $$rbuf, 8192);
- return $self->recv_err($r, $len) unless $r;
+ return recv_err($self, $r, $len) unless $r;
}
}
}
diff --git a/lib/PublicInbox/NNTP.pm b/lib/PublicInbox/NNTP.pm
index fbf8f7f..8740377 100644
--- a/lib/PublicInbox/NNTP.pm
+++ b/lib/PublicInbox/NNTP.pm
@@ -212,7 +212,7 @@ sub cmd_listgroup ($;$) {
}
$self->{ng} or return '412 no newsgroup selected';
- $self->long_response(0, long_response_limit, sub {
+ long_response($self, 0, long_response_limit, sub {
my ($i) = @_;
my $nr = $self->{ng}->mm->id_batch($$i, sub {
my ($ary) = @_;
@@ -322,7 +322,7 @@ sub cmd_newnews ($$$$;$$) {
$ts .= '..';
my $opts = { asc => 1, limit => 1000, offset => 0 };
- $self->long_response(0, long_response_limit, sub {
+ long_response($self, 0, long_response_limit, sub {
my ($i) = @_;
my $srch = $srch[0];
my $res = $srch->query($ts, $opts);
@@ -462,7 +462,7 @@ sub set_art {
sub cmd_article ($;$) {
my ($self, $art) = @_;
- my $r = $self->art_lookup($art, 1);
+ my $r = art_lookup($self, $art, 1);
return $r unless ref $r;
my ($n, $mid, $s) = @$r;
set_art($self, $art);
@@ -474,7 +474,7 @@ sub cmd_article ($;$) {
sub cmd_head ($;$) {
my ($self, $art) = @_;
- my $r = $self->art_lookup($art, 2);
+ my $r = art_lookup($self, $art, 2);
return $r unless ref $r;
my ($n, $mid, $s) = @$r;
set_art($self, $art);
@@ -485,7 +485,7 @@ sub cmd_head ($;$) {
sub cmd_body ($;$) {
my ($self, $art) = @_;
- my $r = $self->art_lookup($art, 0);
+ my $r = art_lookup($self, $art, 0);
return $r unless ref $r;
my ($n, $mid, $s) = @$r;
set_art($self, $art);
@@ -495,7 +495,7 @@ sub cmd_body ($;$) {
sub cmd_stat ($;$) {
my ($self, $art) = @_;
- my $r = $self->art_lookup($art, 0);
+ my $r = art_lookup($self, $art, 0);
return $r unless ref $r;
my ($n, $mid, undef) = @$r;
set_art($self, $art);
@@ -612,7 +612,7 @@ sub hdr_message_id ($$$) { # optimize XHDR Message-ID [range] for slrnpull.
my $mm = $self->{ng}->mm;
my ($beg, $end) = @$r;
more($self, $xhdr ? r221 : r225);
- $self->long_response($beg, $end, sub {
+ long_response($self, $beg, $end, sub {
my ($i) = @_;
my $mid = $mm->mid_for($$i);
more($self, "$$i <$mid>") if defined $mid;
@@ -655,7 +655,7 @@ sub hdr_xref ($$$) { # optimize XHDR Xref [range] for rtin
my $mm = $ng->mm;
my ($beg, $end) = @$r;
more($self, $xhdr ? r221 : r225);
- $self->long_response($beg, $end, sub {
+ long_response($self, $beg, $end, sub {
my ($i) = @_;
my $mid = $mm->mid_for($$i);
more($self, "$$i ".xref($ng, $$i)) if defined $mid;
@@ -686,7 +686,7 @@ sub hdr_searchmsg ($$$$) {
my ($beg, $end) = @$r;
more($self, $xhdr ? r221 : r225);
my $off = 0;
- $self->long_response($beg, $end, sub {
+ long_response($self, $beg, $end, sub {
my ($i) = @_;
my $res = $srch->query_xover($beg, $end, $off);
my $msgs = $res->{msgs};
@@ -772,7 +772,7 @@ sub cmd_xrover ($;$) {
my $mm = $ng->mm;
my $srch = $ng->search;
more($self, '224 Overview information follows');
- $self->long_response($beg, $end, sub {
+ long_response($self, $beg, $end, sub {
my ($i) = @_;
my $mid = $mm->mid_for($$i) or return;
my $h = search_header_for($srch, $mid, 'references');
@@ -822,7 +822,7 @@ sub cmd_xover ($;$) {
more($self, "224 Overview information follows for $beg to $end");
my $srch = $self->{ng}->search;
my $off = 0;
- $self->long_response($beg, $end, sub {
+ long_response($self, $beg, $end, sub {
my ($i) = @_;
my $res = $srch->query_xover($beg, $end, $off);
my $msgs = $res->{msgs};
@@ -896,7 +896,7 @@ sub do_more ($$) {
$data = substr($data, $n, $dlen - $n);
}
}
- $self->do_write($data);
+ do_write($self, $data);
}
# callbacks for Danga::Socket
@@ -924,7 +924,7 @@ sub event_read {
my $line = $1;
my $t0 = now();
my $fd = $self->{fd};
- $r = eval { $self->process_line($line) };
+ $r = eval { process_line($self, $line) };
my $d = $self->{long_res} ?
" deferred[$fd]" : '';
out($self, "[$fd] %s - %0.6f$d", $line, now() - $t0);
@@ -947,7 +947,7 @@ sub watch_read {
# another long response.
Danga::Socket->AddTimer(0, sub {
if (&Danga::Socket::POLLIN & $self->{event_watch}) {
- $self->event_read;
+ event_read($self);
}
});
}
diff --git a/lib/PublicInbox/Spawn.pm b/lib/PublicInbox/Spawn.pm
index 02c5446..23f303f 100644
--- a/lib/PublicInbox/Spawn.pm
+++ b/lib/PublicInbox/Spawn.pm
@@ -15,6 +15,7 @@ use strict;
use warnings;
use base qw(Exporter);
use Symbol qw(gensym);
+use IO::Handle;
use PublicInbox::ProcessPipe;
our @EXPORT_OK = qw/which spawn popen_rd/;
@@ -168,7 +169,7 @@ sub popen_rd {
pipe(my ($r, $w)) or die "pipe: $!\n";
$opts ||= {};
my $blocking = $opts->{Blocking};
- $r->blocking($blocking) if defined $blocking;
+ IO::Handle::blocking($r, $blocking) if defined $blocking;
$opts->{1} = fileno($w);
my $pid = spawn($cmd, $env, $opts);
return ($r, $pid) if wantarray;
diff --git a/script/public-inbox-httpd b/script/public-inbox-httpd
index 19315bb..f1a5d79 100755
--- a/script/public-inbox-httpd
+++ b/script/public-inbox-httpd
@@ -68,7 +68,7 @@ use fields qw(cb);
sub new {
my ($class, $io, $cb) = @_;
my $self = fields::new($class);
- $io->blocking(0);
+ IO::Handle::blocking($io, 0);
$self->SUPER::new($io);
$self->{cb} = $cb;
$self->watch_read(1);
--
EW
^ permalink raw reply related [flat|nested] 9+ messages in thread