I have an under
statement that may generate and error (for instance, authentication error). I use content negotiation all over, and I'd like to return the error inside the under
in a proper format. An example code:
under sub {
my $self = shift;
# Authenticated
my $token = $self->param('token') || '';
return 1 if $token eq '123456';
# Not authenticated
$self->respond_to(
json => {
json => { error => 'Invalid authentication token.' },
status => 401
},
text => {
text => 'Unauthorized.',
status => 401
}
);
return undef;
}
I can use render
inside under
, but respond_to
won't work. Probably under
work for that. But in that case, what should I do?