diff options
author | Alan Pearce | 2024-05-15 12:44:03 +0200 |
---|---|---|
committer | Alan Pearce | 2024-05-15 12:44:03 +0200 |
commit | 2d5ee7b69cbe27e7e1abef7bf3451cb6455c4387 (patch) | |
tree | c0d321d6a9f1ea9de1447742175261ed231a3590 /internal/server/mux.go | |
parent | 149cbe7681607eb48f47df14c7a39e6f289fb7b6 (diff) | |
download | searchix-2d5ee7b69cbe27e7e1abef7bf3451cb6455c4387.tar.lz searchix-2d5ee7b69cbe27e7e1abef7bf3451cb6455c4387.tar.zst searchix-2d5ee7b69cbe27e7e1abef7bf3451cb6455c4387.zip |
feat: render HTML error pages
Diffstat (limited to 'internal/server/mux.go')
-rw-r--r-- | internal/server/mux.go | 30 |
1 files changed, 18 insertions, 12 deletions
diff --git a/internal/server/mux.go b/internal/server/mux.go index aa2c1a6..c1f36fe 100644 --- a/internal/server/mux.go +++ b/internal/server/mux.go @@ -48,6 +48,8 @@ type TemplateData struct { SourceResult *bleve.SearchResult ExtraHeadHTML template.HTML Version VersionInfo + Code int + Message string } type ResultData[T options.NixOption] struct { @@ -64,6 +66,8 @@ var versionInfo = &VersionInfo{ CommitSHA: config.CommitSHA, } +var templates TemplateCollection + func applyDevModeOverrides(config *config.Config) { if len(config.Web.ContentSecurityPolicy.ScriptSrc) == 0 { config.Web.ContentSecurityPolicy.ScriptSrc = config.Web.ContentSecurityPolicy.DefaultSrc @@ -93,14 +97,16 @@ func NewMux( Repanic: true, }) - templates, err := loadTemplates() + templates, err = loadTemplates() if err != nil { log.Panicf("could not load templates: %v", err) } + errorHandler := createErrorHandler(config) + top := http.NewServeMux() mux := http.NewServeMux() - mux.HandleFunc("/{$}", func(w http.ResponseWriter, _ *http.Request) { + mux.HandleFunc("/{$}", func(w http.ResponseWriter, r *http.Request) { indexData := TemplateData{ ExtraHeadHTML: config.Web.ExtraHeadHTML, Sources: config.Importer.Sources, @@ -108,7 +114,7 @@ func NewMux( } err := templates["index"].Execute(w, indexData) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + errorHandler(w, r, err.Error(), http.StatusInternalServerError) } }) @@ -118,7 +124,7 @@ func NewMux( source := config.Importer.Sources[sourceKey] if source == nil { - http.Error(w, "Source not found", http.StatusNotFound) + errorHandler(w, r, "Source not found", http.StatusNotFound) return } @@ -133,18 +139,18 @@ func NewMux( if pg != "" { page, err = strconv.ParseUint(pg, 10, 64) if err != nil || page == 0 { - http.Error(w, "Bad query string", http.StatusBadRequest) + errorHandler(w, r, "Bad query string", http.StatusBadRequest) } } results, err := index.Search(ctx, sourceKey, qs, (page-1)*search.ResultsPerPage) if err != nil { if err == context.DeadlineExceeded { - http.Error(w, "Search timed out", http.StatusInternalServerError) + errorHandler(w, r, "Search timed out", http.StatusInternalServerError) return } slog.Error("search error", "error", err) - http.Error(w, err.Error(), http.StatusInternalServerError) + errorHandler(w, r, err.Error(), http.StatusInternalServerError) } tdata := ResultData[options.NixOption]{ @@ -163,13 +169,13 @@ func NewMux( if results.Total > hits { q, err := url.ParseQuery(r.URL.RawQuery) if err != nil { - http.Error(w, "Query string error", http.StatusBadRequest) + errorHandler(w, r, "Query string error", http.StatusBadRequest) return } if page*search.ResultsPerPage > results.Total { - http.Error(w, "Not found", http.StatusNotFound) + errorHandler(w, r, "Not found", http.StatusNotFound) return } @@ -198,12 +204,12 @@ func NewMux( } if err != nil { slog.Error("template error", "template", "options", "error", err) - http.Error(w, err.Error(), http.StatusInternalServerError) + errorHandler(w, r, err.Error(), http.StatusInternalServerError) } } else { sourceResult, err := index.GetSource(ctx, sourceKey) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + errorHandler(w, r, err.Error(), http.StatusInternalServerError) return } @@ -216,7 +222,7 @@ func NewMux( Version: *versionInfo, }) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + errorHandler(w, r, err.Error(), http.StatusInternalServerError) return } |