Created
October 30, 2024 13:53
-
-
Save marcintustin/47839ebee778a94093f17009e71ae006 to your computer and use it in GitHub Desktop.
Handling a pg error and getting the stacktrace in go
This file contains hidden or 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
import ( | |
"github.com/jackc/pgconn" | |
"github.com/pkg/errors" | |
) | |
func main() { | |
if err != nil { | |
var pgerr *pgconn.PgError | |
if errors.As(err, &pgerr) && pgerr.SQLState() == database.DUPLICATE { | |
t.Log(err) | |
type stackTracer interface { | |
error | |
StackTrace() errors.StackTrace | |
} | |
var tracerr stackTracer | |
var iterr error = err | |
for errors.As(iterr, &tracerr) { | |
t.Error(tracerr.StackTrace()) | |
iterr = errors.Unwrap(tracerr) | |
} | |
t.Fatal(err) | |
} else { | |
t.Fatal(err) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment