-
Notifications
You must be signed in to change notification settings - Fork 506
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Proof of concept for ErrorHandler interface
This implementation is not complete. It just demonstrates the ideas behind the ErrorHandler interface.
- Loading branch information
Craig Fitzpatrick
committed
Jul 9, 2021
1 parent
b597672
commit 9cbf4b8
Showing
2 changed files
with
78 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package mssql | ||
|
||
import ( | ||
"context" | ||
"database/sql/driver" | ||
) | ||
|
||
// ErrorHandler interface allows user to alter the error returned by | ||
// the driver to database/sql | ||
type ErrorHandler interface { | ||
HandleError( | ||
queryCtx context.Context, | ||
transactionCtx context.Context, | ||
stmt string, | ||
connectionGood bool, | ||
err error) error | ||
} | ||
|
||
// optionalErrorHandler implements the ErrorHandler interface with | ||
// a default behavior (pass back the error unaltered) that can be | ||
// overridden with an optional ErrorHandler supplied by the user. | ||
type optionalErrorHandler struct { | ||
errorHandler ErrorHandler | ||
} | ||
|
||
// HandleError returns the orignal error by default unless the user | ||
// has specified an optional ErrorHandler to override the default. | ||
func (o optionalErrorHandler) HandleError( | ||
queryCtx context.Context, | ||
transactionCtx context.Context, | ||
stmt string, | ||
connectionGood bool, | ||
err error) error { | ||
if nil != o.errorHandler { | ||
return o.errorHandler.HandleError(queryCtx, transactionCtx, stmt, connectionGood, err) | ||
} else { | ||
return err | ||
} | ||
} | ||
|
||
// AutoRetryErrorHandler implements the ErrorHandler interface by | ||
// returning driver.ErrBadConn when the underlying connection is bad. | ||
// This allows the auto-retry logic in database/sql to work as designed, | ||
// at the expense of error detail (see issues #275 and #586) | ||
type AutoRetryErrorHandler struct{} | ||
|
||
// HandleError returns driver.ErrBadConn when the underlying connection | ||
// is bad, allowing database/sql auto-retry logic to work as designed. | ||
func (a AutoRetryErrorHandler) HandleError( | ||
queryCtx context.Context, | ||
transactionCtx context.Context, | ||
stmt string, | ||
connectionGood bool, | ||
err error) error { | ||
if !connectionGood { | ||
return driver.ErrBadConn | ||
} else { | ||
return err | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters