porsager / postgres

Postgres.js - The Fastest full featured PostgreSQL client for Node.js, Deno, Bun and CloudFlare
The Unlicense
7.42k stars 271 forks source link

PostgresError prints as {} with node --test #696

Closed Pyrolistical closed 11 months ago

Pyrolistical commented 1 year ago

Env:

$ node -v
v20.8.0
$ ack postgres package.json
    "postgres": "3.4.0",

Given repo.js

import { test } from "node:test";

import postgres from "postgres";
test("should print PostgresError", async () => {
  const sql = postgres(`postgres://${process.env.USER}@localhost`);
  try {
    await sql`
      invalid query
    `;
  } catch (error) {
    // console.log("error does print correctly", error);
    throw error;
  } finally {
    await sql.end();
  }
});

Run node --test repo.js

Expected

$ node --test repo.js
✖ should print PostgresError (...ms)
  PostgresError: syntax error at or near "invalid"
      at TestContext.<anonymous> (.../repo.js:12:11)
      at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
      at async Test.run (node:internal/test_runner/test:632:9)
      at async startSubtest (node:internal/test_runner/harness:208:3)

Actual

$ node --test repo.js
✖ should print PostgresError (...ms)
  {}
Pyrolistical commented 1 year ago

I think it has something to do with how the errors from the test are serialized to the test runner. By the time it gets to class TestsStream, it is already broken.

Pyrolistical commented 1 year ago

I found the issue. It is caused by the Object.create in queryError

This hack fixes the issues:

function queryError(query, err) {
    query.reject(err)
    // query.reject(Object.create(err, {
    //   stack: { value: err.stack + query.origin.replace(/.*\n/, '\n'), enumerable: options.debug },
    //   query: { value: query.string, enumerable: options.debug },
    //   parameters: { value: query.parameters, enumerable: options.debug },
    //   args: { value: query.args, enumerable: options.debug },
    //   types: { value: query.statement && query.statement.types, enumerable: options.debug }
    // }))
  }