refactor(api): move postgres error handling into a separate function in a separate file

This commit is contained in:
2025-01-06 03:53:08 +03:00
parent c7df4a1960
commit 9a0b81fba3
3 changed files with 26 additions and 35 deletions
+22
View File
@@ -0,0 +1,22 @@
package api
import (
"strconv"
"github.com/jackc/pgx/v5/pgconn"
)
func HandlePgError(err error) (int, string) {
pgErr, ok := err.(*pgconn.PgError)
if !ok {
return 500, err.Error()
}
statusStr := pgErr.Message[:3]
message := pgErr.Message[4:]
status, err := strconv.ParseInt(statusStr, 10, 0)
if err == nil {
return int(status), message
} else {
return 500, message
}
}