Skip to content

Commit

Permalink
fix: test
Browse files Browse the repository at this point in the history
  • Loading branch information
soneda-yuya committed Jan 12, 2025
1 parent 3a2bcdc commit 9f0d21a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
3 changes: 3 additions & 0 deletions server/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 ./...

Expand Down
11 changes: 6 additions & 5 deletions server/internal/app/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,20 @@ 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 {
graphqlErr.Path = fieldCtx.Path()
} 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
Expand Down
12 changes: 11 additions & 1 deletion server/internal/app/graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"])
})

}

0 comments on commit 9f0d21a

Please sign in to comment.