FU: Add "denied" and "notfound" convenience methods

This commit is contained in:
Yorhel 2025-02-27 14:00:28 +01:00
parent b06cc24826
commit 3662931fc2
2 changed files with 16 additions and 6 deletions

14
FU.pm
View file

@ -352,7 +352,7 @@ sub _do_req($c) {
fu->done; fu->done;
} }
} }
fu->error(404); fu->notfound;
} }
1; 1;
}; };
@ -671,6 +671,8 @@ sub formdata {
sub done { die bless [200,'Done',FU::_caller_info], 'FU::err' } sub done { die bless [200,'Done',FU::_caller_info], 'FU::err' }
sub error($,$code,$msg=$code) { die bless [$code,$msg,FU::_caller_info], 'FU::err' } sub error($,$code,$msg=$code) { die bless [$code,$msg,FU::_caller_info], 'FU::err' }
sub denied { fu->error(403) }
sub notfound { fu->error(404) }
sub status($, $code) { $FU::REQ->{status} = $code } sub status($, $code) { $FU::REQ->{status} = $code }
sub set_body($, $data) { sub set_body($, $data) {
@ -1137,7 +1139,7 @@ the following is a valid approach to handle user authentication:
}; };
FU::get '/registered-users-only', sub { FU::get '/registered-users-only', sub {
fu->error(403) if !fu->{user}; fu->denied if !fu->{user};
}; };
In addition to the request information and response generation methods In addition to the request information and response generation methods
@ -1266,6 +1268,14 @@ elsewhere, this ends up in running the appropriate C<FU::on_error> handler.
C<$message> is optional and currently only used for logging. C<$message> is optional and currently only used for logging.
=item fu->denied
Alias for C<< fu->error(403) >>.
=item fu->notfound
Alias for C<< fu->error(404) >>.
=item fu->reset =item fu->reset
Reset the response to an empty state, basically undoing all effects of the Reset the response to an empty state, basically undoing all effects of the

View file

@ -79,7 +79,7 @@ my @tabs = (
tr_ sub { tr_ sub {
td_ $_; td_ $_;
td_ $r->{reshdr}{$_}; td_ $r->{reshdr}{$_};
} for keys $r->{reshdr}->%*; } for sort keys $r->{reshdr}->%*;
}; };
('Response') ('Response')
}, },
@ -308,7 +308,7 @@ sub listing_ {
} }
sub load($id) { sub load($id) {
open my $fn, '<', "$FU::debug_info->{storage}/fu-$id.txt" or fu->error(404); open my $fn, '<', "$FU::debug_info->{storage}/fu-$id.txt" or fu->notfound;
scalar <$fn>; scalar <$fn>;
local $/=undef; local $/=undef;
fu->set_body(scalar <$fn>); fu->set_body(scalar <$fn>);
@ -322,12 +322,12 @@ sub render {
fu->set_body(framework_ collect); fu->set_body(framework_ collect);
} elsif ($q eq 'last') { } elsif ($q eq 'last') {
my $lst = listing; my $lst = listing;
fu->error(404) if !@$lst; fu->notfound if !@$lst;
load $lst->[$#$lst]; load $lst->[$#$lst];
} elsif ($FU::debug_info->{storage} && $q =~ /^[0-9a-f]{22}$/) { } elsif ($FU::debug_info->{storage} && $q =~ /^[0-9a-f]{22}$/) {
load $q; load $q;
} else { } else {
fu->error(404); fu->notfound
} }
} }