From: Chris W. <ch...@cw...> - 2001-10-22 13:55:32
|
* Ray Zimmerman (rz...@co...) [011022 09:32]: > So, I was wondering, why do you use the "put the error info into some > error variables and die with a string" approach rather than "put the > error info into an object and die with that object" approach to error > handling in SPOPS? Maybe there is an advantage I don't see. And the > follow-on question, in case it turns out the latter is more desirable > ... would you consider changing it? I'll have to dig into the history books for this one... Ah, I kind of remember. I had tried (in OpenInteract) to create a global error handler, much like the one Matt Sergeant wrote up in the mod_perl guide. But I had some problems with internal errors being thrown by some modules NOT being a string -- that is, they expected a string and died a horrible death when they couldn't examine the string. Do I remember the modules or the errors? Nope. This was 2+ years ago. So instead of looking into it more, I just created the simpler version we have now. (Lazy) So I totally agree that it's a better way to do things, particularly when you frequently get all the error information anyway: ---------------------------------------- my $object = eval { $class->fetch( $id ) }; if ( $@ ) { my $error_info = SPOPS::Error->get; warn "Error: $error_info->{system_msg}\n"; } ---------------------------------------- So yes, I would definitely change it. The only concern I have is backward compatibility. There are just a few items with that. (1) Create a 'set()' method that is just an alias for 'new()' (2) Create a 'get()' method that just returns the last error object created, and modify 'new()' to track the last object created. (3) Allow people to still inspect $@ as if it were a string. We should be able to use 'overload' to stringify the object so people can do this. Simple example: ---------------------------------------- package My::Test; use strict; use overload '""' => \&as_string; sub new { return bless( $_[1], $_[0] ) } sub as_string { return $_[0]->{user_msg} } package main; eval { die My::Test->new({ user_msg => 'This is the user message', system_msg => 'The system is going down!' }); }; if ( $@ ) { print "User: $@->{user_msg}\n", "Sys: $@->{system_msg}\n"; if ( $@ =~ /user/ ) { print "Found 'user' in error message!"; } } ---------------------------------------- So I think we should be good. (I'll rewrite some code that uses the errors and see for sure.) I don't think we need to make any other modifications to SPOPS error handling. It's very simple and should probably continue to be so. But... While we're rewriting it we might want to think about any other changes -- no reason to change the API multiple times in a short period :-) Chris -- Chris Winters (ch...@cw...) Building enterprise-capable snack solutions since 1988. |