diff --git a/handler/error/error.go b/handler/error/error.go index 7cb4c4e..e87e3f9 100644 --- a/handler/error/error.go +++ b/handler/error/error.go @@ -13,8 +13,18 @@ func init() { log = logger.Logger() } -func ALL(w http.ResponseWriter, r *http.Request) { - component := errorView.Main("Not Found") +func NotFound(w http.ResponseWriter, r *http.Request) { + component := errorView.NotFound("Not Found") + err := component.Render(r.Context(), w) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + log.Error(err.Error()) + return + } +} + +func InternalServerError(w http.ResponseWriter, r *http.Request) { + component := errorView.InternalServerError("Internal Server Error") err := component.Render(r.Context(), w) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) diff --git a/handler/upload/initialisation/initialisation.go b/handler/upload/initialisation/initialisation.go index 17849ed..9d0b4b1 100644 --- a/handler/upload/initialisation/initialisation.go +++ b/handler/upload/initialisation/initialisation.go @@ -28,13 +28,13 @@ func POST(w http.ResponseWriter, r *http.Request) { body, err := io.ReadAll(r.Body) if err != nil { - handleError(w, err, http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) return } var fileInfo types.FileInfo if err := json.Unmarshal(body, &fileInfo); err != nil { - handleError(w, err, http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) return } @@ -43,7 +43,7 @@ func POST(w http.ResponseWriter, r *http.Request) { if errors.Is(err, gorm.ErrRecordNotFound) { upload, err := handleNewUpload(userSession, fileInfo) if err != nil { - handleError(w, err, http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) return } respondJSON(w, upload) diff --git a/middleware/middleware.go b/middleware/middleware.go index 90a2f4a..c6399d3 100644 --- a/middleware/middleware.go +++ b/middleware/middleware.go @@ -25,14 +25,20 @@ type wrapper struct { } func (w *wrapper) WriteHeader(code int) { - if code == http.StatusNotFound { + switch code { + case http.StatusNotFound: w.Header().Set("Content-Type", "text/html") - errorHandler.ALL(w.ResponseWriter, w.request) + errorHandler.NotFound(w.ResponseWriter, w.request) + return + case http.StatusInternalServerError: + w.Header().Set("Content-Type", "text/html") + errorHandler.InternalServerError(w.ResponseWriter, w.request) + return + default: + w.ResponseWriter.WriteHeader(code) + w.statusCode = code return } - w.ResponseWriter.WriteHeader(code) - w.statusCode = code - return } func Handler(next http.Handler) http.Handler { diff --git a/public/InternalServerErrorIcon.svg b/public/InternalServerErrorIcon.svg new file mode 100644 index 0000000..dca3ad3 --- /dev/null +++ b/public/InternalServerErrorIcon.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/routes/routes.go b/routes/routes.go index 51c46df..a5a3e98 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -193,6 +193,11 @@ func SetupRoutes() *http.ServeMux { http.ServeFile(w, r, "public/favicon.ico") }) + //TODO add error message catching to the middleware so we can show the error message at the error page + handler.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) { + http.Error(w, "anjay", http.StatusInternalServerError) + }) + fileServer := http.FileServer(http.Dir("./public")) handler.Handle("/public/", http.StripPrefix("/public", fileServer)) diff --git a/view/error/error.templ b/view/error/error.templ index e2b39d8..08e993e 100644 --- a/view/error/error.templ +++ b/view/error/error.templ @@ -2,17 +2,17 @@ package errorView import "github.com/fossyy/filekeeper/view/layout" -templ content(title string){ +templ NotFound(title string){ @layout.Base(title){