diff --git a/handler/auth/google/callback/callback.go b/handler/auth/google/callback/callback.go index ea2a412..e765b8f 100644 --- a/handler/auth/google/callback/callback.go +++ b/handler/auth/google/callback/callback.go @@ -85,7 +85,8 @@ func init() { func GET(w http.ResponseWriter, r *http.Request) { if _, ok := CsrfTokens[r.URL.Query().Get("state")]; !ok { - http.Error(w, "csrf token mismatch", http.StatusInternalServerError) + //csrf token mismatch error + w.WriteHeader(http.StatusInternalServerError) return } @@ -106,16 +107,16 @@ func GET(w http.ResponseWriter, r *http.Request) { resp, err := http.Post("https://oauth2.googleapis.com/token", "application/x-www-form-urlencoded", bytes.NewBufferString(formData.Encode())) if err != nil { + w.WriteHeader(http.StatusInternalServerError) log.Error("Error:", err) - http.Error(w, "Failed to get token", http.StatusInternalServerError) return } defer resp.Body.Close() var oauthData OauthToken if err := json.NewDecoder(resp.Body).Decode(&oauthData); err != nil { + w.WriteHeader(http.StatusInternalServerError) log.Error("Error reading token response body:", err) - http.Error(w, "Failed to read token response body", http.StatusInternalServerError) return } @@ -124,8 +125,8 @@ func GET(w http.ResponseWriter, r *http.Request) { req, err := http.NewRequest("GET", "https://www.googleapis.com/oauth2/v1/userinfo?alt=json", nil) req.Header.Set("Authorization", "Bearer "+oauthData.AccessToken) if err != nil { + w.WriteHeader(http.StatusInternalServerError) log.Error("Error creating user info request:", err) - http.Error(w, "Failed to create user info request", http.StatusInternalServerError) return } @@ -134,14 +135,14 @@ func GET(w http.ResponseWriter, r *http.Request) { var oauthUser OauthUser if err := json.NewDecoder(userInfoResp.Body).Decode(&oauthUser); err != nil { + w.WriteHeader(http.StatusInternalServerError) log.Error("Error reading user info response body:", err) - http.Error(w, "Failed to read user info response body", http.StatusInternalServerError) return } if oauthUser.Email == "" { + w.WriteHeader(http.StatusInternalServerError) log.Error("Error reading user info response body: email not found") - http.Error(w, "Invalid email is given", http.StatusInternalServerError) return } @@ -159,7 +160,7 @@ func GET(w http.ResponseWriter, r *http.Request) { user, err := cache.GetUser(oauthUser.Email) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) log.Error(err.Error()) return } diff --git a/handler/auth/google/google.go b/handler/auth/google/google.go index da9202a..d774536 100644 --- a/handler/auth/google/google.go +++ b/handler/auth/google/google.go @@ -3,16 +3,24 @@ package googleOauthHandler import ( "fmt" googleOauthCallbackHandler "github.com/fossyy/filekeeper/handler/auth/google/callback" + "github.com/fossyy/filekeeper/logger" "github.com/fossyy/filekeeper/utils" "net/http" "time" ) +var log *logger.AggregatedLogger + +func init() { + log = logger.Logger() +} + func GET(w http.ResponseWriter, r *http.Request) { token, err := utils.GenerateCSRFToken() googleOauthCallbackHandler.CsrfTokens[token] = &googleOauthCallbackHandler.CsrfToken{Token: token, CreateTime: time.Now()} if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) + log.Error(err.Error()) return } http.Redirect(w, r, fmt.Sprintf("https://accounts.google.com/o/oauth2/auth?scope=email profile&response_type=code&access_type=offline&state=%s&redirect_uri=%s&client_id=%s", token, utils.Getenv("GOOGLE_CALLBACK"), utils.Getenv("GOOGLE_CLIENT_ID")), http.StatusFound) diff --git a/handler/auth/google/setup/setup.go b/handler/auth/google/setup/setup.go index 16aa7df..4eace7f 100644 --- a/handler/auth/google/setup/setup.go +++ b/handler/auth/google/setup/setup.go @@ -67,7 +67,7 @@ func GET(w http.ResponseWriter, r *http.Request) { }) err := component.Render(r.Context(), w) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) log.Error(err.Error()) return } @@ -82,7 +82,7 @@ func POST(w http.ResponseWriter, r *http.Request) { } err := r.ParseForm() if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) log.Error(err.Error()) return } @@ -97,7 +97,7 @@ func POST(w http.ResponseWriter, r *http.Request) { }) err := component.Render(r.Context(), w) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) log.Error(err.Error()) return } @@ -121,7 +121,7 @@ func POST(w http.ResponseWriter, r *http.Request) { }) err := component.Render(r.Context(), w) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) log.Error(err.Error()) return } diff --git a/handler/download/download.go b/handler/download/download.go index cb48aa8..b41fc4d 100644 --- a/handler/download/download.go +++ b/handler/download/download.go @@ -20,7 +20,7 @@ func GET(w http.ResponseWriter, r *http.Request) { userSession := r.Context().Value("user").(types.User) files, err := db.DB.GetFiles(userSession.UserID.String()) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) return } @@ -37,7 +37,7 @@ func GET(w http.ResponseWriter, r *http.Request) { component := downloadView.Main("Download Page", filesData) err = component.Render(r.Context(), w) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) log.Error(err.Error()) return } diff --git a/handler/download/file/file.go b/handler/download/file/file.go index df61902..cb17d68 100644 --- a/handler/download/file/file.go +++ b/handler/download/file/file.go @@ -19,7 +19,7 @@ func GET(w http.ResponseWriter, r *http.Request) { fileID := r.PathValue("id") file, err := db.DB.GetFile(fileID) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) log.Error(err.Error()) return } @@ -32,13 +32,13 @@ func GET(w http.ResponseWriter, r *http.Request) { if filepath.Dir(saveFolder) != filepath.Join(basePath, file.OwnerID.String()) { log.Error("invalid path") - http.Error(w, err.Error(), http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) return } openFile, err := os.OpenFile(filepath.Join(saveFolder, file.Name), os.O_RDONLY, 0) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) log.Error(err.Error()) return } @@ -46,7 +46,7 @@ func GET(w http.ResponseWriter, r *http.Request) { stat, err := openFile.Stat() if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) log.Error(err.Error()) return } diff --git a/handler/error/error.go b/handler/error/error.go index 7cb4c4e..8c8785f 100644 --- a/handler/error/error.go +++ b/handler/error/error.go @@ -1,6 +1,7 @@ package errorHandler import ( + "fmt" "net/http" "github.com/fossyy/filekeeper/logger" @@ -13,11 +14,21 @@ 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) + fmt.Fprint(w, err.Error()) + 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 { + fmt.Fprint(w, err.Error()) log.Error(err.Error()) return } diff --git a/handler/forgotPassword/forgotPassword.go b/handler/forgotPassword/forgotPassword.go index edc3edd..4db9dec 100644 --- a/handler/forgotPassword/forgotPassword.go +++ b/handler/forgotPassword/forgotPassword.go @@ -71,7 +71,7 @@ func GET(w http.ResponseWriter, r *http.Request) { }) err := component.Render(r.Context(), w) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) log.Error(err.Error()) return } @@ -95,7 +95,7 @@ func POST(w http.ResponseWriter, r *http.Request) { }) err := component.Render(r.Context(), w) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) log.Error(err.Error()) return } @@ -111,7 +111,7 @@ func POST(w http.ResponseWriter, r *http.Request) { err = verifyForgot(userData) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) log.Error(err.Error()) return } @@ -119,7 +119,7 @@ func POST(w http.ResponseWriter, r *http.Request) { component := forgotPasswordView.EmailSend("Forgot Password Page") err = component.Render(r.Context(), w) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) log.Error(err.Error()) return } diff --git a/handler/forgotPassword/verify/verify.go b/handler/forgotPassword/verify/verify.go index 128185e..223dab2 100644 --- a/handler/forgotPassword/verify/verify.go +++ b/handler/forgotPassword/verify/verify.go @@ -39,7 +39,7 @@ func GET(w http.ResponseWriter, r *http.Request) { }) err := component.Render(r.Context(), w) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) log.Error(err.Error()) return } @@ -58,7 +58,7 @@ func POST(w http.ResponseWriter, r *http.Request) { err := r.ParseForm() if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) log.Error(err.Error()) return } @@ -72,7 +72,7 @@ func POST(w http.ResponseWriter, r *http.Request) { }) err := component.Render(r.Context(), w) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) log.Error(err.Error()) return } @@ -81,14 +81,14 @@ func POST(w http.ResponseWriter, r *http.Request) { hashedPassword, err := utils.HashPassword(password) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) log.Error(err.Error()) return } err = db.DB.UpdateUserPassword(data.User.Email, hashedPassword) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) log.Error(err.Error()) return } @@ -103,7 +103,7 @@ func POST(w http.ResponseWriter, r *http.Request) { component := forgotPasswordView.ChangeSuccess("Forgot Password Page") err = component.Render(r.Context(), w) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) log.Error(err.Error()) return } diff --git a/handler/index/index.go b/handler/index/index.go index 4bcbdc2..93157e8 100644 --- a/handler/index/index.go +++ b/handler/index/index.go @@ -19,7 +19,7 @@ func GET(w http.ResponseWriter, r *http.Request) { component := indexView.Main("main page", userSession) err := component.Render(r.Context(), w) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) log.Error(err.Error()) return } diff --git a/handler/logout/logout.go b/handler/logout/logout.go index ab5c8cb..85cb180 100644 --- a/handler/logout/logout.go +++ b/handler/logout/logout.go @@ -20,7 +20,7 @@ func GET(w http.ResponseWriter, r *http.Request) { if errors.Is(err, &session.SessionNotFoundError{}) { storeSession.Destroy(w) } - http.Error(w, err.Error(), http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) return } diff --git a/handler/signin/signin.go b/handler/signin/signin.go index d81c9d2..e6dde46 100644 --- a/handler/signin/signin.go +++ b/handler/signin/signin.go @@ -60,7 +60,7 @@ func GET(w http.ResponseWriter, r *http.Request) { err := component.Render(r.Context(), w) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) log.Error(err.Error()) return } @@ -84,7 +84,7 @@ func POST(w http.ResponseWriter, r *http.Request) { log.Error(err.Error()) err = component.Render(r.Context(), w) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) log.Error(err.Error()) return } @@ -134,7 +134,7 @@ func POST(w http.ResponseWriter, r *http.Request) { }) err = component.Render(r.Context(), w) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) log.Error(err.Error()) return } diff --git a/handler/signup/signup.go b/handler/signup/signup.go index eb68f59..6400fb5 100644 --- a/handler/signup/signup.go +++ b/handler/signup/signup.go @@ -70,7 +70,7 @@ func GET(w http.ResponseWriter, r *http.Request) { }) err := component.Render(r.Context(), w) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) log.Error(err.Error()) return } @@ -79,7 +79,7 @@ func GET(w http.ResponseWriter, r *http.Request) { func POST(w http.ResponseWriter, r *http.Request) { err := r.ParseForm() if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) log.Error(err.Error()) return } @@ -94,7 +94,7 @@ func POST(w http.ResponseWriter, r *http.Request) { }) err := component.Render(r.Context(), w) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) log.Error(err.Error()) return } @@ -116,7 +116,7 @@ func POST(w http.ResponseWriter, r *http.Request) { }) err = component.Render(r.Context(), w) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) log.Error(err.Error()) return } @@ -125,7 +125,7 @@ func POST(w http.ResponseWriter, r *http.Request) { err = verifyEmail(&newUser) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) log.Error(err.Error()) return } @@ -133,7 +133,7 @@ func POST(w http.ResponseWriter, r *http.Request) { component := signupView.EmailSend("Sign up Page") err = component.Render(r.Context(), w) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) log.Error(err.Error()) return } diff --git a/handler/signup/verify/verify.go b/handler/signup/verify/verify.go index f26bea5..a735b20 100644 --- a/handler/signup/verify/verify.go +++ b/handler/signup/verify/verify.go @@ -33,7 +33,7 @@ func GET(w http.ResponseWriter, r *http.Request) { }) err := component.Render(r.Context(), w) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) log.Error(err.Error()) return } @@ -47,7 +47,7 @@ func GET(w http.ResponseWriter, r *http.Request) { err = component.Render(r.Context(), w) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) log.Error(err.Error()) return } 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/handler/upload/upload.go b/handler/upload/upload.go index 204556e..569fe4c 100644 --- a/handler/upload/upload.go +++ b/handler/upload/upload.go @@ -21,7 +21,7 @@ func init() { func GET(w http.ResponseWriter, r *http.Request) { component := filesView.Main("upload page") if err := component.Render(r.Context(), w); err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) return } } @@ -29,7 +29,7 @@ func GET(w http.ResponseWriter, r *http.Request) { func POST(w http.ResponseWriter, r *http.Request) { fileID := r.PathValue("id") if err := r.ParseMultipartForm(32 << 20); err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) return } @@ -39,7 +39,7 @@ func POST(w http.ResponseWriter, r *http.Request) { if _, err := os.Stat(uploadDir); os.IsNotExist(err) { if err := os.Mkdir(uploadDir, os.ModePerm); err != nil { log.Error("error getting upload info: " + err.Error()) - http.Error(w, err.Error(), http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) return } } @@ -47,7 +47,7 @@ func POST(w http.ResponseWriter, r *http.Request) { file, err := cache.GetFile(fileID) if err != nil { log.Error("error getting upload info: " + err.Error()) - http.Error(w, err.Error(), http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) return } @@ -57,14 +57,14 @@ func POST(w http.ResponseWriter, r *http.Request) { if filepath.Dir(saveFolder) != filepath.Join(basePath, userSession.UserID.String()) { log.Error("invalid path") - http.Error(w, err.Error(), http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) return } fileByte, fileHeader, err := r.FormFile("chunk") if err != nil { log.Error("error getting upload info: " + err.Error()) - http.Error(w, err.Error(), http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) return } defer fileByte.Close() @@ -80,14 +80,14 @@ func POST(w http.ResponseWriter, r *http.Request) { dst, err := os.OpenFile(filepath.Join(saveFolder, file.Name), os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666) if err != nil { log.Error("error making upload folder: " + err.Error()) - http.Error(w, err.Error(), http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) return } defer dst.Close() if _, err := io.Copy(dst, fileByte); err != nil { log.Error("error copying byte to file dst: " + err.Error()) - http.Error(w, err.Error(), http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) return } diff --git a/handler/user/user.go b/handler/user/user.go index 39e4ea8..daa58dc 100644 --- a/handler/user/user.go +++ b/handler/user/user.go @@ -20,7 +20,7 @@ func GET(w http.ResponseWriter, r *http.Request) { component := userView.Main("User Page", userSession, session.GetSessions(userSession.Email)) err := component.Render(r.Context(), w) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) log.Error(err.Error()) return } diff --git a/middleware/middleware.go b/middleware/middleware.go index 90a2f4a..d655d5b 100644 --- a/middleware/middleware.go +++ b/middleware/middleware.go @@ -25,13 +25,23 @@ type wrapper struct { } func (w *wrapper) WriteHeader(code int) { + w.statusCode = code + if code == http.StatusNotFound { w.Header().Set("Content-Type", "text/html") - errorHandler.ALL(w.ResponseWriter, w.request) + w.ResponseWriter.WriteHeader(code) + errorHandler.NotFound(w.ResponseWriter, w.request) + return + } + + if code == http.StatusInternalServerError { + w.Header().Set("Content-Type", "text/html") + w.ResponseWriter.WriteHeader(code) + errorHandler.InternalServerError(w.ResponseWriter, w.request) return } w.ResponseWriter.WriteHeader(code) - w.statusCode = code + return } 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..82039e6 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){

404 Not Found

-

+

The page you are looking for does not exist. It might have been moved or deleted.

+
+ brand image + +
+ + } } \ No newline at end of file