Skip to content

Commit

Permalink
better error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 28, 2018
1 parent 37042c1 commit c2db7d1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
8 changes: 5 additions & 3 deletions lib/SQL/Abstract/Pg.pm
Expand Up @@ -25,9 +25,10 @@ sub _insert_returning {
$conflict => {
ARRAYREF => sub {
my ($fields, $set) = @$conflict;
puke qq{ARRAYREF value "$fields" not allowed}
puke qq{on_conflict value "$fields" not allowed}
unless ref $fields eq 'ARRAY';
puke qq{ARRAYREF value "$set" not allowed} unless ref $set eq 'HASH';
puke qq{on_conflict value "$set" not allowed}
unless ref $set eq 'HASH';

$conflict_sql
= '(' . join(', ', map { $self->_quote($_) } @$fields) . ')';
Expand Down Expand Up @@ -96,7 +97,7 @@ sub _order_by {
$self->_SWITCH_refkind(
$for => {
SCALAR => sub {
puke qq{SCALAR value "$for" not allowed} unless $for eq 'update';
puke qq{for value "$for" not allowed} unless $for eq 'update';
$for_sql = $self->_sqlcase('UPDATE');
},
SCALARREF => sub { $for_sql .= $$for }
Expand All @@ -121,6 +122,7 @@ sub _table {

$table = $self->SUPER::_table(\@table);
for my $join (@join) {
puke 'join value needs at least 3 elements' if @$join < 3;
my $type = @$join > 3 ? shift @$join : '';
my ($name, $fk, $pk) = @$join;
$table
Expand Down
10 changes: 7 additions & 3 deletions t/sql.t
Expand Up @@ -58,9 +58,9 @@ is_deeply \@sql, $result, 'right query';

# ON CONFLICT (unsupported value)
eval { $abstract->insert('foo', {bar => 'baz'}, {on_conflict => [{}]}) };
like $@, qr/ARRAYREF value "HASH/, 'right error';
like $@, qr/on_conflict value "HASH/, 'right error';
eval { $abstract->insert('foo', {bar => 'baz'}, {on_conflict => [[], []]}) };
like $@, qr/ARRAYREF value "ARRAY/, 'right error';
like $@, qr/on_conflict value "ARRAY/, 'right error';
eval { $abstract->insert('foo', {bar => 'baz'}, {on_conflict => {}}) };
like $@, qr/HASHREF/, 'right error';

Expand Down Expand Up @@ -97,7 +97,7 @@ is_deeply \@sql, ['SELECT * FROM "foo" FOR update skip locked'], 'right query';

# FOR (unsupported value)
eval { $abstract->select('foo', '*', undef, {for => 'update skip locked'}) };
like $@, qr/SCALAR value "update skip locked" not allowed/, 'right error';
like $@, qr/for value "update skip locked" not allowed/, 'right error';
eval { $abstract->select('foo', '*', undef, {for => []}) };
like $@, qr/ARRAYREF/, 'right error';

Expand All @@ -119,4 +119,8 @@ is_deeply \@sql,
['SELECT * FROM "foo" LEFT JOIN "bar" ON ("bar"."foo_id" = "foo"."id")'],
'right query';

# JOIN (unsupported value)
eval { $abstract->select(['foo', []]) };
like $@, qr/join value needs at least 3 elements/, 'right error';

done_testing();

0 comments on commit c2db7d1

Please sign in to comment.