porsager / postgres

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

Incorrect type definitions causing confusion when trying to catch PostgresError #862

Open michalkvasnicak opened 2 months ago

michalkvasnicak commented 2 months ago

I noticed that when I want to catch an error and identify it using instanceof I got an error TypeError: Right-hand side of 'instanceof' is not an object. Which was caused by following code:

import { PostgresError } from 'postgres';

try {
  // await query
} catch (e) {
  if (e instanceof PostgresError) {
    // do something
  }
}

To fix that I had to use default import:

import Postgres from 'postgres';

try {
  // await query
} catch (e) {
  if (e instanceof Postgres.PostgresError) {
    // do something
  }
}

This is confusing because typescript was not able to catch the failing example as something wrong and I was able to find this error only in runtime.

porsager commented 1 month ago

There is no PostgresError named export so the first sample doesn't make sense and I don't know typescript so feel free to do a PR that fixes the ts issue.