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

Error position #20

Open
alecmev opened this issue Dec 14, 2018 · 3 comments
Open

Error position #20

alecmev opened this issue Dec 14, 2018 · 3 comments

Comments

@alecmev
Copy link

alecmev commented Dec 14, 2018

I've hand-written a pretty large baseline migration, and then started debugging it by creating a fresh database and running postgres-migrations repeatedly. One problem I've run into is that crucial error details are lost inside this library, here:

} catch (err) {
try {
await cleanup()
} finally {
// eslint-disable-next-line no-unsafe-finally
throw new Error(
dedent`
An error occurred running '${migration.name}'. Rolled back this migration.
No further migrations were run.
Reason: ${err.message}`,
)
}
}

Sometimes PostgreSQL spews out messages like syntax error at or near ")", in a file full of functions, triggers, constraints, arrays, etc. The only way to find the cause for an error like that is to know the position, which is contained inside the err returned from the driver.

The easiest solution would probably be to extend the error message with Position: ${err.position}, but there may be some value is passing through the other fields too. An even better solution would be to print the problematic line and its surroundings, with the exact position pointed out with a ^, but that's a lot of extra effort for a small QoL improvement, so that's just an idea for the future.

@ThomWright
Copy link
Owner

Hi, thanks for the issue. I reckon the solution of putting the err.position in our error message is something I'd accept a PR for.

Probably the main reason I don't do much with the errors is that I don't know what errors might be thrown. I assume not every error will have a position property. However, if it does it seems worth emitting that.

What I tend to do in your position is to look at the PostgreSQL logs, mainly because I don't trust JS libraries (including mine!) to give the full picture.

@bionicles
Copy link

Similar problem on my first attempts to use this library. I get pumped and write a big migration, then gotta debug it. But the error message reads: error at or near "." ... and there are 76 "." in the migration!

cause: Error: An error occurred running 'prepare'. Rolled back this migration. No further migrations were run. Reason: syntax error at or near "."

Can we at least add line indicator?

@ThomWright
Copy link
Owner

I don't think Postgres gives that information (correct me if I'm wrong). We can only report what we get back from Postgres.

Again, I'm happy to accept a PR for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants