Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save requaos/93e01fa3e00ed286cfde003a739cab0e to your computer and use it in GitHub Desktop.
Save requaos/93e01fa3e00ed286cfde003a739cab0e to your computer and use it in GitHub Desktop.
When using SQLBoiler with Postgres DB, you will eventually need to react to unique constraint violations in a constructive manner:
logger := zap.NewNOP()
err = pilot.Insert(ctx, m.DB.Master(), boil.Infer())
switch err := errors.Cause(err).(type) {
case nil:
// Carry on
case *pq.Error:
if err.Code.Name() == "unique_violation" && err.Constraint == "unique_constraint_name" {
// Do something
return nil
}
logger.Error("Failed to save pilot",
zap.Error(err),
zap.String("pq_err_code", string(err.Code)),
zap.String("pq_err_code_name", err.Code.Name()))
return errors.Wrap(err, "failed save pilot")
default:
logger.Error("Failed to save pilot",
zap.Error(err),
zap.String("error_type", fmt.Sprintf("%T", err)),
zap.String("error_detail", fmt.Sprintf("%#v", err)))
return errors.Wrap(err, "failed save pilot")
}
@requaos
Copy link
Author

requaos commented Apr 10, 2019

Using fields on your logger to print relevant info and also wrapping the errors returned with stack and additional message

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment