sync: Use errors instead of string to report
This commit is contained in:
parent
d8943ba1f3
commit
b0129e5239
9 changed files with 186 additions and 178 deletions
|
@ -93,7 +93,7 @@ func parseExerciceDirname(edir string) (eid int, ename string, err error) {
|
|||
}
|
||||
|
||||
// BuildExercice creates an Exercice from a given importer.
|
||||
func BuildExercice(i Importer, theme *fic.Theme, epath string, dmap *map[int64]*fic.Exercice) (e *fic.Exercice, p ExerciceParams, eid int, edir string, errs []string) {
|
||||
func BuildExercice(i Importer, theme *fic.Theme, epath string, dmap *map[int64]*fic.Exercice) (e *fic.Exercice, p ExerciceParams, eid int, edir string, errs []error) {
|
||||
e = &fic.Exercice{}
|
||||
|
||||
e.Path = epath
|
||||
|
@ -102,7 +102,7 @@ func BuildExercice(i Importer, theme *fic.Theme, epath string, dmap *map[int64]*
|
|||
var err error
|
||||
eid, e.Title, err = parseExerciceDirname(edir)
|
||||
if err != nil {
|
||||
errs = append(errs, fmt.Sprintf("%q: unable to parse exercice directory: %s", edir, err))
|
||||
errs = append(errs, fmt.Errorf("unable to parse exercice directory: %w", err))
|
||||
return nil, p, eid, edir, errs
|
||||
}
|
||||
|
||||
|
@ -110,7 +110,7 @@ func BuildExercice(i Importer, theme *fic.Theme, epath string, dmap *map[int64]*
|
|||
if myTitle, err := GetFileContent(i, path.Join(epath, "title.txt")); err == nil {
|
||||
myTitle = strings.TrimSpace(myTitle)
|
||||
if strings.Contains(myTitle, "\n") {
|
||||
errs = append(errs, fmt.Sprintf("%q: title.txt: Title can't contain new lines", edir))
|
||||
errs = append(errs, fmt.Errorf("title.txt: Title can't contain new lines"))
|
||||
} else {
|
||||
e.Title = myTitle
|
||||
}
|
||||
|
@ -128,20 +128,20 @@ func BuildExercice(i Importer, theme *fic.Theme, epath string, dmap *map[int64]*
|
|||
err = fmt.Errorf("Unable to find overview.txt nor overview.md")
|
||||
}
|
||||
if err != nil {
|
||||
errs = append(errs, fmt.Sprintf("%q: overview.txt: %s", edir, err))
|
||||
errs = append(errs, fmt.Errorf("overview.txt: %s", err))
|
||||
} else {
|
||||
e.Overview = fixnbsp(e.Overview)
|
||||
|
||||
var buf bytes.Buffer
|
||||
err := goldmark.Convert([]byte(strings.Split(e.Overview, "\n")[0]), &buf)
|
||||
if err != nil {
|
||||
errs = append(errs, fmt.Sprintf("%q: overview.md: an error occurs during markdown formating of the headline: %s", edir, err))
|
||||
errs = append(errs, fmt.Errorf("overview.md: an error occurs during markdown formating of the headline: %w", err))
|
||||
} else {
|
||||
e.Headline = string(buf.Bytes())
|
||||
}
|
||||
|
||||
if e.Overview, err = ProcessMarkdown(i, e.Overview, epath); err != nil {
|
||||
errs = append(errs, fmt.Sprintf("%q: overview.md: an error occurs during markdown formating: %s", edir, err))
|
||||
errs = append(errs, fmt.Errorf("overview.md: an error occurs during markdown formating: %w", err))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -153,20 +153,20 @@ func BuildExercice(i Importer, theme *fic.Theme, epath string, dmap *map[int64]*
|
|||
err = fmt.Errorf("Unable to find statement.txt nor statement.md")
|
||||
}
|
||||
if err != nil {
|
||||
errs = append(errs, fmt.Sprintf("%q: statement.md: %s", edir, err))
|
||||
errs = append(errs, fmt.Errorf("statement.md: %w", err))
|
||||
} else {
|
||||
if e.Statement, err = ProcessMarkdown(i, fixnbsp(e.Statement), epath); err != nil {
|
||||
errs = append(errs, fmt.Sprintf("%q: statement.md: an error occurs during markdown formating: %s", edir, err))
|
||||
errs = append(errs, fmt.Errorf("statement.md: an error occurs during markdown formating: %w", err))
|
||||
}
|
||||
}
|
||||
|
||||
if i.exists(path.Join(epath, "finished.txt")) {
|
||||
e.Finished, err = GetFileContent(i, path.Join(epath, "finished.txt"))
|
||||
if err != nil {
|
||||
errs = append(errs, fmt.Sprintf("%q: finished.txt: %s", edir, err))
|
||||
errs = append(errs, fmt.Errorf("finished.txt: %w", err))
|
||||
} else {
|
||||
if e.Finished, err = ProcessMarkdown(i, e.Finished, epath); err != nil {
|
||||
errs = append(errs, fmt.Sprintf("%q: finished.txt: an error occurs during markdown formating: %s", edir, err))
|
||||
errs = append(errs, fmt.Errorf("finished.txt: an error occurs during markdown formating: %w", err))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -175,19 +175,19 @@ func BuildExercice(i Importer, theme *fic.Theme, epath string, dmap *map[int64]*
|
|||
var md toml.MetaData
|
||||
p, md, err = parseExerciceParams(i, epath)
|
||||
if err != nil {
|
||||
errs = append(errs, fmt.Sprintf("%q: %s", edir, err))
|
||||
errs = append(errs, fmt.Errorf("challenge.txt: %w", err))
|
||||
return
|
||||
}
|
||||
|
||||
// Alert about unknown keys in challenge.txt
|
||||
if len(md.Undecoded()) > 0 {
|
||||
for _, k := range md.Undecoded() {
|
||||
errs = append(errs, fmt.Sprintf("%q: challenge.txt: unknown key %q found, check https://srs.nemunai.re/fic/files/challenge/", edir, k))
|
||||
errs = append(errs, fmt.Errorf("challenge.txt: unknown key %q found, check https://srs.nemunai.re/fic/files/challenge/", k))
|
||||
}
|
||||
}
|
||||
|
||||
if p.Gain == 0 {
|
||||
errs = append(errs, fmt.Sprintf("%q: challenge.txt: Undefined gain for challenge", edir))
|
||||
errs = append(errs, fmt.Errorf("challenge.txt: Undefined gain for challenge"))
|
||||
} else {
|
||||
e.Gain = p.Gain
|
||||
}
|
||||
|
@ -195,11 +195,11 @@ func BuildExercice(i Importer, theme *fic.Theme, epath string, dmap *map[int64]*
|
|||
// Handle dependency
|
||||
if len(p.Dependencies) > 0 {
|
||||
if len(p.Dependencies[0].Theme) > 0 && p.Dependencies[0].Theme != theme.Name {
|
||||
errs = append(errs, fmt.Sprintf("%q: unable to treat dependency to another theme (%q): not implemented.", edir, p.Dependencies[0].Theme))
|
||||
errs = append(errs, fmt.Errorf("unable to treat dependency to another theme (%q): not implemented.", p.Dependencies[0].Theme))
|
||||
} else {
|
||||
if dmap == nil {
|
||||
if dmap2, err := buildDependancyMap(i, theme); err != nil {
|
||||
errs = append(errs, fmt.Sprintf("%q: unable to build dependency map: %s", edir, err))
|
||||
errs = append(errs, fmt.Errorf("unable to build dependency map: %w", err))
|
||||
} else {
|
||||
dmap = &dmap2
|
||||
}
|
||||
|
@ -217,7 +217,7 @@ func BuildExercice(i Importer, theme *fic.Theme, epath string, dmap *map[int64]*
|
|||
for k, _ := range *dmap {
|
||||
dmap_keys = append(dmap_keys, fmt.Sprintf("%d", k))
|
||||
}
|
||||
errs = append(errs, fmt.Sprintf("%q: Unable to find required exercice dependancy %d (available at time of processing: %s)", edir, p.Dependencies[0].Id, strings.Join(dmap_keys, ",")))
|
||||
errs = append(errs, fmt.Errorf("Unable to find required exercice dependancy %d (available at time of processing: %s)", p.Dependencies[0].Id, strings.Join(dmap_keys, ",")))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -230,10 +230,10 @@ func BuildExercice(i Importer, theme *fic.Theme, epath string, dmap *map[int64]*
|
|||
if !i.exists(e.VideoURI) {
|
||||
e.VideoURI = ""
|
||||
} else if size, err := getFileSize(i, e.VideoURI); err != nil {
|
||||
errs = append(errs, fmt.Sprintf("%q: resolution.mp4: ", edir, err))
|
||||
errs = append(errs, fmt.Errorf("resolution.mp4: %w", err))
|
||||
e.VideoURI = ""
|
||||
} else if size == 0 {
|
||||
errs = append(errs, fmt.Sprintf("%q: resolution.mp4: The file is empty!", edir))
|
||||
errs = append(errs, fmt.Errorf("resolution.mp4: The file is empty!"))
|
||||
e.VideoURI = ""
|
||||
} else {
|
||||
e.VideoURI = strings.Replace(url.PathEscape(path.Join("$RFILES$", e.VideoURI)), "%2F", "/", -1)
|
||||
|
@ -247,13 +247,13 @@ func BuildExercice(i Importer, theme *fic.Theme, epath string, dmap *map[int64]*
|
|||
|
||||
if i.exists(writeup) {
|
||||
if size, err := getFileSize(i, writeup); err != nil {
|
||||
errs = append(errs, fmt.Sprintf("%q: resolution.md: %s", edir, err.Error()))
|
||||
errs = append(errs, fmt.Errorf("resolution.md: %w", err))
|
||||
} else if size == 0 {
|
||||
errs = append(errs, fmt.Sprintf("%q: resolution.md: The file is empty!", edir))
|
||||
errs = append(errs, fmt.Errorf("resolution.md: The file is empty!"))
|
||||
} else if e.Resolution, err = GetFileContent(i, writeup); err != nil {
|
||||
errs = append(errs, fmt.Sprintf("%q: resolution.md: %s", edir, err.Error()))
|
||||
errs = append(errs, fmt.Errorf("resolution.md: %w", err))
|
||||
} else if e.Resolution, err = ProcessMarkdown(i, e.Resolution, epath); err != nil {
|
||||
errs = append(errs, fmt.Sprintf("%q: resolution.md: error during markdown processing: %s", edir, err.Error()))
|
||||
errs = append(errs, fmt.Errorf("resolution.md: error during markdown processing: %w", err))
|
||||
} else {
|
||||
resolutionFound = true
|
||||
}
|
||||
|
@ -263,7 +263,7 @@ func BuildExercice(i Importer, theme *fic.Theme, epath string, dmap *map[int64]*
|
|||
if LogMissingResolution {
|
||||
log.Printf("%q: no resolution video or text file found in %s", edir, epath, epath)
|
||||
} else {
|
||||
errs = append(errs, fmt.Sprintf("%q: no resolution video or text file found in %s", edir, epath))
|
||||
errs = append(errs, fmt.Errorf("no resolution video or text file found in %s", epath))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -271,28 +271,32 @@ func BuildExercice(i Importer, theme *fic.Theme, epath string, dmap *map[int64]*
|
|||
}
|
||||
|
||||
// SyncExercice imports new or updates existing given exercice.
|
||||
func SyncExercice(i Importer, theme *fic.Theme, epath string, dmap *map[int64]*fic.Exercice) (e *fic.Exercice, eid int, errs []string) {
|
||||
func SyncExercice(i Importer, theme *fic.Theme, epath string, dmap *map[int64]*fic.Exercice) (e *fic.Exercice, eid int, errs []error) {
|
||||
var err error
|
||||
var edir string
|
||||
var p ExerciceParams
|
||||
var berrors []error
|
||||
|
||||
e, p, eid, edir, errs = BuildExercice(i, theme, epath, dmap)
|
||||
e, p, eid, edir, berrors = BuildExercice(i, theme, epath, dmap)
|
||||
for _, e := range berrors {
|
||||
errs = append(errs, fmt.Errorf("%q: %w", edir, e))
|
||||
}
|
||||
|
||||
if e != nil {
|
||||
// Create or update the exercice
|
||||
err = theme.SaveNamedExercice(e)
|
||||
if err != nil {
|
||||
errs = append(errs, fmt.Sprintf("%q: error on exercice save: %s", edir, err))
|
||||
errs = append(errs, fmt.Errorf("%q: error on exercice save: %w", edir, err))
|
||||
return
|
||||
}
|
||||
|
||||
// Import eercice tags
|
||||
if _, err := e.WipeTags(); err != nil {
|
||||
errs = append(errs, fmt.Sprintf("%q: Unable to wipe tags: %s", edir, err))
|
||||
errs = append(errs, fmt.Errorf("%q: Unable to wipe tags: %w", edir, err))
|
||||
}
|
||||
for _, tag := range p.Tags {
|
||||
if _, err := e.AddTag(tag); err != nil {
|
||||
errs = append(errs, fmt.Sprintf("%q: Unable to add tag: %s", edir, err))
|
||||
errs = append(errs, fmt.Errorf("%q: Unable to add tag: %w", edir, err))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
@ -302,9 +306,9 @@ func SyncExercice(i Importer, theme *fic.Theme, epath string, dmap *map[int64]*f
|
|||
}
|
||||
|
||||
// SyncExercices imports new or updates existing exercices, in a given theme.
|
||||
func SyncExercices(i Importer, theme *fic.Theme) (errs []string) {
|
||||
func SyncExercices(i Importer, theme *fic.Theme) (errs []error) {
|
||||
if exercices, err := GetExercices(i, theme); err != nil {
|
||||
errs = append(errs, err.Error())
|
||||
errs = append(errs, err)
|
||||
} else {
|
||||
emap := map[string]int{}
|
||||
|
||||
|
|
Reference in a new issue