sync: Report custom errors

This commit is contained in:
nemunaire 2022-07-11 19:57:33 +02:00
commit c78545c18b
17 changed files with 510 additions and 137 deletions

View file

@ -19,19 +19,19 @@ import (
type importHint struct {
Line int
Hint fic.EHint
Hint *fic.EHint
FlagsDeps []int64
}
func buildExerciceHints(i Importer, exercice *fic.Exercice) (hints []importHint, errs []error) {
params, _, err := parseExerciceParams(i, exercice.Path)
if err != nil {
errs = append(errs, fmt.Errorf("%q: challenge.txt: %w", path.Base(exercice.Path), err))
errs = append(errs, NewChallengeTxtError(exercice, 0, err))
return
}
for n, hint := range params.Hints {
h := fic.EHint{}
h := &fic.EHint{}
if hint.Title == "" {
h.Title = fmt.Sprintf("Astuce #%d", n+1)
} else {
@ -45,10 +45,10 @@ func buildExerciceHints(i Importer, exercice *fic.Exercice) (hints []importHint,
if hint.Filename != "" {
if hint.Content != "" {
errs = append(errs, fmt.Errorf("%q: challenge.txt: hint %s (%d): content and filename can't be filled at the same time", path.Base(exercice.Path), hint.Title, n+1))
errs = 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, fmt.Errorf("%q: challenge.txt: hint %s (%d): %s: File not found", path.Base(exercice.Path), hint.Title, n+1, hint.Filename))
errs = append(errs, NewHintError(exercice, h, n, fmt.Errorf("%q: File not found", hint.Filename)))
continue
} else {
// Handle files as downloadable content
@ -70,20 +70,27 @@ func buildExerciceHints(i Importer, exercice *fic.Exercice) (hints []importHint,
// 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, fmt.Errorf("%q: unable to import hint file %q: %w", path.Base(exercice.Path), hint.Filename, err))
errs = 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, fmt.Errorf("%q: unable to import hint file %q: invalid string returned as filename", path.Base(exercice.Path), hint.Filename))
errs = 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, fmt.Errorf("%q: challenge.txt: hint %s (%d): content and filename can't be empty at the same time", path.Base(exercice.Path), hint.Title, n+1))
errs = append(errs, NewHintError(exercice, h, n, fmt.Errorf("content and filename can't be empty at the same time")))
continue
} else if h.Content, err = ProcessMarkdown(i, fixnbsp(hint.Content), exercice.Path); err != nil {
errs = append(errs, fmt.Errorf("%q: challenge.txt: hint %s (%d): error during markdown formating: %w", path.Base(exercice.Path), hint.Title, n+1, err))
errs = 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) {
errs = append(errs, NewHintError(exercice, h, n, e))
}
}
newHint := importHint{
@ -120,7 +127,7 @@ func SyncExerciceHints(i Importer, exercice *fic.Exercice, flagsBindings map[int
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, fmt.Errorf("%q: hint #%d %s: %w", path.Base(exercice.Path), hint.Line, hint.Hint.Title, err))
errs = append(errs, NewHintError(exercice, hint.Hint, hint.Line, err))
} else {
hintsBindings[hint.Line] = h
@ -128,10 +135,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, fmt.Errorf("%q: error hint #%d dependency to flag #%d: %w", path.Base(exercice.Path), hint.Line, nf, herr))
errs = append(errs, NewHintError(exercice, hint.Hint, hint.Line, fmt.Errorf("error hint dependency to flag #%d: %w", nf, herr)))
}
} else {
errs = append(errs, fmt.Errorf("%q: error hint #%d dependency to flag #%d: Unexistant flag", path.Base(exercice.Path), hint.Line, nf))
errs = append(errs, NewHintError(exercice, hint.Hint, hint.Line, fmt.Errorf("error hint dependency to flag #%d: Unexistant flag", nf)))
}
}
}