From 9f0d21a85c73299b1efafc352828de501f878e46 Mon Sep 17 00:00:00 2001 From: soneda-yuya Date: Sun, 12 Jan 2025 14:38:59 +0900 Subject: [PATCH] fix: test --- server/Makefile | 3 +++ server/internal/app/graphql.go | 11 ++++++----- server/internal/app/graphql_test.go | 12 +++++++++++- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/server/Makefile b/server/Makefile index 470b16e92..c6104d293 100644 --- a/server/Makefile +++ b/server/Makefile @@ -63,6 +63,9 @@ run-app: run-db: docker compose -f ../docker-compose.yml up -d reearth-mongo +test: + REEARTH_DB=reearth go test -v ./... + generate: go generate ./... diff --git a/server/internal/app/graphql.go b/server/internal/app/graphql.go index caf17fd6b..6c5443951 100644 --- a/server/internal/app/graphql.go +++ b/server/internal/app/graphql.go @@ -112,6 +112,11 @@ func customErrorPresenter(ctx context.Context, e error, devMode bool) *gqlerror. systemError = e.Error() } + // Ensure Extensions map exists + if graphqlErr.Extensions == nil { + graphqlErr.Extensions = make(map[string]interface{}) + } + // Add debugging information in development mode if devMode { if fieldCtx := graphql.GetFieldContext(ctx); fieldCtx != nil { @@ -119,12 +124,8 @@ func customErrorPresenter(ctx context.Context, e error, devMode bool) *gqlerror. } else { graphqlErr.Path = ast.Path{} } - graphqlErr.Extensions["system_error"] = systemError - } - // Ensure Extensions map exists - if graphqlErr.Extensions == nil { - graphqlErr.Extensions = make(map[string]interface{}) + graphqlErr.Extensions["system_error"] = systemError } return graphqlErr diff --git a/server/internal/app/graphql_test.go b/server/internal/app/graphql_test.go index a4cb3211b..f7584c002 100644 --- a/server/internal/app/graphql_test.go +++ b/server/internal/app/graphql_test.go @@ -46,7 +46,7 @@ func TestCustomErrorPresenter(t *testing.T) { assert.Equal(t, nil, graphqlErr.Extensions["system_error"]) }) - t.Run("Development mode with debugging information(Path)", func(t *testing.T) { + t.Run("Development mode with AppError", func(t *testing.T) { graphqlErr := customErrorPresenter(ctx, appErr, true) assert.NotNil(t, graphqlErr) @@ -55,4 +55,14 @@ func TestCustomErrorPresenter(t *testing.T) { assert.Equal(t, "test_code", graphqlErr.Extensions["code"]) assert.Equal(t, "Test description", graphqlErr.Extensions["description"]) }) + + t.Run("Development mode with default error", func(t *testing.T) { + defaultErr := errors.New("default error") + graphqlErr := customErrorPresenter(ctx, defaultErr, true) + + assert.NotNil(t, graphqlErr) + assert.Equal(t, "default error", graphqlErr.Message) + assert.Equal(t, defaultErr.Error(), graphqlErr.Extensions["system_error"]) + }) + }