-
Notifications
You must be signed in to change notification settings - Fork 294
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
Caught errors within transactions still thrown as uncaught #455
Comments
Check this out: https://www.postgresql.org/docs/14/tutorial-transactions.html Transactions are all or nothing. If you catch error from individual SQL statement inside the You may look at savepoints if you want to avoid this. |
Hmm.. In that case what would you suggest the sql.begin promise should resolve to? I suppose it could be the resolved value from your inner query catch but even so I think it goes a bit against how you use transactions in raw sql. You should use savepoints to recover from specific statement (groups) in your transactions instead. eg. try something like this (not tested) sql.begin(sql =>
sql.savepoint(sql => sql`select * from nonexistent`)
.then(console.log)
.catch((ex) => Promise.resolve('no no it's fine everything is okay'))
).catch((ex) => console.log('got:', ex)); |
there is no intent to make further queries within the transaction after the error. it feels like that ought to be a separate concern from what the return value of the certainly in my scenario the query failure does not mean the user operation failed, and indeed the successful result i need to return once i i understand the correlation you are trying to draw @Megous and i guess if you assume the promise language is meant in this scenario to model and follow a sql transaction's semantics, then it is correct to say that a statement failure is a statement failure is a statement failure because the transaction is done, and therefore the return value of i'd argue instead that promises come with their own semantics beyond just the signature of so i guess yes, i would expect that the result would be the result of my promise chain. i would expect that if i tried to make further database requests some kind of error would occur or be thrown. |
i guess to word a different way, it's very confusing to me that the promise i return from inside begin is respected in terms of flow control but not the result value in the event of a failure. whereas on success both flow control and data are followed. it feels strange that my promises are being awaited on but not always listened to. |
@issa-tseng the I've opened PR #444 to fix this so that As a side note, either way unless you are doing some custom transaction handling you probably don't want to be committing the transaction when there's an error. With PR #444 merged you could do: sql.begin((tsql) => tsql`select * from nonexistent`)
.then(console.log)
.catch((ex) => Promise.resolve(`no no it's fine everything is okay`)); or sql.begin((tsql) =>
tsql`select * from nonexistent`.catch((ex) => Promise.reject(new MyCustomError(ex)))
)
.then(console.log)
.catch((ex) => {
if (ex instanceof MyCustomError) {
return Promise.resolve(`no no it's fine everything is okay`);
} else {
return Promise.reject(ex);
}
}); |
hey, sorry, it's been a while since i had time to sit at a computer. i think your solution sounds reasonable, thank you! |
@issa-tseng sorry I haven't had time to look closer at this, but rereading now, I think your points makes sense. Unfortunately it's a breaking change to alter the behaviour, but I think it's worth looking into for v4. Do you have any input reading the rest of the thread @Megous ? |
hey @porsager, thanks for the note! i'm guessing this is a separate issue, but is this related at all to the multi portal work you tried out? |
maybe it's just me, but i find this surprising:
i feel like this should not result in an error at all. i handled it, right?
The text was updated successfully, but these errors were encountered: