redirect errors to browser in ruby + cgi

A

anne001

"With some execution environments, it is be possible to have the error
message and stack backtrace presented to you in the browser window when
the error occurs. If you can configure your server that way, you'll
find that debugging goes much faster"

I have found this site for perl cgi which seems relevant, but can't
find the ruby cgi info
----------->
Q4.4: What are STDERR and STDIN and STDOUT connected to in a PCP?

In a CGI environment, STDERR points to the server error log file. You
can take this to your advantage by outputting debug messages, and then
checking the log file later on.

Both STDIN and STDOUT point to the browser. In actuality, STDIN
actualls points to the server which interprets the client (or
browser's) request and information, and sends that to the script.

In order to catch errors, you can "dupe" STDERR to STDOUT early on in
your script (after outputting the valid HTTP headers):

open (STDERR, ">&STDOUT");
This redirects all of the error messages to STDOUT (i.e the browser).
http://www.perl.com/pub/a/doc/FAQs/cgi/perl-cgi-faq.html
-------------->

$stderr.puts "my error msg:"
puts this string to the /var/log/httpd/error_log
but

$stderr=$stdout
does not seem to redirect errors to the browser. How can I do this in
ruby?

what about "stack backtraces" do they show up in the error_log file?
 
A

anne001

It seems that in perl, there is a cgi fx carp which can redirect errors
to the browser. Anyone knows where to find an example doing this with
ruby?
 
A

anne001

in python I found this
import cgi, sys

sys.stderr = sys.stdout

is there something equivalent for ruby?
 
T

ts

a> is there something equivalent for ruby?

$stdout.sync = true
$stderr.reopen($stdout)
 
A

anne001

I am so disappointed,
$stdout.sync = true
$stderr.reopen($stdout)
does not work for me, the error_log only says
Premature end of script headers:
/Library/WebServer/CGI-Executables/basic/time2.cgi
is all it says. it does not get to the dividebyzero error

do you need a require something? I have tiger's default 1.8.2.
I tried with my darwinport 1.8.4
#!/private/opt/local/bin/ruby
but I had the same error.

Here is the code, it runs without error if b=1, I want b=0 to see if I
can redirect errors
without the new code, it gives me a divide by zero error in error_log
divided by 0 (ZeroDivisionError)

--------------------->
#!/usr/bin/ruby -T
$SAFE = 1

require 'cgi'
$stdout.sync = true
$stderr.reopen $stdout

a=1
b=0

c=a/b
# Create an instance of CGI, with HTML 4 output
cgi = CGI.new("html4")
# Send the following to the CGI object's output
cgi.out {
cgi.html {
# Produce a header
cgi.head { cgi.title { "Hello world!" } } +
# Produce a body
cgi.body {
cgi.p { "The time is: " + Time.now.to_s } +
cgi.p { "dividing by zero: " + c.to_s } }
}
}
 
T

ts

a> require 'cgi'
a> $stdout.sync = true
a> $stderr.reopen $stdout

a> a=1
a> b=0

a> c=a/b

The problem is that, at this point, you've not send the first line :

Content-Type: text/html

The output is send to $stdout, but without this line the web server give
an error

Try it with

$stdout.sync = true
$stderr.reopen $stdout
puts "Content-type: text/html\n\n"


a=1
b=0

c=a/b
 
A

anne001

I tried the redirection is a cgi test program, but I assume $stdout is
general ruby.

so I tried the following eruby code which works fine until I try to
have it
execute the first of the magic lines! $stdout.sync=true.

How does this work with erb or eruby?
-------------------------------->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html>
<title>
<%= "The Eruby .rhtml parser is up and working fine"%>
</title>
</head>
<body bgcolor ="white">
<pre>

<%
$stdout.sync = true
# $stderr.reopen($stdout)
# $stderr.puts "my error msg: about to divide by zero"
%>
<p>
<%
a=1
b=0.5
c= a/b
puts "a = " + a.to_s
puts "b = " + b.to_s
puts "c = a/b = " + c.to_s
%>
</p>

</pre>
</body>
</html>
 
A

anne001

Oups I forgot, "In a CGI environment, STDERR points to the server error
log file.", so CGI is required.

It also occured to me that perhaps the redirection should occur before
the html instructions
But the line $stdout.sync is enough to crash the browser, even in
start of file, even by itself with the require 'cgi'.

<% require 'cgi' %>
<%
$stdout.sync = true
# $stderr.reopen($stdout)
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html>
<title>
<%= "The Eruby .rhtml parser is up and working fine"%>
</title>
</head>
<body bgcolor ="white">
<pre>

<%# $stderr.puts "my error msg: about to divide by zero" %>
<p>
<%
a=1
b=0.5
c= a/b
puts "a = " + a.to_s
puts "b = " + b.to_s
puts "c = a/b = " + c.to_s
%>
</p>

</pre>
</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top