aboutsummaryrefslogtreecommitdiff
path: root/echo/error_handler.go
blob: 2316ccd92ed45b4749e5c7d2e17e30ba49d7e1a9 (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package echo

import (
	"bytes"
	"fmt"
	"html/template"
	"io/fs"
	"net/http"

	"github.com/elnormous/contenttype"
	"github.com/labstack/echo/v4"
)

// TODO: This should allow plugging in other content types
// TODO: This should also be refactored into something prettier
func ErrorHandler(templates fs.FS, funcs template.FuncMap) func(error, echo.Context) {
	handleHtml := func(c echo.Context, he *echo.HTTPError) error {
		t, err := template.New("").Funcs(funcs).ParseFS(
			templates,
			"404.tpl",
			"40x.tpl",
			"50x.tpl",
			"header.tpl",
			"footer.tpl",
		)
		if err != nil {
			return err
		}

		path := "50x.tpl"
		if he.Code == 404 {
			path = "404.tpl"
		} else if he.Code >= 400 && he.Code <= 499 {
			path = "40x.tpl"
		}

		buf := bytes.Buffer{}
		if err = t.ExecuteTemplate(&buf, path, nil); err != nil {
			err = c.String(he.Code, fmt.Sprintf("%s", he.Message))
		}

		return c.HTMLBlob(he.Code, buf.Bytes())
	}

	handlePlain := func(c echo.Context, he *echo.HTTPError) error {
		return c.String(he.Code, fmt.Sprintf("%s", he.Message))
	}

	handleJson := func(c echo.Context, he *echo.HTTPError) error {
		code := he.Code
		message := he.Message
		if m, ok := he.Message.(string); ok {
			if c.Echo().Debug {
				message = echo.Map{"message": m, "error": he.Error()}
			} else {
				message = echo.Map{"message": m}
			}
		}

		return c.JSON(code, message)
	}

	errorWhileErroring := func(c echo.Context, err interface{}) {
		c.Echo().Logger.Error(err)
		if c.Echo().Debug {
			c.JSON(http.StatusInternalServerError, &echo.HTTPError{
				Code:    http.StatusInternalServerError,
				Message: fmt.Sprintf("Error while processing error page. %s", err),
			})
		} else {
			c.JSON(http.StatusInternalServerError, &echo.HTTPError{
				Code:    http.StatusInternalServerError,
				Message: http.StatusText(http.StatusInternalServerError),
			})
		}
	}

	handlers := map[string]func(echo.Context, *echo.HTTPError) error{
		"text/plain":       handlePlain,
		"text/html":        handleHtml,
		"text/json":        handleJson,
		"application/json": handleJson,
	}

	// This is hand maintained here because order is important for negotiation,
	// especially in the case of */*
	hIndex := []contenttype.MediaType{
		contenttype.NewMediaType("text/json"),
		contenttype.NewMediaType("application/json"),
		contenttype.NewMediaType("text/plain"),
		contenttype.NewMediaType("text/html"),
	}

	return func(err error, c echo.Context) {
		defer func() {
			if r := recover(); r != nil {
				errorWhileErroring(c, r)
			}
		}()

		he, ok := err.(*echo.HTTPError)
		if ok {
			if he.Internal != nil {
				if herr, ok := he.Internal.(*echo.HTTPError); ok {
					he = herr
				}
			}
		} else {
			he = &echo.HTTPError{
				Code:    http.StatusInternalServerError,
				Message: http.StatusText(http.StatusInternalServerError),
			}
		}

		ct, _, err := contenttype.GetAcceptableMediaType(c.Request(), hIndex)
		if err != nil {
			c.Echo().Logger.Error("Error negotiating content type in error handler, using json")
			ct = contenttype.NewMediaType("text/json")
		}

		handle, ok := handlers[ct.String()]
		if !ok {
			c.Echo().Logger.Errorf("Error handler content type %s is unknown", ct.String())
			handle = handleJson
		}

		if !c.Response().Committed {
			if c.Request().Method == http.MethodHead { // Issue #608
				err = c.NoContent(he.Code)
			} else {
				err = handle(c, he)
			}
			if err != nil {
				errorWhileErroring(c, err)
			}
		}
	}
}