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
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
%&^&$%
^
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.
···
On Mon, Jul 07, 2003 at 05:15:45PM +0900, Gerard A.W. Vreeswijk wrote:
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.
On Mon, Jul 07, 2003 at 05:15:45PM +0900, Gerard A.W. Vreeswijk wrote:
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.
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.
···
On Mon, Jul 07, 2003 at 08:24:45AM +0000, Gerard A.W. Vreeswijk wrote:
On Mon, 7 Jul 2003 17:19:26 +0900, in comp.lang.ruby you wrote:
>On Mon, Jul 07, 2003 at 05:15:45PM +0900, Gerard A.W. Vreeswijk wrote:
>> 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.
Thanks Brian. Still, I would not be surprised if there is a solution
by means of opening Excpetion class.
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-:
I don’t want to use the CGI module, because I consider this to be
overkill for the small script I am writing.
just a note: the CGI module is pretty small if you just go CGI.new
as opposed to CGI.new(“html”), because none of the tag writing
stuff is initialized.
“Gerard A.W. Vreeswijk” gv@cs.uu.nl schrieb im Newsbeitrag
news:3f093a9f.6962781@news.cs.uu.nl…
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.
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
···
On Mon, 7 Jul 2003 17:19:26 +0900, Brian Candler B.Candler@pobox.com > wrote:
On Mon, Jul 07, 2003 at 05:15:45PM +0900, Gerard A.W. Vreeswijk wrote:
I’d say Ruby’s model is much cleaner. It’s very clear which is the parsing
stage, and which is the execution stage - and personally I wouldn’t want
Ruby to attempt to execute any program which it could not even parse!
Note that things like ‘require’ and class definitions don’t actually happen
until the execution stage either:
class Foo
def wibble
end
end # << class Foo does not exist until this point has been RUN
a = Foo.new
This means you can do conditional creation of classes:
if ARGV[0] == “test”
class Foo
def wibble
puts “Testing only”
end
end
else
class Foo
def wibble
puts “Live environment”
end
end
end
Foo.new.wibble
$ ruby x.rb
Live environment
$ ruby x.rb test
Testing only
Regards,
Brian.
···
On Mon, Jul 07, 2003 at 07:36:42PM +0900, Gerard A.W. Vreeswijk wrote:
On Mon, 7 Jul 2003 18:38:03 +0900, Brian Candler B.Candler@pobox.com > wrote:
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: