Skip to content

Commit

Permalink
error pgx handling
Browse files Browse the repository at this point in the history
  • Loading branch information
fauzanfebrian committed Nov 18, 2023
1 parent 86486c1 commit 913a56f
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 24 deletions.
11 changes: 4 additions & 7 deletions api/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

db "github.com/fauzanfebrian/simplebank/db/sqlc"
"github.com/gin-gonic/gin"
"github.com/lib/pq"
)

type createAccountRequest struct {
Expand All @@ -28,12 +27,10 @@ func (server *Server) createAccount(ctx *gin.Context) {
}
account, err := server.store.CreateAccount(ctx, arg)
if err != nil {
if pqErr, ok := err.(*pq.Error); ok {
switch pqErr.Code.Name() {
case "foreign_key_violation", "unique_violation":
ctx.JSON(http.StatusForbidden, errorResponse(err))
return
}
switch db.ErrorCode(err) {
case db.ForeignKeyViolation, db.UniqueViolation:
ctx.JSON(http.StatusForbidden, errorResponse(err))
return
}
ctx.JSON(http.StatusInternalServerError, errorResponse(err))
return
Expand Down
11 changes: 4 additions & 7 deletions api/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/fauzanfebrian/simplebank/util"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/lib/pq"
)

type createUserRequest struct {
Expand Down Expand Up @@ -61,12 +60,10 @@ func (server *Server) createUser(ctx *gin.Context) {
}
user, err := server.store.CreateUser(ctx, arg)
if err != nil {
if pqErr, ok := err.(*pq.Error); ok {
switch pqErr.Code.Name() {
case "unique_violation":
ctx.JSON(http.StatusForbidden, errorResponse(err))
return
}
switch db.ErrorCode(err) {
case db.UniqueViolation:
ctx.JSON(http.StatusForbidden, errorResponse(err))
return
}
ctx.JSON(http.StatusInternalServerError, errorResponse(err))
return
Expand Down
20 changes: 19 additions & 1 deletion db/sqlc/error.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
package db

import "github.com/jackc/pgx/v5"
import (
"errors"

"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
)

var ErrRecordNotFound = pgx.ErrNoRows

const (
ForeignKeyViolation = "23503"
UniqueViolation = "23505"
)

func ErrorCode(err error) string {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
return pgErr.Code
}
return ""
}
8 changes: 2 additions & 6 deletions gapi/create_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/fauzanfebrian/simplebank/val"
"github.com/fauzanfebrian/simplebank/worker"
"github.com/hibiken/asynq"
"github.com/lib/pq"
"google.golang.org/genproto/googleapis/rpc/errdetails"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -39,11 +38,8 @@ func (server *Server) CreateUser(ctx context.Context, req *pb.CreateUserRequest)
}
txResult, err := server.store.CreateUserTx(ctx, arg)
if err != nil {
if pqErr, ok := err.(*pq.Error); ok {
switch pqErr.Code.Name() {
case "unique_violation":
return nil, status.Errorf(codes.AlreadyExists, "username already exist: %s", err)
}
if db.ErrorCode(err) == db.UniqueViolation {
return nil, status.Errorf(codes.AlreadyExists, err.Error())
}
return nil, status.Errorf(codes.Internal, "failed to create user: %s", err)
}
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ require (
github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.0
github.com/hibiken/asynq v0.24.1
github.com/jackc/pgx/v5 v5.5.0
github.com/lib/pq v1.10.9
github.com/rakyll/statik v0.1.7
github.com/redis/go-redis/v9 v9.0.3
github.com/rs/zerolog v1.31.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,6 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
Expand Down

0 comments on commit 913a56f

Please sign in to comment.