Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Capture HTTP Error in Perl

by suzun30 (Acolyte)
on Mar 26, 2014 at 17:56 UTC ( #1079845=perlquestion: print w/replies, xml ) Need Help??
suzun30 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Is there a way to capture the HTTP Error and display a customized error message at the client side. Appreciate any feedback/pointers. Thanks,

Replies are listed 'Best First'.
Re: Capture HTTP Error in Perl
by kcott (Chancellor) on Mar 26, 2014 at 18:07 UTC


      Appreciate your kind response. I had initially posted a detailed post with my issue, I did not get much response, hence posted this, I have a web page returning the 'Page cannot be displayed ' error with no error code on the server (IIS 6). The error occurs randomly with no pattern. The page I am trying to print contains a link to a excel file available for download to the user. use strict; use CGI qw(:standard); use CGI::Carp('fatalsToBrowser'); use Spreadsheet::ParseExcel::SaveParser; print header(); print start_multipart_form(); if(param('Submit')){ if(param('file_upl')){ Display_File(); } }else{ Display_Main(); } sub Display_Main(){ print "Please upload file for processing"; print filefield(-name=>"file_upl",-accept=>'application/ +l') ; print submit(-name=>"Submit"); } sub Display_File{ my $fh = upload('upload_file'); if(!$fh){ print "file too big". $cgi_error(); exit 0; }else{ open (OUTFILE, ">> $file_path") or die("Can't open/create $file_n +ame".$!); binmode OUTFILE; while(<$fh>){ print OUTFILE; } close OUTFILE; } my $out_file = Read_Write_excel(); #Saves file in a folder on serve +r print "Thank you. Dowload"; print "<a href=file://///server/share/dir/$out_file>here</a>"; } sub Read_Write_excel{ my $out_file = 'C:\OUT_File.xls' my $parser = new Spreadsheet::ParseExcel::SaveParser; my $Book = $parser->Parse($file_path); my $Sheet = $Book->worksheet('Data'); my ($first_row,$last_row) = $Sheet->row_range(); my $key_col = 0; my $id; for(my $row=1;$row<=$last_row;$row++){ $id = $Sheet->get_cell($row,$key_col)->value(); if($id){ $col = $col++; $Sheet->AddCell($row,$col,'Id found'); } else{ $col = $col++; $Sheet->AddCell($row,$col,'Id Not found'); } } $Book->SaveAs($out_file); return $out_file;

      Debugging Options tried out: 1. Checked IIS log files for HTTP Error code or error messages. Nothing found. 2. Used fiddler to get HTTP Response code in this case, and still only saw 200 there even when the page broke. 3.Tried using log Parser on IIS Server side to catch http code. Nothing there too.

      Appreciate any pointers.

        I've never used IIS, so I can't be a lot of help with that. Having said that, I suspect (from "only saw 200 there even when the page broke") that particular 200 refers to successfully rendering the page which contained little more than '<p>Page cannot be displayed</p>': lines in the log just prior to that may be more informative.

        The message "Page cannot be displayed" is intended for general users, not developers. Use whatever development tools are provided by your browser to get a more informative message.

        I'd recommend adding 'use warnings;' to all your scripts (unless you have a very good reason not to). While you're experiencing difficulties in the development phase, you might want to make that 'use warnings FATAL => 'all';'.

        Try cutting your CGI script down to the bare bones (i.e. no spreadsheet processing or file uploads) and ensuring that works. Then try uploading a single, existing file from a known location and sort out any bugs there. Once all that's working, add the code to generate the upload file and continue testing. Doing this, in a stepwise progression from simple to complex, will mean basic problems will be found early on and you'll be testing new code at each stage rather than trying to test everything at once.

        Have you looked in the CGI documentation? Many of its sections look like they may be useful in this specific instance, including (but not limited to): DEBUGGING, CREATING A FILE UPLOAD FIELD and PROCESSING A FILE UPLOAD FIELD.

        The following threads (on this site) are a little old but may still provide useful information: Troubleshooting Perl CGI scripts and CGI Help Guide.

        -- Ken

Re: Capture HTTP Error in Perl
by hippo (Canon) on Mar 26, 2014 at 18:08 UTC

    There certainly is. Have a look at HTTP::Response, in particular the is_error and code methods.


      Thanks for responding. If my understanding is right, I think I will be able to set the HTTP error code to a customized message . But I do not have the HTTP error code in the first place. I am looking at ways to first capture this. I tried the LWP:UserAgent, with the following code

      my $response = $ua->get('http://servername:84/dir/'); if($response->is_success){ }else{ die $response->status_line; }

      This returns me 401 - Not authorized error. I am using Win32::NetAdmin to authrize my user with the user login from env variable. How do I bypass this with the UserAgent module?

      Second question is, I get the error when I submit the page, how do I simulate this using the LWP::UserAgent ? My site has several perl scripts on the navigation menu which of which mine is one. I have given my specific .pl file name in the url. But it does not seem to authenticate it.

      Genuinely appreciate any feedback.

        The HTTP response code is just a number. It will be in the range 100 to 599 as given in the official IANA list. If you use LWP::UserAgent you can retrieve this number with the code method. If you should need to set the code (and I don't see why you should from what you've said so far) set it just to one of these integers and not to a message.

        I am using Win32::NetAdmin to authrize my user with the user login from env variable. How do I bypass this with the UserAgent module?

        I'm entirely unfamiliar with that module, and indeed with every module under Win32::* - you are on your own there. If it uses HTTP Basic Auth or if you supplant it with HTTP Basic Auth, then you could use the credentials method of LWP::UserAgent to authenticate.

Re: Capture HTTP Error in Perl
by Anonymous Monk on Mar 26, 2014 at 20:04 UTC
    You need to give us a full description of what you are doing and how, because there is obviously more going on here than your one-line OP suggests.

      I have posted my code in the thread above. My page returns successfully 90% of the time. In some random cases even though it creates the output file and reaches the point where i am printing the download link in the html(checked by printing to the log file), it gives a "Internet Explorer cannot display the webpage " error. I do not get any error code on the IIS 6 server log. I tried using fiddler to capture any http error code and got nothing even when the page returned the error. Kindly let me know anything i am missing.

      Thanks and appreciate your time

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1079845]
Approved by kcott
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2018-06-24 11:18 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.