Skip to content

Certain transaction errors are swallowed #830

@aslushnikov

Description

@aslushnikov

Hi! First and foremost, thank you for the pleasant and capable library!

Issue 1: swallowed errors from manual transactions

Let's say we have the following SQL file with a wrong INSERT statement (types of permissions mismatches the expected one):

-- bad.sql
BEGIN;

CREATE TABLE user_permissions (
  permissions TEXT[] NOT NULL
);

-- Migration script - NOTE that VALUES is broken, so this operation will fail.
INSERT INTO user_permissions (permissions)
VALUES (('read', 'write', 'delete'));

COMMIT;

Trying to execute this file with sql.file silently swallows the error and pretends that everything is good.

// test.mjs
import postgres from 'postgres'

const sql = postgres({ max: 1 });
await sql.file('./bad.sql');
await sql.end();

I'd expect the sql.file command to throw an error.

Issue 2: swallowed errors from sql.begin()

The following also doesn't throw any errors:

import postgres from 'postgres'

const sql = postgres({ max: 1 });

await sql.begin(async sql => [
  sql`
    CREATE TABLE user_permissions (
      permissions TEXT[] NOT NULL
    )
  `,
  sql`
    INSERT INTO user_permissions (permissions)
    VALUES (('read', 'write', 'delete'))
  `,
]);

await sql.end();

Appendix: when the error is correctly thrown

I found that only the following snippet actually throws an error in this case:

import postgres from 'postgres'

const sql = postgres({ max: 1 });

await sql.begin(async sql => {
  await sql`
    CREATE TABLE user_permissions (
      permissions TEXT[] NOT NULL
    )
  `;
  await sql`
    INSERT INTO user_permissions (permissions)
    VALUES (('read', 'write', 'delete'))
  `;
});

await sql.end();

This throws the expected error:

PostgresError: column "permissions" is of type text[] but expression is of type record
    at ErrorResponse (file:///Users/aslushnikov/flakiness/node_modules/postgres/src/connection.js:790:26)
    at handle (file:///Users/aslushnikov/flakiness/node_modules/postgres/src/connection.js:476:6)
    at Socket.data (file:///Users/aslushnikov/flakiness/node_modules/postgres/src/connection.js:315:9)
    at Socket.emit (node:events:514:28)
    at addChunk (node:internal/streams/readable:376:12)
    at readableAddChunk (node:internal/streams/readable:349:9)
    at Readable.push (node:internal/streams/readable:286:10)
    at TCP.onStreamRead (node:internal/stream_base_commons:190:23)
    at cachedError (file:///Users/aslushnikov/flakiness/node_modules/postgres/src/query.js:170:23)
    at new Query (file:///Users/aslushnikov/flakiness/node_modules/postgres/src/query.js:36:24)
    at sql (file:///Users/aslushnikov/flakiness/node_modules/postgres/src/index.js:112:11)
    at file:///Users/aslushnikov/flakiness/database/test.mjs:18:12 {

Is this a known issue? Do I do something wrong?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions