Rename locale.MustGetAll to GetAll and return error also

Since app.New returns error too, there is no need for locale.GetAll to
panic on error; main will log and close the application.
This commit is contained in:
jordi fita mas 2023-08-06 04:08:07 +02:00
parent ca4ecd3a3e
commit e3138652fa
2 changed files with 15 additions and 9 deletions

View File

@ -29,7 +29,10 @@ type App struct {
} }
func New(db *database.DB, avatarsDir string) (http.Handler, error) { func New(db *database.DB, avatarsDir string) (http.Handler, error) {
locales := locale.MustGetAll(context.Background(), db) locales, err := locale.GetAll(context.Background(), db)
if err != nil {
return nil, err
}
static := http.FileServer(http.Dir("web/static")) static := http.FileServer(http.Dir("web/static"))
profile, err := newProfileHandler(static, avatarsDir) profile, err := newProfileHandler(static, avatarsDir)
if err != nil { if err != nil {

View File

@ -31,15 +31,18 @@ func (m Locales) Tags() []language.Tag {
return keys return keys
} }
func MustGetAll(ctx context.Context, db *database.DB) Locales { func GetAll(ctx context.Context, db *database.DB) (Locales, error) {
availableLanguages := mustGetAvailableLanguages(ctx, db) availableLanguages, err := getAvailableLanguages(ctx, db)
if err != nil {
return nil, err
}
locales := map[language.Tag]*Locale{} locales := map[language.Tag]*Locale{}
for _, lang := range availableLanguages { for _, lang := range availableLanguages {
locale := newLocale(lang) locale := newLocale(lang)
locale.AddDomain("camper") locale.AddDomain("camper")
locales[lang.tag] = locale locales[lang.tag] = locale
} }
return locales return locales, nil
} }
func newLocale(lang availableLanguage) *Locale { func newLocale(lang availableLanguage) *Locale {
@ -85,10 +88,10 @@ type availableLanguage struct {
currencyPattern string currencyPattern string
} }
func mustGetAvailableLanguages(ctx context.Context, db *database.DB) []availableLanguage { func getAvailableLanguages(ctx context.Context, db *database.DB) ([]availableLanguage, error) {
rows, err := db.Query(ctx, "select lang_tag, currency_pattern from language where selectable") rows, err := db.Query(ctx, "select lang_tag, currency_pattern from language where selectable")
if err != nil { if err != nil {
panic(err) return nil, err
} }
defer rows.Close() defer rows.Close()
@ -98,13 +101,13 @@ func mustGetAvailableLanguages(ctx context.Context, db *database.DB) []available
var currencyPattern string var currencyPattern string
err = rows.Scan(&langTag, &currencyPattern) err = rows.Scan(&langTag, &currencyPattern)
if err != nil { if err != nil {
panic(err) return nil, err
} }
languages = append(languages, availableLanguage{language.MustParse(langTag), currencyPattern}) languages = append(languages, availableLanguage{language.MustParse(langTag), currencyPattern})
} }
if rows.Err() != nil { if rows.Err() != nil {
panic(rows.Err()) return nil, rows.Err()
} }
return languages return languages, nil
} }