sync: Replace []error by go.uber.org/multierr

This commit is contained in:
nemunaire 2023-11-22 12:16:53 +01:00
parent 9f49a689fd
commit b6966d47ce
25 changed files with 380 additions and 348 deletions

View file

@ -12,6 +12,7 @@ import (
"strings"
"github.com/gin-gonic/gin"
"go.uber.org/multierr"
_ "golang.org/x/crypto/blake2b"
"srs.epita.fr/fic-server/libfic"
@ -23,10 +24,10 @@ type importHint struct {
FlagsDeps []int64
}
func buildExerciceHints(i Importer, exercice *fic.Exercice, exceptions *CheckExceptions) (hints []importHint, errs []error) {
func buildExerciceHints(i Importer, exercice *fic.Exercice, exceptions *CheckExceptions) (hints []importHint, errs error) {
params, _, err := parseExerciceParams(i, exercice.Path)
if err != nil {
errs = append(errs, NewChallengeTxtError(exercice, 0, err))
errs = multierr.Append(errs, NewChallengeTxtError(exercice, 0, err))
return
}
@ -45,10 +46,10 @@ func buildExerciceHints(i Importer, exercice *fic.Exercice, exceptions *CheckExc
if hint.Filename != "" {
if hint.Content != "" {
errs = append(errs, NewHintError(exercice, h, n, fmt.Errorf("content and filename can't be filled at the same time")))
errs = multierr.Append(errs, NewHintError(exercice, h, n, fmt.Errorf("content and filename can't be filled at the same time")))
continue
} else if !i.Exists(path.Join(exercice.Path, "hints", hint.Filename)) {
errs = append(errs, NewHintError(exercice, h, n, fmt.Errorf("%q: File not found", hint.Filename)))
errs = multierr.Append(errs, NewHintError(exercice, h, n, fmt.Errorf("%q: File not found", hint.Filename)))
continue
} else {
// Handle files as downloadable content
@ -70,35 +71,35 @@ func buildExerciceHints(i Importer, exercice *fic.Exercice, exceptions *CheckExc
// Special format for downloadable hints: $FILES + hexhash + path from FILES/
return "$FILES" + hex.EncodeToString(result512) + strings.TrimPrefix(filePath, fic.FilesDir), nil
}); err != nil {
errs = append(errs, NewHintError(exercice, h, n, fmt.Errorf("%q: unable to import hint file: %w", hint.Filename, err)))
errs = multierr.Append(errs, NewHintError(exercice, h, n, fmt.Errorf("%q: unable to import hint file: %w", hint.Filename, err)))
continue
} else if s, ok := res.(string); !ok {
errs = append(errs, NewHintError(exercice, h, n, fmt.Errorf("%q: unable to import hint file: invalid string returned as filename", hint.Filename)))
errs = multierr.Append(errs, NewHintError(exercice, h, n, fmt.Errorf("%q: unable to import hint file: invalid string returned as filename", hint.Filename)))
continue
} else {
h.Content = s
}
}
} else if hint.Content == "" {
errs = append(errs, NewHintError(exercice, h, n, fmt.Errorf("content and filename can't be empty at the same time")))
errs = multierr.Append(errs, NewHintError(exercice, h, n, fmt.Errorf("content and filename can't be empty at the same time")))
continue
} else {
// Call checks hooks
for _, hk := range hooks.mdTextHooks {
for _, err := range hk(h.Content, exercice.Language, exceptions) {
errs = append(errs, NewHintError(exercice, h, n, err))
for _, err := range multierr.Errors(hk(h.Content, exercice.Language, exceptions)) {
errs = multierr.Append(errs, NewHintError(exercice, h, n, err))
}
}
if h.Content, err = ProcessMarkdown(i, fixnbsp(hint.Content), exercice.Path); err != nil {
errs = append(errs, NewHintError(exercice, h, n, fmt.Errorf("error during markdown formating: %w", err)))
errs = multierr.Append(errs, NewHintError(exercice, h, n, fmt.Errorf("error during markdown formating: %w", err)))
}
}
// Call checks hooks
for _, hook := range hooks.hintHooks {
for _, e := range hook(h, exercice, exceptions) {
errs = append(errs, NewHintError(exercice, h, n, e))
for _, e := range multierr.Errors(hook(h, exercice, exceptions)) {
errs = multierr.Append(errs, NewHintError(exercice, h, n, e))
}
}
@ -119,28 +120,28 @@ func buildExerciceHints(i Importer, exercice *fic.Exercice, exceptions *CheckExc
}
// CheckExerciceHints checks if all hints are corrects..
func CheckExerciceHints(i Importer, exercice *fic.Exercice, exceptions *CheckExceptions) ([]importHint, []error) {
func CheckExerciceHints(i Importer, exercice *fic.Exercice, exceptions *CheckExceptions) ([]importHint, error) {
exceptions = exceptions.GetFileExceptions("challenge.txt", "challenge.toml")
return buildExerciceHints(i, exercice, exceptions)
}
// SyncExerciceHints reads the content of hints/ directories and import it as EHint for the given challenge.
func SyncExerciceHints(i Importer, exercice *fic.Exercice, flagsBindings map[int64]fic.Flag, exceptions *CheckExceptions) (hintsBindings map[int]*fic.EHint, errs []error) {
func SyncExerciceHints(i Importer, exercice *fic.Exercice, flagsBindings map[int64]fic.Flag, exceptions *CheckExceptions) (hintsBindings map[int]*fic.EHint, errs error) {
if _, err := exercice.WipeHints(); err != nil {
errs = append(errs, err)
errs = multierr.Append(errs, err)
} else {
exceptions = exceptions.GetFileExceptions("challenge.txt", "challenge.toml")
hints, berrs := buildExerciceHints(i, exercice, exceptions)
errs = append(errs, berrs...)
errs = multierr.Append(errs, berrs)
hintsBindings = map[int]*fic.EHint{}
for _, hint := range hints {
// Import hint
if h, err := exercice.AddHint(hint.Hint.Title, hint.Hint.Content, hint.Hint.Cost); err != nil {
errs = append(errs, NewHintError(exercice, hint.Hint, hint.Line, err))
errs = multierr.Append(errs, NewHintError(exercice, hint.Hint, hint.Line, err))
} else {
hintsBindings[hint.Line] = h
@ -148,10 +149,10 @@ func SyncExerciceHints(i Importer, exercice *fic.Exercice, flagsBindings map[int
for _, nf := range hint.FlagsDeps {
if f, ok := flagsBindings[nf]; ok {
if herr := h.AddDepend(f); herr != nil {
errs = append(errs, NewHintError(exercice, hint.Hint, hint.Line, fmt.Errorf("error hint dependency to flag #%d: %w", nf, herr)))
errs = multierr.Append(errs, NewHintError(exercice, hint.Hint, hint.Line, fmt.Errorf("error hint dependency to flag #%d: %w", nf, herr)))
}
} else {
errs = append(errs, NewHintError(exercice, hint.Hint, hint.Line, fmt.Errorf("error hint dependency to flag #%d: Unexistant flag", nf)))
errs = multierr.Append(errs, NewHintError(exercice, hint.Hint, hint.Line, fmt.Errorf("error hint dependency to flag #%d: Unexistant flag", nf)))
}
}
}