-
Notifications
You must be signed in to change notification settings - Fork 1
/
errors.go
56 lines (45 loc) · 1.07 KB
/
errors.go
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
package server
import (
"errors"
"fmt"
"net/http"
"github.com/gin-gonic/gin"
)
type errorWithMessage struct {
code int
message string
}
func newErrorWithMessage(code int, format string, args ...any) errorWithMessage {
return errorWithMessage{
code: code,
message: fmt.Sprintf(format, args...),
}
}
func (e errorWithMessage) Error() string {
return e.message
}
func handleWebcalErr(c *gin.Context, err error) {
var msgErr errorWithMessage
if errors.As(err, &msgErr) {
c.String(msgErr.code, msgErr.message)
return
}
log(c).Error(err)
handleWebcalErr(c, newErrorWithMessage(
http.StatusInternalServerError,
"%s", http.StatusText(http.StatusInternalServerError),
))
}
func handleHTMXError(c *gin.Context, calendar Month, err error) {
var msgErr errorWithMessage
if errors.As(err, &msgErr) {
calendar.Error = msgErr.message
c.HTML(http.StatusOK, "calendar", calendar)
return
}
log(c).Error(err)
handleHTMXError(c, calendar, newErrorWithMessage(
http.StatusInternalServerError,
"%s", http.StatusText(http.StatusInternalServerError),
))
}