-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Add SQL message to error object #1714
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
Conversation
This provides the message associated with a SQL error (can be customized when throwing a user error in MySQL) in the error object. This is useful for passing messages through directly from the database to the end user.
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.
Thanks for the pull request! Can you update the PR to follow the contributing guide (https://github.com/mysqljs/mysql#contributing)? Notably the following seems to be missing: (3) include tests
Let me know if you need help updating it and I can :)
Will do! Sorry about that, wasn't sure if a change this small qualified for a new test or not. Thanks for the quick review. |
@dougwilson I added tests for the function that I changed. I couldn't find any tests for the I tried to match the existing code and testing styles, but please don't hesitate to tell me if I made any mistakes. |
Hi @AdamVig it's no problem at all! So we don't test the |
Per @dougwilson, this file is tested indirectly by integration tests instead of being tested directly by unit tests. This reverts commit f4113f4.
This test ensures that the `Sequence` class passes through the error message from the database in the `sqlMessage` property so that it can be passed to the end user.
Thanks for the pointers! I added an integration test for this feature. I used |
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.
Nice! Only one last nit: I cannot re-run this test without manually going into the db and dropping the table and trigger between the tests. Can you make the tests re-runnable? Typically the tests use temporary tables, for example. If you want to make a table and drop it manually, then make sure you perform the hook within a handler that will run even when the test fails.
Since a temporary table cannot be used in this test (triggers cannot be defined on temporary tables), the table used must be dropped after the test runs.
Good catch! (And thank you for all the feedback.) I added a query to drop the table after the test runs. I'm not sure it will run when the test fails, though. Can you give me an example of how to do that? |
Hi @AdamVig I'll take a shot and creating an example when I get to a computer, but just wanted to respond now. Generally I would say that all code paths just need to lead to dropping the table in your code. For example, I would change it such that when |
Now, even when a part of the test fails, the test table will be dropped. This ensures that the test is repeatable.
That explanation was helpful - I may not need an example after all. I am used to test frameworks with hooks like My latest commit follows your advice, and I think it drops the table in all situations in which the test could fail. |
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.
Great! I think I'll just go ahead and wrap up this PR so it doesn't sit forever. The error handling doesn't work still, unfortunately, so I'll just tweak it to work. You can test this by making a typo in the CREATE TRIGGER and see it errors but the table is never dropped. This is because the DROP is queued to send over the network, but then the test is immediately crashed with the assert.ifError right below that line.
Thank you @dougwilson! |
This provides the message associated with a SQL error (can be customized when throwing a user error in MySQL) in the error object. This is useful for passing messages through directly from the database to the end user.
The motivation for this change is my usage of
Knex.js
on an API I am building. I am using MySQL triggers for data validation and I want to pass the error messages straight from the triggers to the end user via the API.See this issue comment for an example of the error object format I see in
Knex.js
. This pull request will add a property to the error object calledsqlMessage
that has just the message from the thrown MySQL error.Thanks for maintaining this project!