From 5dff377d4bd918ad9df46c36a4f7453278505621 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81ron=20Nosz=C3=A1ly?= Date: Sat, 24 Aug 2024 14:32:46 +0200 Subject: [PATCH] Mask problemsets and problemlist "sql no rows in result set" errors to njudge.Error* --- internal/njudge/db/problemsets.go | 3 ++- internal/njudge/memory/problemlist.go | 15 +++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/internal/njudge/db/problemsets.go b/internal/njudge/db/problemsets.go index 4ff6e41..d43804e 100644 --- a/internal/njudge/db/problemsets.go +++ b/internal/njudge/db/problemsets.go @@ -2,6 +2,7 @@ package db import ( "database/sql" + "github.com/mraron/njudge/internal/njudge" "github.com/mraron/njudge/internal/njudge/db/models" "golang.org/x/net/context" @@ -25,7 +26,7 @@ func (p Problemsets) toNjudge(ps *models.Problemset) *njudge.Problemset { func (p Problemsets) GetByName(ctx context.Context, problemsetName string) (*njudge.Problemset, error) { res, err := models.Problemsets(models.ProblemsetWhere.Name.EQ(problemsetName)).One(ctx, p.db) if err != nil { - return nil, err + return nil, MaskNotFoundError(err, njudge.ErrorProblemsetNotFound) } return p.toNjudge(res), nil } diff --git a/internal/njudge/memory/problemlist.go b/internal/njudge/memory/problemlist.go index ef48ee3..5b64715 100644 --- a/internal/njudge/memory/problemlist.go +++ b/internal/njudge/memory/problemlist.go @@ -2,11 +2,14 @@ package memory import ( "context" - "github.com/mraron/njudge/internal/web/templates/i18n" + "database/sql" + "errors" "sort" "strings" "unicode" + "github.com/mraron/njudge/internal/web/templates/i18n" + "github.com/mraron/njudge/internal/njudge" "github.com/mraron/njudge/pkg/problems" "golang.org/x/text/runes" @@ -14,6 +17,14 @@ import ( "golang.org/x/text/unicode/norm" ) +func MaskNotFoundError(err, mask error) error { + if errors.Is(err, sql.ErrNoRows) { + return mask + } + + return err +} + type ProblemListQuery struct { store problems.Store ps njudge.Problems @@ -145,7 +156,7 @@ func (p *ProblemListQuery) filterAuthor(ctx context.Context, req njudge.ProblemL func (p *ProblemListQuery) GetProblemList(ctx context.Context, req njudge.ProblemListRequest) (*njudge.ProblemList, error) { allProblems, err := p.ps.GetAll(ctx) if err != nil { - return nil, err + return nil, MaskNotFoundError(err, nil) } filters := []func(context.Context, njudge.ProblemListRequest, njudge.Problem) (bool, error){