sync: Report custom errors

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

View file

@ -120,7 +120,7 @@ func BuildTheme(i Importer, tdir string) (th *fic.Theme, errs []error) {
th.URLId = fic.ToURLid(th.Name)
if authors, err := getAuthors(i, tdir); err != nil {
errs = append(errs, fmt.Errorf("unable to get AUTHORS.txt: %w", err))
errs = append(errs, NewThemeError(th, fmt.Errorf("unable to get AUTHORS.txt: %w", err)))
return nil, errs
} else {
// Format authors
@ -137,7 +137,7 @@ func BuildTheme(i Importer, tdir string) (th *fic.Theme, errs []error) {
err = fmt.Errorf("unable to find overview.txt nor overview.md")
}
if err != nil {
errs = append(errs, fmt.Errorf("unable to get theme's overview: %w", err))
errs = append(errs, NewThemeError(th, fmt.Errorf("unable to get theme's overview: %w", err)))
} else {
// Split headline from intro
ovrvw := strings.Split(fixnbsp(intro), "\n")
@ -149,12 +149,12 @@ func BuildTheme(i Importer, tdir string) (th *fic.Theme, errs []error) {
// Format overview (markdown)
th.Intro, err = ProcessMarkdown(i, intro, tdir)
if err != nil {
errs = append(errs, fmt.Errorf("overview.txt: an error occurs during markdown formating: %w", err))
errs = append(errs, NewThemeError(th, fmt.Errorf("overview.txt: an error occurs during markdown formating: %w", err)))
}
var buf bytes.Buffer
err := goldmark.Convert([]byte(th.Headline), &buf)
if err != nil {
errs = append(errs, fmt.Errorf("overview.txt: an error occurs during markdown formating of the headline: %w", err))
errs = append(errs, NewThemeError(th, fmt.Errorf("overview.txt: an error occurs during markdown formating of the headline: %w", err)))
} else {
th.Headline = string(buf.Bytes())
}
@ -165,7 +165,7 @@ func BuildTheme(i Importer, tdir string) (th *fic.Theme, errs []error) {
} else if i.exists(path.Join(tdir, "heading.png")) {
th.Image = path.Join(tdir, "heading.png")
} else {
errs = append(errs, fmt.Errorf("heading.jpg: No such file"))
errs = append(errs, NewThemeError(th, fmt.Errorf("heading.jpg: No such file")))
}
if i.exists(path.Join(tdir, "partner.jpg")) {
@ -176,11 +176,11 @@ func BuildTheme(i Importer, tdir string) (th *fic.Theme, errs []error) {
if i.exists(path.Join(tdir, "partner.txt")) {
if txt, err := GetFileContent(i, path.Join(tdir, "partner.txt")); err != nil {
errs = append(errs, fmt.Errorf("unable to get partner's text: %w", err))
errs = append(errs, NewThemeError(th, fmt.Errorf("unable to get partner's text: %w", err)))
} else {
th.PartnerText, err = ProcessMarkdown(i, txt, tdir)
if err != nil {
errs = append(errs, fmt.Errorf("partner.txt: an error occurs during markdown formating: %w", err))
errs = append(errs, NewThemeError(th, fmt.Errorf("partner.txt: an error occurs during markdown formating: %w", err)))
}
}
}
@ -199,7 +199,7 @@ func SyncThemes(i Importer) (errs []error) {
for _, tdir := range themes {
btheme, berrs := BuildTheme(i, tdir)
for _, e := range berrs {
errs = append(errs, fmt.Errorf("%q: %w", tdir, e))
errs = append(errs, e)
}
if btheme == nil {
@ -216,7 +216,7 @@ func SyncThemes(i Importer) (errs []error) {
btheme.Image = strings.TrimPrefix(filePath, fic.FilesDir)
return nil, nil
}); err != nil {
errs = append(errs, fmt.Errorf("%q: unable to import heading image: %w", tdir, err))
errs = append(errs, NewThemeError(btheme, fmt.Errorf("unable to import heading image: %w", err)))
}
}
@ -226,14 +226,14 @@ func SyncThemes(i Importer) (errs []error) {
btheme.PartnerImage = strings.TrimPrefix(filePath, fic.FilesDir)
return nil, nil
}); err != nil {
errs = append(errs, fmt.Errorf("%q: unable to import partner image: %w", tdir, err))
errs = append(errs, NewThemeError(btheme, fmt.Errorf("unable to import partner image: %w", err)))
}
}
var theme *fic.Theme
if theme, err = fic.GetThemeByPath(btheme.Path); err != nil {
if _, err := fic.CreateTheme(btheme); err != nil {
errs = append(errs, fmt.Errorf("%q: an error occurs during add: %w", tdir, err))
errs = append(errs, NewThemeError(btheme, fmt.Errorf("an error occurs during add: %w", err)))
continue
}
}
@ -241,7 +241,7 @@ func SyncThemes(i Importer) (errs []error) {
if !fic.CmpTheme(theme, btheme) {
btheme.Id = theme.Id
if _, err := btheme.Update(); err != nil {
errs = append(errs, fmt.Errorf("%q: an error occurs during update: %w", tdir, err))
errs = append(errs, NewThemeError(btheme, fmt.Errorf("an error occurs during update: %w", err)))
continue
}
}