feat: enhance error handling in ListGroupNodes with detailed query diagnostics

This commit is contained in:
robinj0726 2026-10-28 16:54:13 +08:00
parent deaf35e55c
commit 7ee9497cfa
2 changed files with 46 additions and 7 deletions

View File

@ -4313,6 +4313,16 @@ ORDER BY level ASC, sort_num ASC, id ASC
return categories, nil
}
// GroupListQueryError retains the executed statement for server-side diagnostics.
type GroupListQueryError struct {
Query string
Args []any
Err error
}
func (e *GroupListQueryError) Error() string { return fmt.Sprintf("query group nodes: %v", e.Err) }
func (e *GroupListQueryError) Unwrap() error { return e.Err }
// ListGroupNodes loads one page of product group nodes and their leaf-brand bindings.
func (r *ProductRepository) ListGroupNodes(ctx context.Context, filter GroupListFilter, page, pageSize int32, scope StoreScope) ([]*CategoryRecord, int64, error) {
if err := requireWriteScope(scope); err != nil {
@ -4342,11 +4352,11 @@ WHERE c.node_type = 2
AND ($2 = false OR c.status = 1)
AND ($3 = '' OR CASE
WHEN $5 THEN LOWER(CASE WHEN c.level = 1 THEN c.name ELSE COALESCE(parent.name, '') END) = LOWER($3)
ELSE CASE WHEN c.level = 1 THEN c.name ELSE COALESCE(parent.name, '') END ILIKE ('%' || $3 || '%') ESCAPE '\\'
ELSE CASE WHEN c.level = 1 THEN c.name ELSE COALESCE(parent.name, '') END ILIKE ('%' || $3 || '%') ESCAPE '\'
END)
AND ($4 = '' OR (c.level = 2 AND CASE
WHEN $5 THEN LOWER(c.name) = LOWER($4)
ELSE c.name ILIKE ('%' || $4 || '%') ESCAPE '\\'
ELSE c.name ILIKE ('%' || $4 || '%') ESCAPE '\'
END))
`, scope.StoreID, filter.OnlyVisible, firstLevelName, secondLevelName, filter.ExactMatch).Scan(&total); err != nil {
return nil, 0, fmt.Errorf("count group nodes: %w", err)
@ -4356,7 +4366,7 @@ WHERE c.node_type = 2
}
offset := int64(page-1) * int64(pageSize)
rows, err := r.db.QueryContext(ctx, `
const query = `
SELECT c.id,
c.node_type,
c.name,
@ -4395,11 +4405,13 @@ WHERE c.node_type = 2
ELSE c.name ILIKE ('%' || $4 || '%') ESCAPE '\'
END))
GROUP BY c.id
ORDER BY c.created_at DESC, c.id DESC
ORDER BY c.id DESC
LIMIT $6 OFFSET $7
`, scope.StoreID, filter.OnlyVisible, firstLevelName, secondLevelName, filter.ExactMatch, pageSize, offset)
`
args := []any{scope.StoreID, filter.OnlyVisible, firstLevelName, secondLevelName, filter.ExactMatch, pageSize, offset}
rows, err := r.db.QueryContext(ctx, query, args...)
if err != nil {
return nil, 0, fmt.Errorf("query group nodes: %w", err)
return nil, 0, &GroupListQueryError{Query: query, Args: args, Err: err}
}
defer rows.Close()

View File

@ -12,6 +12,7 @@ import (
"time"
"unicode/utf8"
"github.com/jackc/pgx/v5/pgconn"
"go.opentelemetry.io/otel/attribute"
otelcodes "go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
@ -1499,7 +1500,33 @@ func (s *ProductServer) ListGroups(ctx context.Context, req *productv1.ListGroup
if err != nil {
dbSpan.RecordError(err)
dbSpan.SetStatus(otelcodes.Error, err.Error())
s.observer.Logger(ctx).Error("list groups failed", zap.Bool("only_visible", onlyVisible), zap.Error(err))
fields := []zap.Field{
zap.Int64("tenant_id", scope.TenantID),
zap.Int64("store_id", scope.StoreID),
zap.Bool("only_visible", onlyVisible),
zap.String("first_level_name", filter.FirstLevelName),
zap.String("second_level_name", filter.SecondLevelName),
zap.Bool("exact_match", filter.ExactMatch),
zap.Int32("page", page),
zap.Int32("page_size", pageSize),
zap.Error(err),
}
var queryErr *repository.GroupListQueryError
if errors.As(err, &queryErr) {
fields = append(fields, zap.String("sql_query", queryErr.Query), zap.Any("sql_args", queryErr.Args))
}
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
fields = append(fields,
zap.String("sqlstate", pgErr.Code),
zap.Int32("sql_position", pgErr.Position),
zap.String("pg_detail", pgErr.Detail),
zap.String("pg_where", pgErr.Where),
zap.String("pg_internal_query", pgErr.InternalQuery),
zap.Int32("pg_internal_position", pgErr.InternalPosition),
)
}
s.observer.Logger(dbCtx).Error("list groups failed", fields...)
return nil, status.Errorf(codes.Internal, "list groups: %v", err)
}
dbSpan.SetAttributes(