sync: Replace []error by go.uber.org/multierr
This commit is contained in:
parent
9f49a689fd
commit
b6966d47ce
25 changed files with 380 additions and 348 deletions
|
@ -15,6 +15,7 @@ import (
|
|||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/yuin/goldmark"
|
||||
"go.uber.org/multierr"
|
||||
"golang.org/x/image/draw"
|
||||
|
||||
"srs.epita.fr/fic-server/libfic"
|
||||
|
@ -104,7 +105,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, exceptions *CheckExceptions, errs []error) {
|
||||
func BuildTheme(i Importer, tdir string) (th *fic.Theme, exceptions *CheckExceptions, errs error) {
|
||||
th = &fic.Theme{}
|
||||
|
||||
th.Path = tdir
|
||||
|
@ -116,7 +117,7 @@ func BuildTheme(i Importer, tdir string) (th *fic.Theme, exceptions *CheckExcept
|
|||
if language, err := GetFileContent(i, path.Join(tdir, "language.txt")); err == nil {
|
||||
language = strings.TrimSpace(language)
|
||||
if strings.Contains(language, "\n") {
|
||||
errs = append(errs, NewThemeError(th, fmt.Errorf("language.txt: Language can't contain new lines")))
|
||||
errs = multierr.Append(errs, NewThemeError(th, fmt.Errorf("language.txt: Language can't contain new lines")))
|
||||
} else {
|
||||
th.Language = language
|
||||
}
|
||||
|
@ -133,7 +134,7 @@ func BuildTheme(i Importer, tdir string) (th *fic.Theme, exceptions *CheckExcept
|
|||
th.URLId = fic.ToURLid(th.Name)
|
||||
|
||||
if authors, err := getAuthors(i, tdir); err != nil {
|
||||
errs = append(errs, NewThemeError(th, fmt.Errorf("unable to get AUTHORS.txt: %w", err)))
|
||||
errs = multierr.Append(errs, NewThemeError(th, fmt.Errorf("unable to get AUTHORS.txt: %w", err)))
|
||||
return nil, nil, errs
|
||||
} else {
|
||||
// Format authors
|
||||
|
@ -147,13 +148,13 @@ func BuildTheme(i Importer, tdir string) (th *fic.Theme, exceptions *CheckExcept
|
|||
th.Headline, err = GetFileContent(i, path.Join(tdir, "headline.md"))
|
||||
}
|
||||
if err != nil {
|
||||
errs = append(errs, NewThemeError(th, fmt.Errorf("unable to get theme's headline: %w", err)))
|
||||
errs = multierr.Append(errs, NewThemeError(th, fmt.Errorf("unable to get theme's headline: %w", err)))
|
||||
}
|
||||
if th.Headline != "" {
|
||||
// Call checks hooks
|
||||
for _, h := range hooks.mdTextHooks {
|
||||
for _, err := range h(th.Headline, th.Language, exceptions.GetFileExceptions("headline.md", "headline.txt")) {
|
||||
errs = append(errs, NewThemeError(th, fmt.Errorf("headline.md: %w", err)))
|
||||
for _, err := range multierr.Errors(h(th.Headline, th.Language, exceptions.GetFileExceptions("headline.md", "headline.txt"))) {
|
||||
errs = multierr.Append(errs, NewThemeError(th, fmt.Errorf("headline.md: %w", err)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -167,12 +168,12 @@ func BuildTheme(i Importer, tdir string) (th *fic.Theme, exceptions *CheckExcept
|
|||
err = fmt.Errorf("unable to find overview.txt nor overview.md")
|
||||
}
|
||||
if err != nil {
|
||||
errs = append(errs, NewThemeError(th, fmt.Errorf("unable to get theme's overview: %w", err)))
|
||||
errs = multierr.Append(errs, NewThemeError(th, fmt.Errorf("unable to get theme's overview: %w", err)))
|
||||
} else {
|
||||
// Call checks hooks
|
||||
for _, h := range hooks.mdTextHooks {
|
||||
for _, err := range h(intro, th.Language, exceptions.GetFileExceptions("overview.md", "overview.txt")) {
|
||||
errs = append(errs, NewThemeError(th, fmt.Errorf("overview.md: %w", err)))
|
||||
for _, err := range multierr.Errors(h(intro, th.Language, exceptions.GetFileExceptions("overview.md", "overview.txt"))) {
|
||||
errs = multierr.Append(errs, NewThemeError(th, fmt.Errorf("overview.md: %w", err)))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -188,12 +189,12 @@ func BuildTheme(i Importer, tdir string) (th *fic.Theme, exceptions *CheckExcept
|
|||
// Format overview (markdown)
|
||||
th.Intro, err = ProcessMarkdown(i, intro, tdir)
|
||||
if err != nil {
|
||||
errs = append(errs, NewThemeError(th, fmt.Errorf("overview.txt: an error occurs during markdown formating: %w", err)))
|
||||
errs = multierr.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, NewThemeError(th, fmt.Errorf("overview.txt: an error occurs during markdown formating of the headline: %w", err)))
|
||||
errs = multierr.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())
|
||||
}
|
||||
|
@ -204,7 +205,7 @@ func BuildTheme(i Importer, tdir string) (th *fic.Theme, exceptions *CheckExcept
|
|||
} else if i.Exists(path.Join(tdir, "heading.png")) {
|
||||
th.Image = path.Join(tdir, "heading.png")
|
||||
} else {
|
||||
errs = append(errs, NewThemeError(th, fmt.Errorf("heading.jpg: No such file")))
|
||||
errs = multierr.Append(errs, NewThemeError(th, fmt.Errorf("heading.jpg: No such file")))
|
||||
}
|
||||
|
||||
if i.Exists(path.Join(tdir, "partner.jpg")) {
|
||||
|
@ -215,11 +216,11 @@ func BuildTheme(i Importer, tdir string) (th *fic.Theme, exceptions *CheckExcept
|
|||
|
||||
if i.Exists(path.Join(tdir, "partner.txt")) {
|
||||
if txt, err := GetFileContent(i, path.Join(tdir, "partner.txt")); err != nil {
|
||||
errs = append(errs, NewThemeError(th, fmt.Errorf("unable to get partner's text: %w", err)))
|
||||
errs = multierr.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, NewThemeError(th, fmt.Errorf("partner.txt: an error occurs during markdown formating: %w", err)))
|
||||
errs = multierr.Append(errs, NewThemeError(th, fmt.Errorf("partner.txt: an error occurs during markdown formating: %w", err)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -227,9 +228,9 @@ func BuildTheme(i Importer, tdir string) (th *fic.Theme, exceptions *CheckExcept
|
|||
}
|
||||
|
||||
// SyncThemes imports new or updates existing themes.
|
||||
func SyncThemes(i Importer) (exceptions map[string]*CheckExceptions, errs []error) {
|
||||
func SyncThemes(i Importer) (exceptions map[string]*CheckExceptions, errs error) {
|
||||
if themes, err := GetThemes(i); err != nil {
|
||||
errs = append(errs, fmt.Errorf("Unable to list themes: %w", err))
|
||||
errs = multierr.Append(errs, fmt.Errorf("Unable to list themes: %w", err))
|
||||
} else {
|
||||
rand.Shuffle(len(themes), func(i, j int) {
|
||||
themes[i], themes[j] = themes[j], themes[i]
|
||||
|
@ -239,9 +240,7 @@ func SyncThemes(i Importer) (exceptions map[string]*CheckExceptions, errs []erro
|
|||
|
||||
for _, tdir := range themes {
|
||||
btheme, excepts, berrs := BuildTheme(i, tdir)
|
||||
for _, e := range berrs {
|
||||
errs = append(errs, e)
|
||||
}
|
||||
errs = multierr.Append(errs, berrs)
|
||||
|
||||
if btheme == nil {
|
||||
continue
|
||||
|
@ -259,7 +258,7 @@ func SyncThemes(i Importer) (exceptions map[string]*CheckExceptions, errs []erro
|
|||
btheme.Image = strings.TrimPrefix(filePath, fic.FilesDir)
|
||||
return nil, nil
|
||||
}); err != nil {
|
||||
errs = append(errs, NewThemeError(btheme, fmt.Errorf("unable to import heading image: %w", err)))
|
||||
errs = multierr.Append(errs, NewThemeError(btheme, fmt.Errorf("unable to import heading image: %w", err)))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -269,14 +268,14 @@ func SyncThemes(i Importer) (exceptions map[string]*CheckExceptions, errs []erro
|
|||
btheme.PartnerImage = strings.TrimPrefix(filePath, fic.FilesDir)
|
||||
return nil, nil
|
||||
}); err != nil {
|
||||
errs = append(errs, NewThemeError(btheme, fmt.Errorf("unable to import partner image: %w", err)))
|
||||
errs = multierr.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, NewThemeError(btheme, fmt.Errorf("an error occurs during add: %w", err)))
|
||||
errs = multierr.Append(errs, NewThemeError(btheme, fmt.Errorf("an error occurs during add: %w", err)))
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
@ -284,7 +283,7 @@ func SyncThemes(i Importer) (exceptions map[string]*CheckExceptions, errs []erro
|
|||
if !fic.CmpTheme(theme, btheme) {
|
||||
btheme.Id = theme.Id
|
||||
if _, err := btheme.Update(); err != nil {
|
||||
errs = append(errs, NewThemeError(btheme, fmt.Errorf("an error occurs during update: %w", err)))
|
||||
errs = multierr.Append(errs, NewThemeError(btheme, fmt.Errorf("an error occurs during update: %w", err)))
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue