diff --git a/Makefile b/Makefile index 24b7eec..f81a30f 100644 --- a/Makefile +++ b/Makefile @@ -54,7 +54,7 @@ run: # Run all tests .PHONY: test test: - go test -p 1 ./... + go test -count=1 -p 1 ./... # Run the worker .PHONY: worker @@ -65,4 +65,4 @@ worker: # Check for direct dependency updates .PHONY: check-updates check-updates: - go list -u -m -f '{{if not .Indirect}}{{.}}{{end}}' all + go list -u -m -f '{{if not .Indirect}}{{.}}{{end}}' all | grep "\[" diff --git a/controller/controller.go b/controller/controller.go index aeea5eb..ff62b47 100644 --- a/controller/controller.go +++ b/controller/controller.go @@ -35,8 +35,7 @@ func (c *Controller) RenderPage(ctx echo.Context, page Page) error { // Page name is required if page.Name == "" { - ctx.Logger().Error("page render failed due to missing name") - return echo.NewHTTPError(http.StatusInternalServerError) + return echo.NewHTTPError(http.StatusInternalServerError, "page render failed due to missing name") } // Use the app name in configuration if a value was not set @@ -85,8 +84,7 @@ func (c *Controller) RenderPage(ctx echo.Context, page Page) error { } if err != nil { - ctx.Logger().Errorf("failed to parse and execute templates: %v", err) - return echo.NewHTTPError(http.StatusInternalServerError) + return c.Fail(err, "failed to parse and execute templates") } // Set the status code @@ -168,10 +166,6 @@ func (c *Controller) Redirect(ctx echo.Context, route string, routeParams ...int } // Fail is a helper to fail a request by returning a 500 error and logging the error -func (c *Controller) Fail(ctx echo.Context, err error, log string) error { - if context.IsCanceledError(err) { - return nil - } - ctx.Logger().Errorf("%s: %v", log, err) - return echo.NewHTTPError(http.StatusInternalServerError) +func (c *Controller) Fail(err error, log string) error { + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("%s: %v", log, err)) } diff --git a/middleware/auth.go b/middleware/auth.go index 9d43959..3246d55 100644 --- a/middleware/auth.go +++ b/middleware/auth.go @@ -1,6 +1,7 @@ package middleware import ( + "fmt" "net/http" "strconv" @@ -25,10 +26,10 @@ func LoadAuthenticatedUser(authClient *services.AuthClient) echo.MiddlewareFunc c.Set(context.AuthenticatedUserKey, u) c.Logger().Infof("auth user loaded in to context: %d", u.ID) default: - if context.IsCanceledError(err) { - return nil - } - c.Logger().Errorf("error querying for authenticated user: %v", err) + return echo.NewHTTPError( + http.StatusInternalServerError, + fmt.Sprintf("error querying for authenticated user: %v", err), + ) } return next(c) @@ -71,11 +72,10 @@ func LoadValidPasswordToken(authClient *services.AuthClient) echo.MiddlewareFunc msg.Warning(c, "The link is either invalid or has expired. Please request a new one.") return c.Redirect(http.StatusFound, c.Echo().Reverse("forgot_password")) default: - if context.IsCanceledError(err) { - return nil - } - c.Logger().Error(err) - return echo.NewHTTPError(http.StatusInternalServerError) + return echo.NewHTTPError( + http.StatusInternalServerError, + fmt.Sprintf("error loading password token: %v", err), + ) } } } diff --git a/middleware/entity.go b/middleware/entity.go index 803f24e..3d433ee 100644 --- a/middleware/entity.go +++ b/middleware/entity.go @@ -1,6 +1,7 @@ package middleware import ( + "fmt" "net/http" "strconv" @@ -32,11 +33,10 @@ func LoadUser(orm *ent.Client) echo.MiddlewareFunc { case *ent.NotFoundError: return echo.NewHTTPError(http.StatusNotFound) default: - if context.IsCanceledError(err) { - return nil - } - c.Logger().Error(err) - return echo.NewHTTPError(http.StatusInternalServerError) + return echo.NewHTTPError( + http.StatusInternalServerError, + fmt.Sprintf("error querying user: %v", err), + ) } } } diff --git a/routes/contact.go b/routes/contact.go index 4a7ac99..305720b 100644 --- a/routes/contact.go +++ b/routes/contact.go @@ -41,11 +41,11 @@ func (c *contact) Post(ctx echo.Context) error { // Parse the form values if err := ctx.Bind(&form); err != nil { - return c.Fail(ctx, err, "unable to bind form") + return c.Fail(err, "unable to bind form") } if err := form.Submission.Process(ctx, form); err != nil { - return c.Fail(ctx, err, "unable to process form submission") + return c.Fail(err, "unable to process form submission") } if !form.Submission.HasErrors() { @@ -57,7 +57,7 @@ func (c *contact) Post(ctx echo.Context) error { Send(ctx) if err != nil { - return c.Fail(ctx, err, "unable to send email") + return c.Fail(err, "unable to send email") } } diff --git a/routes/forgot_password.go b/routes/forgot_password.go index 52243fc..daa9b88 100644 --- a/routes/forgot_password.go +++ b/routes/forgot_password.go @@ -50,11 +50,11 @@ func (c *forgotPassword) Post(ctx echo.Context) error { // Parse the form values if err := ctx.Bind(&form); err != nil { - return c.Fail(ctx, err, "unable to parse forgot password form") + return c.Fail(err, "unable to parse forgot password form") } if err := form.Submission.Process(ctx, form); err != nil { - return c.Fail(ctx, err, "unable to process form submission") + return c.Fail(err, "unable to process form submission") } if form.Submission.HasErrors() { @@ -72,13 +72,13 @@ func (c *forgotPassword) Post(ctx echo.Context) error { return succeed() case nil: default: - return c.Fail(ctx, err, "error querying user during forgot password") + return c.Fail(err, "error querying user during forgot password") } // Generate the token token, pt, err := c.Container.Auth.GeneratePasswordResetToken(ctx, u.ID) if err != nil { - return c.Fail(ctx, err, "error generating password reset token") + return c.Fail(err, "error generating password reset token") } ctx.Logger().Infof("generated password reset token for user %d", u.ID) @@ -93,7 +93,7 @@ func (c *forgotPassword) Post(ctx echo.Context) error { Send(ctx) if err != nil { - return c.Fail(ctx, err, "error sending password reset email") + return c.Fail(err, "error sending password reset email") } return succeed() diff --git a/routes/login.go b/routes/login.go index a8e60bd..9c37d90 100644 --- a/routes/login.go +++ b/routes/login.go @@ -52,11 +52,11 @@ func (c *login) Post(ctx echo.Context) error { // Parse the form values if err := ctx.Bind(&form); err != nil { - return c.Fail(ctx, err, "unable to parse login form") + return c.Fail(err, "unable to parse login form") } if err := form.Submission.Process(ctx, form); err != nil { - return c.Fail(ctx, err, "unable to process form submission") + return c.Fail(err, "unable to process form submission") } if form.Submission.HasErrors() { @@ -74,7 +74,7 @@ func (c *login) Post(ctx echo.Context) error { return authFailed() case nil: default: - return c.Fail(ctx, err, "error querying user during login") + return c.Fail(err, "error querying user during login") } // Check if the password is correct @@ -86,7 +86,7 @@ func (c *login) Post(ctx echo.Context) error { // Log the user in err = c.Container.Auth.Login(ctx, u.ID) if err != nil { - return c.Fail(ctx, err, "unable to log in user") + return c.Fail(err, "unable to log in user") } msg.Success(ctx, fmt.Sprintf("Welcome back, %s. You are now logged in.", u.Name)) diff --git a/routes/register.go b/routes/register.go index ecadccd..0f83622 100644 --- a/routes/register.go +++ b/routes/register.go @@ -45,11 +45,11 @@ func (c *register) Post(ctx echo.Context) error { // Parse the form values if err := ctx.Bind(&form); err != nil { - return c.Fail(ctx, err, "unable to parse register form") + return c.Fail(err, "unable to parse register form") } if err := form.Submission.Process(ctx, form); err != nil { - return c.Fail(ctx, err, "unable to process form submission") + return c.Fail(err, "unable to process form submission") } if form.Submission.HasErrors() { @@ -59,7 +59,7 @@ func (c *register) Post(ctx echo.Context) error { // Hash the password pwHash, err := c.Container.Auth.HashPassword(form.Password) if err != nil { - return c.Fail(ctx, err, "unable to hash password") + return c.Fail(err, "unable to hash password") } // Attempt creating the user @@ -77,7 +77,7 @@ func (c *register) Post(ctx echo.Context) error { msg.Warning(ctx, "A user with this email address already exists. Please log in.") return c.Redirect(ctx, "login") default: - return c.Fail(ctx, err, "unable to create user") + return c.Fail(err, "unable to create user") } // Log the user in diff --git a/routes/reset_password.go b/routes/reset_password.go index d32d290..af69775 100644 --- a/routes/reset_password.go +++ b/routes/reset_password.go @@ -41,11 +41,11 @@ func (c *resetPassword) Post(ctx echo.Context) error { // Parse the form values if err := ctx.Bind(&form); err != nil { - return c.Fail(ctx, err, "unable to parse password reset form") + return c.Fail(err, "unable to parse password reset form") } if err := form.Submission.Process(ctx, form); err != nil { - return c.Fail(ctx, err, "unable to process form submission") + return c.Fail(err, "unable to process form submission") } if form.Submission.HasErrors() { @@ -55,7 +55,7 @@ func (c *resetPassword) Post(ctx echo.Context) error { // Hash the new password hash, err := c.Container.Auth.HashPassword(form.Password) if err != nil { - return c.Fail(ctx, err, "unable to hash password") + return c.Fail(err, "unable to hash password") } // Get the requesting user @@ -68,13 +68,13 @@ func (c *resetPassword) Post(ctx echo.Context) error { Save(ctx.Request().Context()) if err != nil { - return c.Fail(ctx, err, "unable to update password") + return c.Fail(err, "unable to update password") } // Delete all password tokens for this user err = c.Container.Auth.DeletePasswordTokens(ctx, usr.ID) if err != nil { - return c.Fail(ctx, err, "unable to delete password tokens") + return c.Fail(err, "unable to delete password tokens") } msg.Success(ctx, "Your password has been updated.") diff --git a/routes/verify_email.go b/routes/verify_email.go index 6bd88f9..740c1ea 100644 --- a/routes/verify_email.go +++ b/routes/verify_email.go @@ -41,7 +41,7 @@ func (c *verifyEmail) Get(ctx echo.Context) error { Only(ctx.Request().Context()) if err != nil { - return c.Fail(ctx, err, "query failed loading email verification token user") + return c.Fail(err, "query failed loading email verification token user") } } @@ -53,7 +53,7 @@ func (c *verifyEmail) Get(ctx echo.Context) error { Save(ctx.Request().Context()) if err != nil { - return c.Fail(ctx, err, "failed to set user as verified") + return c.Fail(err, "failed to set user as verified") } }