From 9a4fe31a3d20aa9ecc6efebed734107055139feb Mon Sep 17 00:00:00 2001 From: Christian Carlsson Date: Mon, 23 Dec 2024 01:28:32 +0000 Subject: [PATCH] feat: handle no db results error --- server/rpc/handlers/v1/exercise.go | 2 +- server/rpc/handlers/v1/routine.go | 2 +- server/xzap/xzap_test.go | 46 ++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 server/xzap/xzap_test.go diff --git a/server/rpc/handlers/v1/exercise.go b/server/rpc/handlers/v1/exercise.go index 9f6c74dd..ed641147 100644 --- a/server/rpc/handlers/v1/exercise.go +++ b/server/rpc/handlers/v1/exercise.go @@ -53,7 +53,7 @@ func (h *exerciseHandler) GetExercise(ctx context.Context, req *connect.Request[ exercise, err := h.repo.GetExercise(ctx, repo.GetExerciseWithID(req.Msg.GetId())) if err != nil { if errors.Is(err, sql.ErrNoRows) { - log.Error("exercise not found") + log.Warn("exercise not found") return nil, connect.NewError(connect.CodeNotFound, nil) } diff --git a/server/rpc/handlers/v1/routine.go b/server/rpc/handlers/v1/routine.go index 00cc571f..29d95645 100644 --- a/server/rpc/handlers/v1/routine.go +++ b/server/rpc/handlers/v1/routine.go @@ -62,7 +62,7 @@ func (h *routineHandler) GetRoutine(ctx context.Context, req *connect.Request[ap ) if err != nil { if errors.Is(err, sql.ErrNoRows) { - log.Error("routine not found", zap.Error(err)) + log.Warn("routine not found", zap.Error(err)) return nil, connect.NewError(connect.CodeNotFound, nil) } diff --git a/server/xzap/xzap_test.go b/server/xzap/xzap_test.go new file mode 100644 index 00000000..df305b1a --- /dev/null +++ b/server/xzap/xzap_test.go @@ -0,0 +1,46 @@ +package xzap_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + "go.uber.org/zap/zapcore" + + "github.com/crlssn/getstronger/server/xzap" +) + +func TestFields(t *testing.T) { + t.Parallel() + + require.Equal(t, zapcore.Field{ + Key: "rpc", + Type: zapcore.StringType, + Integer: 0, + String: "value", + Interface: nil, + }, xzap.FieldRPC("value")) + + require.Equal(t, zapcore.Field{ + Key: "user_id", + Type: zapcore.StringType, + Integer: 0, + String: "value", + Interface: nil, + }, xzap.FieldUserID("value")) + + require.Equal(t, zapcore.Field{ + Key: "routine_id", + Type: zapcore.StringType, + Integer: 0, + String: "value", + Interface: nil, + }, xzap.FiledRoutineID("value")) + + require.Equal(t, zapcore.Field{ + Key: "exercise_id", + Type: zapcore.StringType, + Integer: 0, + String: "value", + Interface: nil, + }, xzap.FieldExerciseID("value")) +}