sync: Use errors instead of string to report

This commit is contained in:
nemunaire 2022-07-02 00:01:05 +02:00
commit b0129e5239
9 changed files with 186 additions and 178 deletions

View file

@ -23,10 +23,10 @@ type importHint struct {
FlagsDeps []int64
}
func buildExerciceHints(i Importer, exercice *fic.Exercice) (hints []importHint, errs []string) {
func buildExerciceHints(i Importer, exercice *fic.Exercice) (hints []importHint, errs []error) {
params, _, err := parseExerciceParams(i, exercice.Path)
if err != nil {
errs = append(errs, fmt.Sprintf("%q: challenge.txt: %s", path.Base(exercice.Path), err))
errs = append(errs, fmt.Errorf("%q: challenge.txt: %w", path.Base(exercice.Path), err))
return
}
@ -45,10 +45,10 @@ func buildExerciceHints(i Importer, exercice *fic.Exercice) (hints []importHint,
if hint.Filename != "" {
if hint.Content != "" {
errs = append(errs, fmt.Sprintf("%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, 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))
continue
} else if !i.exists(path.Join(exercice.Path, "hints", hint.Filename)) {
errs = append(errs, fmt.Sprintf("%q: challenge.txt: hint %s (%d): %s: File not found", path.Base(exercice.Path), hint.Title, n+1, 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))
continue
} else {
// Handle files as downloadable content
@ -70,20 +70,20 @@ 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.Sprintf("%q: unable to import hint file %q: %s", path.Base(exercice.Path), hint.Filename, err))
errs = append(errs, fmt.Errorf("%q: unable to import hint file %q: %w", path.Base(exercice.Path), hint.Filename, err))
continue
} else if s, ok := res.(string); !ok {
errs = append(errs, fmt.Sprintf("%q: unable to import hint file %q: invalid string returned as filename", path.Base(exercice.Path), hint.Filename))
errs = append(errs, fmt.Errorf("%q: unable to import hint file %q: invalid string returned as filename", path.Base(exercice.Path), hint.Filename))
continue
} else {
h.Content = s
}
}
} else if hint.Content == "" {
errs = append(errs, fmt.Sprintf("%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, 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))
continue
} else if h.Content, err = ProcessMarkdown(i, fixnbsp(hint.Content), exercice.Path); err != nil {
errs = append(errs, fmt.Sprintf("%q: challenge.txt: hint %s (%d): error during markdown formating: %s", path.Base(exercice.Path), hint.Title, n+1, err))
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))
}
newHint := importHint{
@ -103,14 +103,14 @@ func buildExerciceHints(i Importer, exercice *fic.Exercice) (hints []importHint,
}
// CheckExerciceHints checks if all hints are corrects..
func CheckExerciceHints(i Importer, exercice *fic.Exercice) ([]importHint, []string) {
func CheckExerciceHints(i Importer, exercice *fic.Exercice) ([]importHint, []error) {
return buildExerciceHints(i, exercice)
}
// 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) (hintsBindings map[int]*fic.EHint, errs []string) {
func SyncExerciceHints(i Importer, exercice *fic.Exercice, flagsBindings map[int64]fic.Flag) (hintsBindings map[int]*fic.EHint, errs []error) {
if _, err := exercice.WipeHints(); err != nil {
errs = append(errs, err.Error())
errs = append(errs, err)
} else {
hints, berrs := buildExerciceHints(i, exercice)
errs = append(errs, berrs...)
@ -120,7 +120,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.Sprintf("%q: hint #%d %s: %s", path.Base(exercice.Path), hint.Line, hint.Hint.Title, err))
errs = append(errs, fmt.Errorf("%q: hint #%d %s: %w", path.Base(exercice.Path), hint.Line, hint.Hint.Title, err))
} else {
hintsBindings[hint.Line] = h
@ -128,10 +128,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.Sprintf("%q: error hint #%d dependency to flag #%d: %s", path.Base(exercice.Path), hint.Line, nf, herr))
errs = append(errs, fmt.Errorf("%q: error hint #%d dependency to flag #%d: %w", path.Base(exercice.Path), hint.Line, nf, herr))
}
} else {
errs = append(errs, fmt.Sprintf("%q: error hint #%d dependency to flag #%d: Unexistant flag", path.Base(exercice.Path), hint.Line, nf))
errs = append(errs, fmt.Errorf("%q: error hint #%d dependency to flag #%d: Unexistant flag", path.Base(exercice.Path), hint.Line, nf))
}
}
}