<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta name=Title content=""><meta name=Keywords content=""><meta http-equiv=Content-Type content="text/html; charset=utf-8"><meta name=Generator content="Microsoft Word 15 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
        {font-family:"Courier New";
        panose-1:2 7 3 9 2 2 5 2 4 4;}
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:Calibri;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:#0563C1;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:#954F72;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal;
        font-family:Calibri;
        color:windowtext;}
span.EmailStyle18
        {mso-style-type:personal;
        font-family:Calibri;
        color:#1F497D;}
span.EmailStyle19
        {mso-style-type:personal;
        font-family:Calibri;
        color:#1F497D;}
span.EmailStyle20
        {mso-style-type:personal-reply;
        font-family:Calibri;
        color:windowtext;}
span.msoIns
        {mso-style-type:export-only;
        mso-style-name:"";
        text-decoration:underline;
        color:teal;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page WordSection1
        {size:595.0pt 842.0pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
        {page:WordSection1;}
--></style></head><body bgcolor=white lang=EN-US link="#0563C1" vlink="#954F72"><div class=WordSection1><p class=MsoNormal><span style='font-size:11.0pt'>Thanks, John. My function looks like this now:<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt'>sub api_error<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt'>{<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt'> my ($repo, $code, $message) = @_;<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt'> my $r = $repo->request;<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt'> $r->status($code);<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt'> $r->custom_response($code, $message);<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt'> return $code;<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt'>}<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt'>…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.<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt'>--<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt'>Adam<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt'><o:p> </o:p></span></p><div style='border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0cm 0cm 0cm'><p class=MsoNormal style='margin-left:36.0pt'><b><span style='color:black'>From: </span></b><span style='color:black'><eprints-tech-bounces@ecs.soton.ac.uk> on behalf of John Salter <J.Salter@leeds.ac.uk><br><b>Reply-To: </b><eprints-tech@ecs.soton.ac.uk><br><b>Date: </b>Tuesday, 26 June 2018 17:32<br><b>To: </b>"eprints-tech@ecs.soton.ac.uk" <eprints-tech@ecs.soton.ac.uk><br><b>Subject: </b>Re: [EP-tech] Sending out Error Statuses with Error codes<o:p></o:p></span></p></div><div><p class=MsoNormal style='margin-left:36.0pt'><span style='font-family:"Times New Roman"'><o:p> </o:p></span></p></div><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:11.0pt;color:#1F497D'>Apologies - that was only ½ helpful…</span><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:11.0pt;color:#1F497D'>This is the other bit that I think you need:</span><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:11.0pt;color:#1F497D'>https://perl.apache.org/docs/2.0/api/Apache2/Response.html#C_custom_response_</span><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:11.0pt;color:#1F497D'> </span><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:11.0pt;color:#1F497D'>Cheers,</span><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:11.0pt;color:#1F497D'>John</span><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:11.0pt;color:#1F497D'> </span><o:p></o:p></p><div><div style='border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0cm 0cm 0cm'><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt'>From:</span></b><span style='font-size:11.0pt'> John Salter <br><b>Sent:</b> 26 June 2018 17:27<br><b>To:</b> eprints-tech@ecs.soton.ac.uk<br><b>Subject:</b> RE: Sending out Error Statuses with Error codes</span><o:p></o:p></p></div></div><p class=MsoNormal style='margin-left:36.0pt'> <o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:11.0pt;color:#1F497D'>Hi Adam,</span><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:11.0pt;color:#1F497D'>You need the 'mod_perl2 User's Guide' by Bekman and Brandt… :o)</span><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:11.0pt;color:#1F497D'> </span><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:11.0pt;color:#1F497D'>Or hopefully this:</span><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:11.0pt;color:#1F497D'><a href="https://perl.apache.org/docs/2.0/api/Apache2/RequestRec.html#C_err_headers_out_">https://perl.apache.org/docs/2.0/api/Apache2/RequestRec.html#C_err_headers_out_</a></span><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:11.0pt;color:#1F497D'> </span><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:11.0pt;color:#1F497D'>Cheers,</span><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:11.0pt;color:#1F497D'>John</span><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:11.0pt;color:#1F497D'> </span><o:p></o:p></p><div><div style='border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0cm 0cm 0cm'><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt'>From:</span></b><span style='font-size:11.0pt'> <a href="mailto:eprints-tech-bounces@ecs.soton.ac.uk">eprints-tech-bounces@ecs.soton.ac.uk</a> [<a href="mailto:eprints-tech-bounces@ecs.soton.ac.uk">mailto:eprints-tech-bounces@ecs.soton.ac.uk</a>] <b>On Behalf Of </b>Adam Field<br><b>Sent:</b> 26 June 2018 17:01<br><b>To:</b> <a href="mailto:eprints-tech@ecs.soton.ac.uk">eprints-tech@ecs.soton.ac.uk</a><br><b>Subject:</b> [EP-tech] Sending out Error Statuses with Error codes</span><o:p></o:p></p></div></div><p class=MsoNormal style='margin-left:36.0pt'> <o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:11.0pt'>Hi</span><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:11.0pt'> </span><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:11.0pt'> 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.</span><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:11.0pt'> </span><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'>sub api_error</span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'>{</span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'> my ($repo, $code, $message) = @_;</span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'> </span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'> my $r = $repo->request;</span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'> </span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'> $r->status($code);</span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'> EPrints::Apache::AnApache::send_status_line( $r, $code );</span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'> $r->content_type('application/json');</span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'> </span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'> my $json = JSON->new;</span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'> my $content = $json->encode(</span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'> {</span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'> status => $code,</span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'> message => $message</span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'> }</span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'> );</span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'> </span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'> $r->err_headers_out->{'Content-Length'} = length $content;</span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'> </span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'># binmode(STDOUT, ":utf8");</span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'># print $content;</span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'> </span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'> return $code;</span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'>}</span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:11.0pt'> </span><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:11.0pt'>Using CURL to look at the headers and body, I get this:</span><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:11.0pt'> </span><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'>HTTP/1.1 400 Bad Request</span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'>Date: Tue, 26 Jun 2018 15:48:51 GMT</span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'>Server: Apache/2.4.7 (Ubuntu)</span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'>Content-Length: 120</span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'>Connection: close</span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'>Content-Type: text/html; charset=iso-8859-1</span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'> </span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'> </span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'><!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"></span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'><html><head></span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'><title>400 Bad Request</title></span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'></head><body></span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'><h1>Bad Request</h1></span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'><p>Your browser sent a request that this server could not understand.<br /></span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'></p></span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'><hr></span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'><address>Apache/2.4.7 (Ubuntu) Server at sherpaneo-test.sherpa.ac.uk Port 443</address></span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'></body></html></span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:11.0pt'> </span><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:11.0pt'>If I uncomment the two commented lines, I get this:</span><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:11.0pt'> </span><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'>HTTP/1.1 200 OK</span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'>Date: Tue, 26 Jun 2018 15:48:36 GMT</span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'>Server: Apache/2.4.7 (Ubuntu)</span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'>Content-Length: 120</span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'>Content-Type: application/json</span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'> </span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'> </span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:11.0pt;font-family:"Courier New"'>{"message":"unrecognised api key in 'api-key' at /usr/share/eprints3/lib/plugins/SherpaAPI.pm line 470.\n","status":400}</span></b><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:11.0pt'> </span><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:11.0pt'> </span><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:11.0pt'>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?</span><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:11.0pt'> </span><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:11.0pt'> </span><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:11.0pt'>Cheers</span><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:11.0pt'> </span><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:11.0pt'>--</span><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:11.0pt'>Adam Field</span><o:p></o:p></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-family:"Times New Roman"'>*** 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/ <o:p></o:p></span></p></div></body></html>