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:
parent
ca4ecd3a3e
commit
e3138652fa
|
@ -29,7 +29,10 @@ type App struct {
|
|||
}
|
||||
|
||||
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"))
|
||||
profile, err := newProfileHandler(static, avatarsDir)
|
||||
if err != nil {
|
||||
|
|
|
@ -31,15 +31,18 @@ func (m Locales) Tags() []language.Tag {
|
|||
return keys
|
||||
}
|
||||
|
||||
func MustGetAll(ctx context.Context, db *database.DB) Locales {
|
||||
availableLanguages := mustGetAvailableLanguages(ctx, db)
|
||||
func GetAll(ctx context.Context, db *database.DB) (Locales, error) {
|
||||
availableLanguages, err := getAvailableLanguages(ctx, db)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
locales := map[language.Tag]*Locale{}
|
||||
for _, lang := range availableLanguages {
|
||||
locale := newLocale(lang)
|
||||
locale.AddDomain("camper")
|
||||
locales[lang.tag] = locale
|
||||
}
|
||||
return locales
|
||||
return locales, nil
|
||||
}
|
||||
|
||||
func newLocale(lang availableLanguage) *Locale {
|
||||
|
@ -85,10 +88,10 @@ type availableLanguage struct {
|
|||
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")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
|
@ -98,13 +101,13 @@ func mustGetAvailableLanguages(ctx context.Context, db *database.DB) []available
|
|||
var currencyPattern string
|
||||
err = rows.Scan(&langTag, ¤cyPattern)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return nil, err
|
||||
}
|
||||
languages = append(languages, availableLanguage{language.MustParse(langTag), currencyPattern})
|
||||
}
|
||||
if rows.Err() != nil {
|
||||
panic(rows.Err())
|
||||
return nil, rows.Err()
|
||||
}
|
||||
|
||||
return languages
|
||||
return languages, nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue