Skip to content

Instantly share code, notes, and snippets.

@kennwhite
Forked from 4ydx/Better error handling
Created May 9, 2016 14:28
Show Gist options
  • Save kennwhite/62a273335358598c61bae943ce155bb7 to your computer and use it in GitHub Desktop.
Save kennwhite/62a273335358598c61bae943ce155bb7 to your computer and use it in GitHub Desktop.
Wrapping multiple similar calls so that error handling code is reduced.
type WriteDB struct {
Conn *sql.DB
Err error
}
func (db *WriteDB) Do(
update,
title,
updated_at,
updated_by,
titleString,
) {
if db.Err != nil {
return
}
_, db.Err = db.Conn.Exec(update, title, updated_at, updated_by, titleString)
}
func main() {
var db WriteDB
db.Conn, db.Err = readDB.Begin()
db.Do(stmtUpdateSettings, title, updated_at, updated_by, "title")
db.Do(stmtUpdateSettings, logo, updated_at, updated_by, "logo")
if db.Err != nil {
db.Conn.Rollback()
}
return db.Conn.Commit()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment