Skip to content

Commit

Permalink
Mask problemsets and problemlist "sql no rows in result set" errors t…
Browse files Browse the repository at this point in the history
…o njudge.Error*
  • Loading branch information
mraron committed Aug 24, 2024
1 parent 16ed785 commit 5dff377
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
3 changes: 2 additions & 1 deletion internal/njudge/db/problemsets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
Expand Down
15 changes: 13 additions & 2 deletions internal/njudge/memory/problemlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,29 @@ 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"
"golang.org/x/text/transform"
"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
Expand Down Expand Up @@ -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){
Expand Down

0 comments on commit 5dff377

Please sign in to comment.