Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]: Postgres.JS errors don't show app stack trace #1827

Closed
Hansenq opened this issue Jan 24, 2024 · 3 comments
Closed

[BUG]: Postgres.JS errors don't show app stack trace #1827

Hansenq opened this issue Jan 24, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@Hansenq
Copy link

Hansenq commented Jan 24, 2024

What version of drizzle-orm are you using?

0.29.3

What version of drizzle-kit are you using?

0.20.13

Describe the Bug

I connect to my database with Postgres.js. When I execute a query through drizzle, and I hit an error in my database (such as a column not found, etc.), I expect a stack trace to let me know which line of my code caused the error. This is an error with drizzle's use of Postgres.js, since the error doesn't happen with node-postgres.

The stack trace that appears contains a stack trace of where the error hit inside the Postgres.js adapter, but it doesn't show where the error occurred in drizzle or in my code:

PostgresError: relation "assistants" does not exist
    at ErrorResponse (/Users/hansenq/Documents/CS/lightski/node_modules/postgres/cjs/src/connection.js:790:26)
    at handle (/Users/hansenq/Documents/CS/lightski/node_modules/postgres/cjs/src/connection.js:476:6)
    at Socket.data (/Users/hansenq/Documents/CS/lightski/node_modules/postgres/cjs/src/connection.js:315:9)
    at Socket.emit (node:events:515:28)
    at Socket.emit (node:domain:488:12)
    at addChunk (node:internal/streams/readable:545:12)
    at readableAddChunkPushByteMode (node:internal/streams/readable:495:3)
    at Socket.Readable.push (node:internal/streams/readable:375:5)
    at TCP.onStreamRead (node:internal/stream_base_commons:190:23) {
  severity_local: 'ERROR',
  severity: 'ERROR',
  code: '42P01',
  position: '187',
  file: 'parse_relation.c',
  line: '1395',
  routine: 'parserOpenTable'
}

This makes it extremely difficult to debug where the error happened, since I have no idea where the error was hit.

Expected behavior

When I switch to node-postgres, I the error is what I expect: a stack trace for my app-level code:

error: relation "assistants" does not exist
    at /Users/hansenq/Documents/CS/lightski/node_modules/pg-pool/index.js:45:11
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async /Users/hansenq/Documents/CS/lightski/node_modules/src/node-postgres/session.ts:64:19
    at async backfillEmbedsFromAssistants (/Users/hansenq/Documents/CS/lightski/packages/database/bin/data-migrations/2024-01-23-backfill-embeds-from-assistants.ts:7:22)
    at async main (/Users/hansenq/Documents/CS/lightski/packages/database/bin/data-migrations/2024-01-23-backfill-embeds-from-assistants.ts:33:3) {
  length: 110,
  severity: 'ERROR',
  code: '42P01',
  detail: undefined,
  hint: undefined,
  position: '187',
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: 'parse_relation.c',
  line: '1395',
  routine: 'parserOpenTable'
}

Environment & setup

No response

@Hansenq Hansenq added the bug Something isn't working label Jan 24, 2024
@realmikesolo
Copy link
Collaborator

Hi, @Hansenq. It appears that the issue you're encountering is related to the differences in how database drivers, specifically Postgres.js and node-postgres (pg), handle and report errors. Each driver has its own error handling mechanism that can affect the stack trace and the level of detail provided in error messages. So, this is not related to Drizzle ORM.

@Hansenq
Copy link
Author

Hansenq commented Jan 28, 2024

Ah, makes sense. Is there a way inside drizzle to intercept database adapter errors and print out/add more information?

Otherwise, this is pretty nasty error that makes it much more difficult to debug, and it might be good to recommend node-postgres over postgres.js for drizzle users in the documentation, just because of this error handling.

@AndriiSherman
Copy link
Member

Yeah, we have a ticket for proper error handling and wrapping drivers errors with our own. You can track it here: #376

I'll close this issue then. Thanks for your report!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants