trapping compile time errors

  • Thread starter Gerard A.W. Vreeswijk
  • Start date
G

Gerard A.W. Vreeswijk

Hi,

I want to catch compile time errors with my own method.

In Perl, you can do this:

$SIG{__DIE__} = \¨

sub die {
print "Error here. May be any error, including SyntaxError\n";
exit 1;
}

I.e., you can trap die and define your own. Does anyone know how to do
this in Ruby? I have tried opening up Exception class and redefining
some of the methods there. No success. Eventually,
I came up with the following, which is not as I imagined. It's ugly.
I am sure there exist better ways but currently I ran out of ideas.
Does anyone have better ideas? Thanks.

===============================================================
#!/usr/bin/ruby
begin
# From Pickaxe book:
# eval( aString [, aBinding [ file [ line ] ] ]) -> anObject
# Evaluates the Ruby expression(s) in aString. If aBinding is
# given, the evaluation is performed in its context. The
# binding may be a Binding object or a Proc object. If the
# optional file and line parameters are present, they will
# be used when reporting syntax errors.
eval DATA.gets(nil), nil, __FILE__, DATA.lineno
# gets(nil), because we want to set the line separator to be
# nil so that entire DATA is slurped into one big string.
rescue Exception
puts "Error here. May be any error, including SyntaxError"
# Redirect STDERR to STDOUT
STDERR.reopen(STDOUT)
# re-raise error. Now appears on STDOUT
raise
end
__END__
# Your program here, including possible syntax errors.
%&^&$%


===============================================================
Outputs:
Error here. May be any error, including SyntaxError
try.rb:13: compile error (SyntaxError)
try.rb:13: parse error
%&^&$%
^
Exit code: 1 Time: 0.229
===============================================================
 
B

Brian Candler

I want to catch compile time errors with my own method. ...
Eventually,
I came up with the following, which is not as I imagined. It's ugly.

It's correct though - need an 'eval' because you must have a working
(syntactically-valid) program A to report the error, before attempting to
compile invalid program B.

The other way is with require or load:

begin
require 'somefile.rb'
rescue Exception => e
puts "Error here (#{e}). May be any error, including SyntaxError"
end

Regards,

Brian.
 
G

Gerard A.W. Vreeswijk

It's correct though - need an 'eval' because you must have a working
(syntactically-valid) program A to report the error, before attempting to
compile invalid program B.

Allright. This is good. Still, I hope it is possible to report
syntax errors other than by eval. Or am I aiming for the impossible?
(In Perl it is possible anyway.)

Why I want it is to report all errors including syntax to the browser.
Such errors must always be preceded by a "Content-type: text/html\n\n"

I don't want to use the CGI module, because I consider this to be
overkill for the small script I am writing.
 
B

Brian Candler

Thanks Brian. Still, I would not be surprised if there is a solution
by means of opening Excpetion class.

No, there cannot be such a solution.

Ruby runs programs by
(1) parsing the code, and converting to an Annotated Syntax Tree
(2) running the AST

If step 1 fails, no code will run at all.

Brian.
 
G

Gerard A.W. Vreeswijk

No, there cannot be such a solution.

Ruby runs programs by
(1) parsing the code, and converting to an Annotated Syntax Tree
(2) running the AST

If step 1 fails, no code will run at all.

Brian.

I am convinced. So Ruby BEGIN differs from Perl BEGIN. Alas I'd say:
check tail of http://www.oreilly.com/catalog/pperl3/chapter/ch18.html
Compare:

modena:gv/klad-: cat try
BEGIN {
print "In Perl will be parsed and run first, irrespective of parse
errors past BEGIN\n";
}

# Syntax errors.
%&^&$%
modena:gv/klad-: ruby try
try:6: parse error
%&^&$%
^
modena:gv/klad-: perl try
In Perl will be parsed and run first, irrespective of parse errors past
BEGIN
syntax error at try line 6, at EOF
Execution of try aborted due to compilation errors.
modena:gv/klad-:
 
R

Robert Klemme

Gerard A.W. Vreeswijk said:
Allright. This is good. Still, I hope it is possible to report
syntax errors other than by eval. Or am I aiming for the impossible?
(In Perl it is possible anyway.)

Why I want it is to report all errors including syntax to the browser.
Such errors must always be preceded by a "Content-type: text/html\n\n"

I don't want to use the CGI module, because I consider this to be
overkill for the small script I am writing.

You can

- catch exceptions from require (as Brian pointed out).

- replace STDERR (or $stderr) by a StringOut (maybe it has a different
name) and see whether it contains something in which case you report it
back to the client.

You don't need to fuddle with class Exception at all.

Apart from that: Syntax errors in skript are normally program errors which
you don't want to report to the client because they should not occur after
testing; if they do occur, an exception dump and / or a log entry is good
enough.

Of course, if a user may provide a script that you want to execute he
want's to get informed of such errors. But this is - as you might have
guessed - a dangerous thing to do...

Regards

robert
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top