[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[EP-tech] Sending out Error Statuses with Error codes



Yeah - when I was looking at something similar, I felt this solution was a bit lacking.
For passing JSON data, it might be OK.

I wanted to emit a complete screen as the response, so did an internal_redirect after outputting the status/header - something like this (either 403 or 401 response):

### in the 'what response to emit' module:
if( defined $user ){
                EPrints::Apache::AnApache::send_status_line( $r, FORBIDDEN, "Forbidden");
                EPrints::Apache::AnApache::send_http_header( $r );
                $r->internal_redirect( '/cgi/acl_40X_handler' );
                $r->status( FORBIDDEN );
                return FORBIDDEN;
} else {
                EPrints::Apache::AnApache::send_status_line( $r, AUTH_REQUIRED, "Unauthorised");
                EPrints::Apache::AnApache::send_http_header( $r );
                $r->internal_redirect( '/cgi/acl_40X_handler' );
                $r->status( AUTH_REQUIRED );
                return 401;
}
###


In a cgi/40Xhandler:

use EPrints;
use strict;

my $repo = EPrints->new->current_repository;
exit( 0 ) unless( defined $repo );

my $r = $repo->get_request;
my $screenid = $repo->param( "screen" );
if( !defined $screenid )
{
        $screenid = "YourDefaultScreen";
}

EPrints::ScreenProcessor->process(
        session => $repo,
        screenid => $screenid
);

Not sure if that is useful in any way - I found it a tricky part of mod_perl2 to get my head around!

Cheers,
John


From: eprints-tech-bounces at ecs.soton.ac.uk [mailto:eprints-tech-bounces at ecs.soton.ac.uk] On Behalf Of Adam Field
Sent: 27 June 2018 12:51
To: eprints-tech at ecs.soton.ac.uk
Subject: Re: [EP-tech] Sending out Error Statuses with Error codes

Thanks, John.  My function looks like this now:

sub api_error
{
        my ($repo, $code, $message) = @_;

        my $r = $repo->request;

        $r->status($code);
        $r->custom_response($code, $message);

        return $code;
}

?and while isn?t quite as fancy as I?d like it, it does the job well enough to integrate with swagger UI (which was the key goal).  I can always make it better later.

--
Adam



From: <eprints-tech-bounces at ecs.soton.ac.uk<mailto:eprints-tech-bounces at ecs.soton.ac.uk>> on behalf of John Salter <J.Salter at leeds.ac.uk<mailto:J.Salter at leeds.ac.uk>>
Reply-To: <eprints-tech at ecs.soton.ac.uk<mailto:eprints-tech at ecs.soton.ac.uk>>
Date: Tuesday, 26 June 2018 17:32
To: "eprints-tech at ecs.soton.ac.uk<mailto:eprints-tech at ecs.soton.ac.uk>" <eprints-tech at ecs.soton.ac.uk<mailto:eprints-tech at ecs.soton.ac.uk>>
Subject: Re: [EP-tech] Sending out Error Statuses with Error codes

Apologies - that was only ? helpful?
This is the other bit that I think you need:
https://perl.apache.org/docs/2.0/api/Apache2/Response.html#C_custom_response_

Cheers,
John

From: John Salter
Sent: 26 June 2018 17:27
To: eprints-tech at ecs.soton.ac.uk<mailto:eprints-tech at ecs.soton.ac.uk>
Subject: RE: Sending out Error Statuses with Error codes

Hi Adam,
You need the 'mod_perl2 User's Guide' by Bekman and Brandt? :o)

Or hopefully this:
https://perl.apache.org/docs/2.0/api/Apache2/RequestRec.html#C_err_headers_out_

Cheers,
John

From: eprints-tech-bounces at ecs.soton.ac.uk<mailto:eprints-tech-bounces at ecs.soton.ac.uk> [mailto:eprints-tech-bounces at ecs.soton.ac.uk] On Behalf Of Adam Field
Sent: 26 June 2018 17:01
To: eprints-tech at ecs.soton.ac.uk<mailto:eprints-tech at ecs.soton.ac.uk>
Subject: [EP-tech] Sending out Error Statuses with Error codes

Hi

                I?m working on an API that spits out objects in JSON, and I have a function that handles errors.  I can?t seem to get it to behave, and I wonder if anyone can offer me any advice.

sub api_error
{
        my ($repo, $code, $message) = @_;

        my $r = $repo->request;

        $r->status($code);
        EPrints::Apache::AnApache::send_status_line( $r, $code );
        $r->content_type('application/json');

        my $json = JSON->new;
        my $content = $json->encode(
        {
                status => $code,
                message => $message
        }
        );

        $r->err_headers_out->{'Content-Length'} = length $content;

#       binmode(STDOUT, ":utf8");
#       print $content;

        return $code;
}

Using CURL to look at the headers and body, I get this:

HTTP/1.1 400 Bad Request
Date: Tue, 26 Jun 2018 15:48:51 GMT
Server: Apache/2.4.7 (Ubuntu)
Content-Length: 120
Connection: close
Content-Type: text/html; charset=iso-8859-1


<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
<hr>
<address>Apache/2.4.7 (Ubuntu) Server at sherpaneo-test.sherpa.ac.uk Port 443</address>
</body></html>

If I uncomment the two commented lines, I get this:

HTTP/1.1 200 OK
Date: Tue, 26 Jun 2018 15:48:36 GMT
Server: Apache/2.4.7 (Ubuntu)
Content-Length: 120
Content-Type: application/json


{"message":"unrecognised api key in 'api-key' at /usr/share/eprints3/lib/plugins/SherpaAPI.pm line 470.\n","status":400}


Essentially, if I output anything to STDOUT, something (maybe MOD Perl) switches the status to ?200 OK?.  How can I get the status code from the first version, and the body from the second?  Any ideas?


Cheers

--
Adam Field
*** Options: http://mailman.ecs.soton.ac.uk/mailman/listinfo/eprints-tech *** Archive: http://www.eprints.org/tech.php/ *** EPrints community wiki: http://wiki.eprints.org/ *** EPrints developers Forum: http://forum.eprints.org/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ecs.soton.ac.uk/pipermail/eprints-tech/attachments/20180627/58818433/attachment-0001.html