-
-
Notifications
You must be signed in to change notification settings - Fork 134
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
feat: include pg message in errors #234
Conversation
test/slonik/integration.ts
Outdated
|
||
t.regex( | ||
error.message, | ||
/^Query violates a unique integrity constraint: duplicate key value violates unique constraint "person_pkey"$/, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any thoughts on getting rid of the "Query violates a unique integrity constraint" part completely? We'd still get the type safety by having the UniqueIntegrityConstraintViolationError
class, and the other part conveys the same information, but a bit more helpfully/specifically.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am of the inverse opinion that we shouldn't include: duplicate key value violates unique constraint "person_pkey"
part.
I am generally of opinion that error messages should be static, and that details such as "which constraint" belong as properties of the error.
Mostly to make coveralls happy
src/errors.ts
Outdated
readonly originalError: Error | ||
|
||
constructor (originalError: Error, message: string) { | ||
super(`${message}: ${originalError.message}`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the reason for wanting to include the original error message?
I've set a reminder to comeback to this PR tomorrow. My main concern is that there is no guarantees that |
e974d7c
to
3936167
Compare
3936167
to
47050e6
Compare
There's always a tradeoff between error specificity and stability. I think of Error messages as mostly for humans. So they should be short, predictable and understandable, but also deliver an appropriate amount of information. In general I find postgres does a good job finding that balance. Right now slonik abstracts away the slightly more precise postgres message. To take the example from the linked issue: let slonik = createPool(...)
await slonik.query(sql`create table t1(id int unique, name text unique)`)
await slonik.query(sql`insert into t1 values(1, 'a')`)
await slonik.query(sql`insert into t1 values(2, 'a')`).catch(e => console.log(e.message)) In the above code, we'll only ever see "Query violates a unique integrity constraint.", whereas with the original message, we'd see "duplicate key value violates unique constraint "t1_name_key". With the original error message, you can see at a glance which table and column is the root of the problem. Of course,
As for coupling - you're right, there's somewhat increased coupling - but isn't it to postgres itself, rather than |
@gajus bump on this. If you want to keep the errors as they are, what if I made it configurable? Right now there's no way as a user for me to switch to the original error message. I could add a function Open to that idea? |
Thank you for bumping the conversation. Your arguments make sense. I have pushed the update. |
🎉 This PR is included in version 23.2.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Fixes #233