Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I have a module that is effectively a wrapper for DBI - when you call it's constructor (new), you pass the same arguments you would to DBI::connect(). The problem i am having is getting the module to carp errors back to the client without giving away the line number from the module itself. For example, given some wrapper named Foo and trying to connect to a non-existant server, i would like for the following error:
DBI->connect(mysql:host) failed: Unknown MySQL Server Host 'host' (2)
   at Foo.pm line 13
   at ./foo.pl line 11
to only list ./foo.pl, the client that called the module.

Now, philosophical question: if this really should be desired behavior and left alone, then i am all for that. But, if there is a way to implement what i'd like, then i would sure appreciate a nudge in the right direction. My reasoning is that such errors should not accidentaly lead a user into the source code of the module, because it is a 'red herring'. By even mentioning the originating line of code from the wrapper module, a user might just be tempted to think the 'module is broken.' (I also have an appreciation for debugging as well, and would like to leave that door open.)

Here is what i have been working with so far:

package Foo; use DBI; use Carp; use strict; sub new { my $class = shift; my $self = {}; bless $self, $class; # THE PROBLEM: $self->{'dbh'} = DBI->connect(@_) or carp; return $self; } 1;
I did try wrapping the instantiation in an eval block:
# THE SAME PROBLEM: eval { $self->{'dbh'} = DBI->connect(@_,{RaiseError=>1,PrintError=>0}) }; carp $@ if $@;
... but the results were exactly the same. An evil thought did cross my mind at this point - substitute the module line number(s) out ... but that surely is not 'The Right Way'.

And here is the client which calls the wrapper with bad credentials to generate the errors. It also uses DBI in the same manner as the 'control subject':

use strict; use Foo; use DBI; print "Comparing wrapper:\n"; my $foo = Foo->new(qw(DBI:mysql:mysql:host user pass)); print "\nComparing DBI:\n"; my $dbh = DBI->connect(qw(DBI:mysql:mysql:host user pass));
What i would like is to only show that the error orginated from the client and not the module ... just like DBI reports:
DBI->connect(mysql:host) failed: Unknown MySQL Server Host 'host' (2)
   at ./foo.pl line 14
Thanks!

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

In reply to Confusion about properly using Carp by jeffa

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-19 02:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found