gitstack

grindlemire/gothem-stack code browser

1.5 KB Go 71 lines 2025-11-29 · 98bbe10 raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package handler

import (
	"encoding/json"
	"net/http"

	"github.com/grindlemire/gothem-stack/web/pages/authfail"
	"github.com/labstack/echo/v4"
	"github.com/pkg/errors"
	"go.uber.org/zap"
)

func Error(err error, c echo.Context) {
	if c.Response().Committed {
		return
	}

	he, ok := err.(*echo.HTTPError)
	if ok {
		// If there is an internal error then use that for printing
		if he.Internal != nil {
			err = he.Internal
		}
	} else {
		he = &echo.HTTPError{
			Code:     http.StatusInternalServerError,
			Message:  http.StatusText(http.StatusInternalServerError),
			Internal: err,
		}
	}

	code := he.Code
	message := he.Message

	// only log unauthorized errors at the debug level
	if he.Code != http.StatusUnauthorized {
		zap.S().Error(err)
	} else {
		zap.S().Debug(err)
		// render the auth fail page for unauthorized errors
		c.Response().WriteHeader(http.StatusUnauthorized)
		renderErr := render(c, authfail.Page())
		if renderErr != nil {
			zap.S().Error(errors.Wrap(renderErr, "rendering auth fail page"))
		}
		return
	}

	switch m := he.Message.(type) {
	case string:
		message = echo.Map{"message": m}
	case json.Marshaler:
		// do nothing - this type knows how to format itself to JSON
	case error:
		message = echo.Map{"message": m.Error()}
	}

	// Send response
	if c.Request().Method == http.MethodHead {
		err = c.NoContent(he.Code)
		if err != nil {
			zap.S().Error(errors.Wrap(err, "sending no content"))
		}
		return
	}

	err = c.JSON(code, message)
	if err != nil {
		zap.S().Error(errors.Wrap(err, "marshalling json payload"))
	}
}