Catch and output directory walking errors

Previously, app would panic and admins would see unhelpful errors.

This closes #620
This commit is contained in:
Matt Baer 2023-01-06 15:34:22 -05:00
parent 3e6669828c
commit 99d72881cf
2 changed files with 23 additions and 3 deletions

View file

@ -11,6 +11,7 @@
package author package author
import ( import (
"github.com/writeas/web-core/log"
"github.com/writefreely/writefreely/config" "github.com/writefreely/writefreely/config"
"os" "os"
"path/filepath" "path/filepath"
@ -113,10 +114,17 @@ func IsValidUsername(cfg *config.Config, username string) bool {
// Username is invalid if page with the same name exists. So traverse // Username is invalid if page with the same name exists. So traverse
// available pages, adding them to reservedUsernames map that'll be checked // available pages, adding them to reservedUsernames map that'll be checked
// later. // later.
filepath.Walk(filepath.Join(cfg.Server.PagesParentDir, "pages"), func(path string, i os.FileInfo, err error) error { err := filepath.Walk(filepath.Join(cfg.Server.PagesParentDir, "pages"), func(path string, i os.FileInfo, err error) error {
if err != nil {
return err
}
reservedUsernames[i.Name()] = true reservedUsernames[i.Name()] = true
return nil return nil
}) })
if err != nil {
log.Error("[IMPORTANT WARNING]: Could not determine IsValidUsername! %s", err)
return false
}
// Username is invalid if it is reserved! // Username is invalid if it is reserved!
if _, reserved := reservedUsernames[username]; reserved { if _, reserved := reservedUsernames[username]; reserved {

View file

@ -135,7 +135,10 @@ func InitTemplates(cfg *config.Config) error {
log.Info("Loading pages...") log.Info("Loading pages...")
// Initialize all static pages that use the base template // Initialize all static pages that use the base template
filepath.Walk(filepath.Join(cfg.Server.PagesParentDir, pagesDir), func(path string, i os.FileInfo, err error) error { err = filepath.Walk(filepath.Join(cfg.Server.PagesParentDir, pagesDir), func(path string, i os.FileInfo, err error) error {
if err != nil {
return err
}
if !i.IsDir() && !strings.HasPrefix(i.Name(), ".") { if !i.IsDir() && !strings.HasPrefix(i.Name(), ".") {
key := i.Name() key := i.Name()
initPage(cfg.Server.PagesParentDir, path, key) initPage(cfg.Server.PagesParentDir, path, key)
@ -143,10 +146,16 @@ func InitTemplates(cfg *config.Config) error {
return nil return nil
}) })
if err != nil {
return err
}
log.Info("Loading user pages...") log.Info("Loading user pages...")
// Initialize all user pages that use base templates // Initialize all user pages that use base templates
filepath.Walk(filepath.Join(cfg.Server.TemplatesParentDir, templatesDir, "user"), func(path string, f os.FileInfo, err error) error { err = filepath.Walk(filepath.Join(cfg.Server.TemplatesParentDir, templatesDir, "user"), func(path string, f os.FileInfo, err error) error {
if err != nil {
return err
}
if !f.IsDir() && !strings.HasPrefix(f.Name(), ".") { if !f.IsDir() && !strings.HasPrefix(f.Name(), ".") {
corePath := path corePath := path
if cfg.Server.TemplatesParentDir != "" { if cfg.Server.TemplatesParentDir != "" {
@ -162,6 +171,9 @@ func InitTemplates(cfg *config.Config) error {
return nil return nil
}) })
if err != nil {
return err
}
return nil return nil
} }