microsoft / go-sqlcmd

The new sqlcmd, CLI for SQL Server and Azure SQL (winget install sqlcmd / sqlcmd create mssql / sqlcmd open ads)
https://learn.microsoft.com/sql/tools/sqlcmd/go-sqlcmd-utility
MIT License
395 stars 63 forks source link

Add support for individualized error handling to the checkErr pattern #212

Open stuartpa opened 1 year ago

stuartpa commented 1 year ago

Right now we inject a checkErr handler, which we use for to cope with all err return where we always expect a nil response, and let the upper layer cope with it. But this doesn't allow for nuance in error handling. But in general for a CLI there is little nuance, if something generates and err, we just bail.

Comment in PR:

"Can the app layer implementations of checkErr similar to how there can be multiple levels of a try/catch for exceptions? If there's only a global error handler referenced by checkErr then any code that needs individualized error handling isn't going to use this wrapper. "

shueybubbles commented 1 year ago

the type of scenario I'm concerned about is partial completion of some operation where we need to clean up or roll back something on failure before the app exits. Would appropriate usage of defer accomplish the goal? Does the deferred func still get called after checkerr does its thing?

success := false
y := createSomeObject()
defer if !success {
  y.Cleanup()
}
callMethodThatUsesCheckErr(y)
y.Commit()
success = true