redirect errors to browser in ruby + cgi

Discussion in 'Ruby' started by anne001, Apr 23, 2006.

  1. anne001

    anne001 Guest

    "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?
    anne001, Apr 23, 2006
    #1
    1. Advertising

  2. anne001

    anne001 Guest

    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?
    anne001, May 1, 2006
    #2
    1. Advertising

  3. anne001

    anne001 Guest

    in python I found this
    import cgi, sys

    sys.stderr = sys.stdout

    is there something equivalent for ruby?
    anne001, May 1, 2006
    #3
  4. anne001

    ts Guest

    >>>>> "a" == anne001 <> writes:

    a> is there something equivalent for ruby?

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

    --

    Guy Decoux
    ts, May 1, 2006
    #4
  5. anne001

    anne001 Guest

    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 } }
    }
    }
    anne001, May 1, 2006
    #5
  6. anne001

    anne001 Guest

    PS
    I tried
    $stdout.sync = true
    $stderr.reopen($stdout)

    same thing
    anne001, May 1, 2006
    #6
  7. anne001

    ts Guest

    >>>>> "a" == anne001 <> writes:

    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




    --

    Guy Decoux
    ts, May 1, 2006
    #7
  8. anne001

    anne001 Guest

    Gorgeous, it works, thank you so much!!!
    anne001, May 1, 2006
    #8
  9. anne001

    anne001 Guest

    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>
    anne001, May 1, 2006
    #9
  10. anne001

    anne001 Guest

    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>
    anne001, May 2, 2006
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shailan
    Replies:
    2
    Views:
    866
    Shailan
    Dec 15, 2003
  2. Mark Goldin

    Errors, errors, errors

    Mark Goldin, Jan 17, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    920
    Mark Goldin
    Jan 17, 2004
  3. Sal
    Replies:
    1
    Views:
    374
  4. Professor Chen
    Replies:
    0
    Views:
    141
    Professor Chen
    Feb 6, 2006
  5. Patrick Hartman

    CGI redirect method opens new browser window

    Patrick Hartman, Mar 29, 2010, in forum: Perl Misc
    Replies:
    1
    Views:
    157
    Patrick Hartman
    Mar 29, 2010
Loading...

Share This Page