Skip to content

Commit e43f6b2

Browse files
committed
Add exception catching
1 parent 5bbda75 commit e43f6b2

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

servers/detail.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import (
88

99
func DetailHandler(s schema.Server, m *schema.Model, w http.ResponseWriter, r *http.Request) {
1010
rc := NewRequestContext(s, w, r)
11+
12+
defer catchExceptions(rc, w)()
13+
1114
err := rc.Authenticate()
1215
if err != nil {
1316
return

servers/exceptions.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"github.com/bor3ham/reja/schema"
66
"net/http"
7+
"log"
78
)
89

910
type Error struct {
@@ -55,3 +56,25 @@ func MethodNotAllowed(c schema.Context, w http.ResponseWriter) {
5556
w.WriteHeader(http.StatusForbidden)
5657
c.WriteToResponse(errorBlob)
5758
}
59+
60+
func InternalServerError(c schema.Context, w http.ResponseWriter) {
61+
errorBlob := Error{
62+
Exceptions: []Exception{
63+
Exception{
64+
Title: "Internal Server Error",
65+
Detail: "Something went wrong. Please try again later.",
66+
},
67+
},
68+
}
69+
w.WriteHeader(http.StatusInternalServerError)
70+
c.WriteToResponse(errorBlob)
71+
}
72+
73+
func catchExceptions(c schema.Context, w http.ResponseWriter) func() {
74+
return func() {
75+
if err := recover(); err != nil {
76+
InternalServerError(c, w)
77+
log.Printf("Runtime panic: %v", err)
78+
}
79+
}
80+
}

servers/list.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import (
77

88
func ListHandler(s schema.Server, m *schema.Model, w http.ResponseWriter, r *http.Request) {
99
rc := NewRequestContext(s, w, r)
10+
11+
defer catchExceptions(rc, w)()
12+
1013
err := rc.Authenticate()
1114
if err != nil {
1215
return

0 commit comments

Comments
 (0)