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
|
@ -21,9 +21,7 @@ import (
|
|||
)
|
||||
|
||||
// GetThemes returns all theme directories in the base directory.
|
||||
func GetThemes(i Importer) ([]string, error) {
|
||||
var themes []string
|
||||
|
||||
func GetThemes(i Importer) (themes []string, err error) {
|
||||
if dirs, err := i.listDir("/"); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
|
@ -106,7 +104,7 @@ func getAuthors(i Importer, tname string) ([]string, error) {
|
|||
}
|
||||
|
||||
// BuildTheme creates a Theme from a given importer.
|
||||
func BuildTheme(i Importer, tdir string) (th *fic.Theme, errs []string) {
|
||||
func BuildTheme(i Importer, tdir string) (th *fic.Theme, errs []error) {
|
||||
th = &fic.Theme{}
|
||||
|
||||
th.Path = tdir
|
||||
|
@ -122,7 +120,7 @@ func BuildTheme(i Importer, tdir string) (th *fic.Theme, errs []string) {
|
|||
th.URLId = fic.ToURLid(th.Name)
|
||||
|
||||
if authors, err := getAuthors(i, tdir); err != nil {
|
||||
errs = append(errs, fmt.Sprintf("%q: unable to get AUTHORS.txt: %s", th.Name, err))
|
||||
errs = append(errs, fmt.Errorf("unable to get AUTHORS.txt: %w", err))
|
||||
return nil, errs
|
||||
} else {
|
||||
// Format authors
|
||||
|
@ -139,7 +137,7 @@ func BuildTheme(i Importer, tdir string) (th *fic.Theme, errs []string) {
|
|||
err = fmt.Errorf("unable to find overview.txt nor overview.md")
|
||||
}
|
||||
if err != nil {
|
||||
errs = append(errs, fmt.Sprintf("%q: unable to get theme's overview: %s", th.Name, err))
|
||||
errs = append(errs, fmt.Errorf("unable to get theme's overview: %w", err))
|
||||
} else {
|
||||
// Split headline from intro
|
||||
ovrvw := strings.Split(fixnbsp(intro), "\n")
|
||||
|
@ -151,12 +149,12 @@ func BuildTheme(i Importer, tdir string) (th *fic.Theme, errs []string) {
|
|||
// Format overview (markdown)
|
||||
th.Intro, err = ProcessMarkdown(i, intro, tdir)
|
||||
if err != nil {
|
||||
errs = append(errs, fmt.Sprintf("%q: overview.txt: an error occurs during markdown formating: %s", tdir, err))
|
||||
errs = append(errs, 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.Sprintf("%q: overview.txt: an error occurs during markdown formating of the headline: %s", tdir, err))
|
||||
errs = append(errs, fmt.Errorf("overview.txt: an error occurs during markdown formating of the headline: %w", err))
|
||||
} else {
|
||||
th.Headline = string(buf.Bytes())
|
||||
}
|
||||
|
@ -167,7 +165,7 @@ func BuildTheme(i Importer, tdir string) (th *fic.Theme, errs []string) {
|
|||
} else if i.exists(path.Join(tdir, "heading.png")) {
|
||||
th.Image = path.Join(tdir, "heading.png")
|
||||
} else {
|
||||
errs = append(errs, fmt.Sprintf("%q: heading.jpg: No such file", tdir))
|
||||
errs = append(errs, fmt.Errorf("heading.jpg: No such file"))
|
||||
}
|
||||
|
||||
if i.exists(path.Join(tdir, "partner.jpg")) {
|
||||
|
@ -178,11 +176,11 @@ func BuildTheme(i Importer, tdir string) (th *fic.Theme, errs []string) {
|
|||
|
||||
if i.exists(path.Join(tdir, "partner.txt")) {
|
||||
if txt, err := GetFileContent(i, path.Join(tdir, "partner.txt")); err != nil {
|
||||
errs = append(errs, fmt.Sprintf("%q: unable to get partner's text: %s", th.Name, err))
|
||||
errs = append(errs, 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.Sprintf("%q: partner.txt: an error occurs during markdown formating: %s", tdir, err))
|
||||
errs = append(errs, fmt.Errorf("partner.txt: an error occurs during markdown formating: %w", err))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -190,9 +188,9 @@ func BuildTheme(i Importer, tdir string) (th *fic.Theme, errs []string) {
|
|||
}
|
||||
|
||||
// SyncThemes imports new or updates existing themes.
|
||||
func SyncThemes(i Importer) (errs []string) {
|
||||
func SyncThemes(i Importer) (errs []error) {
|
||||
if themes, err := GetThemes(i); err != nil {
|
||||
errs = append(errs, err.Error())
|
||||
errs = append(errs, fmt.Errorf("Unable to list themes: %w", err.Error()))
|
||||
} else {
|
||||
rand.Shuffle(len(themes), func(i, j int) {
|
||||
themes[i], themes[j] = themes[j], themes[i]
|
||||
|
@ -200,7 +198,9 @@ func SyncThemes(i Importer) (errs []string) {
|
|||
|
||||
for _, tdir := range themes {
|
||||
btheme, berrs := BuildTheme(i, tdir)
|
||||
errs = append(errs, berrs...)
|
||||
for _, e := range berrs {
|
||||
errs = append(errs, fmt.Errorf("%q: %w", tdir, e))
|
||||
}
|
||||
|
||||
if btheme == nil {
|
||||
continue
|
||||
|
@ -216,7 +216,7 @@ func SyncThemes(i Importer) (errs []string) {
|
|||
btheme.Image = strings.TrimPrefix(filePath, fic.FilesDir)
|
||||
return nil, nil
|
||||
}); err != nil {
|
||||
errs = append(errs, fmt.Sprintf("%q: unable to import heading image: %s", tdir, err))
|
||||
errs = append(errs, fmt.Errorf("%q: unable to import heading image: %w", tdir, err))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -226,14 +226,14 @@ func SyncThemes(i Importer) (errs []string) {
|
|||
btheme.PartnerImage = strings.TrimPrefix(filePath, fic.FilesDir)
|
||||
return nil, nil
|
||||
}); err != nil {
|
||||
errs = append(errs, fmt.Sprintf("%q: unable to import partner image: %s", tdir, err))
|
||||
errs = append(errs, fmt.Errorf("%q: unable to import partner image: %w", tdir, 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.Sprintf("%q: an error occurs during add: %s", tdir, err))
|
||||
errs = append(errs, fmt.Errorf("%q: an error occurs during add: %w", tdir, err))
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
@ -241,7 +241,7 @@ func SyncThemes(i Importer) (errs []string) {
|
|||
if !fic.CmpTheme(theme, btheme) {
|
||||
btheme.Id = theme.Id
|
||||
if _, err := btheme.Update(); err != nil {
|
||||
errs = append(errs, fmt.Sprintf("%q: an error occurs during update: %s", tdir, err))
|
||||
errs = append(errs, fmt.Errorf("%q: an error occurs during update: %w", tdir, err))
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue