Created
April 10, 2019 20:47
-
-
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:
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
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") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using fields on your logger to print relevant info and also wrapping the errors returned with stack and additional message