Subject: | 'RAISE NOTICE' does not set $sth->err |
From the mailing list via Lamprecht <christoph.lamprecht@online.de>:
Hello,
I came across the following issue: $sth->err is documented to return 6 in case
of a PGRES_NONFATAL_ERROR (PG notice or warning).
Here it returns undef instead:
Cheers, Christoph LAmprecht
use strict;
use warnings;
use DBI;
use DBD::Pg;
use Test::More 'no_plan';
my $dh = DBI->connect(
"DBI:Pg:dbname=testdb",
) or die $DBI::errstr;
$dh->do(
q|CREATE OR REPLACE FUNCTION test_error_handler(character varying)
RETURNS boolean AS
$BODY$
DECLARE
level ALIAS FOR $1;
BEGIN
IF level ~* 'notice' THEN
RAISE NOTICE 'RAISE NOTICE FROM test_error_handler';
ELSIF level ~* 'warning' THEN
RAISE WARNING 'RAISE WARNING FROM test_error_handler';
ELSIF level ~* 'exception' THEN
RAISE EXCEPTION 'RAISE EXCEPTION FROM test_error_handler';
END IF;
RETURN TRUE ;
END;
$BODY$
LANGUAGE 'plpgsql' VOLATILE
|) or die $dh->errstr;
my $sth = $dh->prepare('SELECT * FROM test_error_handler( ? )');
die $sth->errstr if $sth->err;
for my $level (qw/notice warning/){
$sth->execute($level);
is( $sth->err, 6, "level $level sets err to 6");
}
for my $level (qw/exception/){
$sth->execute($level);
is( $sth->err, 7, "level $level sets err to 7");
}
$sth->finish;
$dh->do('DROP FUNCTION test_error_handler(character varying)')
or die $dh->errstr;
#######
output:
NOTICE: RAISE NOTICE FROM test_error_handler
not ok 1 - level notice sets err to 6
# Failed test 'level notice sets err to 6'
# at test.pl line 47.
# got: undef
# expected: '6'
WARNING: RAISE WARNING FROM test_error_handler
not ok 2 - level warning sets err to 6
# Failed test 'level warning sets err to 6'
# at test.pl line 47.
# got: undef
# expected: '6'
DBD::Pg::st execute failed: ERROR: RAISE EXCEPTION FROM test_error_handler at t
est.pl line 50.
ok 3 - level exception sets err to 7
1..3
# Looks like you failed 2 tests of 3.