From 43e16f1d79c6b6d5a2b7a9c57d1c33f581c813c1 Mon Sep 17 00:00:00 2001 From: bagas Date: Tue, 14 May 2024 11:35:15 +0700 Subject: [PATCH] Handle internal server error using new custom template --- handler/auth/google/callback/callback.go | 15 ++++++++------- handler/auth/google/google.go | 10 +++++++++- handler/auth/google/setup/setup.go | 8 ++++---- handler/download/download.go | 4 ++-- handler/download/file/file.go | 8 ++++---- handler/error/error.go | 5 +++-- handler/forgotPassword/forgotPassword.go | 8 ++++---- handler/forgotPassword/verify/verify.go | 12 ++++++------ handler/index/index.go | 2 +- handler/logout/logout.go | 2 +- handler/signin/signin.go | 6 +++--- handler/signup/signup.go | 12 ++++++------ handler/signup/verify/verify.go | 4 ++-- handler/upload/upload.go | 16 ++++++++-------- handler/user/user.go | 2 +- middleware/middleware.go | 18 +++++++++++------- view/error/error.templ | 2 +- 17 files changed, 74 insertions(+), 60 deletions(-) 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 e87e3f9..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" @@ -17,7 +18,7 @@ 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 } @@ -27,7 +28,7 @@ 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) + 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/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 c6399d3..d655d5b 100644 --- a/middleware/middleware.go +++ b/middleware/middleware.go @@ -25,20 +25,24 @@ type wrapper struct { } func (w *wrapper) WriteHeader(code int) { - switch code { - case http.StatusNotFound: + w.statusCode = code + + if code == http.StatusNotFound { w.Header().Set("Content-Type", "text/html") + w.ResponseWriter.WriteHeader(code) errorHandler.NotFound(w.ResponseWriter, w.request) return - case http.StatusInternalServerError: + } + + if code == http.StatusInternalServerError { w.Header().Set("Content-Type", "text/html") + w.ResponseWriter.WriteHeader(code) errorHandler.InternalServerError(w.ResponseWriter, w.request) return - default: - w.ResponseWriter.WriteHeader(code) - w.statusCode = code - return } + w.ResponseWriter.WriteHeader(code) + + return } func Handler(next http.Handler) http.Handler { diff --git a/view/error/error.templ b/view/error/error.templ index 08e993e..82039e6 100644 --- a/view/error/error.templ +++ b/view/error/error.templ @@ -36,7 +36,7 @@ templ NotFound(title string){ templ InternalServerError(title string){ @layout.Base(title){ -
+
brand image