Fix error reponse

- Use status 500 for error page response
- Use json response for fluoride calls
This commit is contained in:
r 2020-01-31 02:44:33 +00:00
parent 17e55d2a9b
commit 6a6cc744b7

View File

@ -52,6 +52,14 @@ func serveJson(w io.Writer, data interface{}) (err error) {
return json.NewEncoder(w).Encode(d) return json.NewEncoder(w).Encode(d)
} }
func serveJsonError(w http.ResponseWriter, err error) {
var d = make(map[string]interface{})
d["error"] = err.Error()
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(d)
return
}
func NewHandler(s Service, staticDir string) http.Handler { func NewHandler(s Service, staticDir string) http.Handler {
r := mux.NewRouter() r := mux.NewRouter()
@ -72,6 +80,7 @@ func NewHandler(s Service, staticDir string) http.Handler {
ctx := context.Background() ctx := context.Background()
err := s.ServeSigninPage(ctx, c) err := s.ServeSigninPage(ctx, c)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError)
s.ServeErrorPage(ctx, c, err) s.ServeErrorPage(ctx, c, err)
return return
} }
@ -86,6 +95,7 @@ func NewHandler(s Service, staticDir string) http.Handler {
err := s.ServeTimelinePage(ctx, c, tType, maxID, minID) err := s.ServeTimelinePage(ctx, c, tType, maxID, minID)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError)
s.ServeErrorPage(ctx, c, err) s.ServeErrorPage(ctx, c, err)
return return
} }
@ -104,6 +114,7 @@ func NewHandler(s Service, staticDir string) http.Handler {
err := s.ServeThreadPage(ctx, c, id, len(reply) > 1) err := s.ServeThreadPage(ctx, c, id, len(reply) > 1)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError)
s.ServeErrorPage(ctx, c, err) s.ServeErrorPage(ctx, c, err)
return return
} }
@ -116,6 +127,7 @@ func NewHandler(s Service, staticDir string) http.Handler {
err := s.ServeLikedByPage(ctx, c, id) err := s.ServeLikedByPage(ctx, c, id)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError)
s.ServeErrorPage(ctx, c, err) s.ServeErrorPage(ctx, c, err)
return return
} }
@ -128,6 +140,7 @@ func NewHandler(s Service, staticDir string) http.Handler {
err := s.ServeRetweetedByPage(ctx, c, id) err := s.ServeRetweetedByPage(ctx, c, id)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError)
s.ServeErrorPage(ctx, c, err) s.ServeErrorPage(ctx, c, err)
return return
} }
@ -142,6 +155,7 @@ func NewHandler(s Service, staticDir string) http.Handler {
err := s.ServeFollowingPage(ctx, c, id, maxID, minID) err := s.ServeFollowingPage(ctx, c, id, maxID, minID)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError)
s.ServeErrorPage(ctx, c, err) s.ServeErrorPage(ctx, c, err)
return return
} }
@ -156,6 +170,7 @@ func NewHandler(s Service, staticDir string) http.Handler {
err := s.ServeFollowersPage(ctx, c, id, maxID, minID) err := s.ServeFollowersPage(ctx, c, id, maxID, minID)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError)
s.ServeErrorPage(ctx, c, err) s.ServeErrorPage(ctx, c, err)
return return
} }
@ -169,6 +184,7 @@ func NewHandler(s Service, staticDir string) http.Handler {
err := s.ServeNotificationPage(ctx, c, maxID, minID) err := s.ServeNotificationPage(ctx, c, maxID, minID)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError)
s.ServeErrorPage(ctx, c, err) s.ServeErrorPage(ctx, c, err)
return return
} }
@ -183,6 +199,7 @@ func NewHandler(s Service, staticDir string) http.Handler {
err := s.ServeUserPage(ctx, c, id, maxID, minID) err := s.ServeUserPage(ctx, c, id, maxID, minID)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError)
s.ServeErrorPage(ctx, c, err) s.ServeErrorPage(ctx, c, err)
return return
} }
@ -200,6 +217,7 @@ func NewHandler(s Service, staticDir string) http.Handler {
if len(offsetStr) > 1 { if len(offsetStr) > 1 {
offset, err = strconv.Atoi(offsetStr) offset, err = strconv.Atoi(offsetStr)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError)
s.ServeErrorPage(ctx, c, err) s.ServeErrorPage(ctx, c, err)
return return
} }
@ -207,6 +225,7 @@ func NewHandler(s Service, staticDir string) http.Handler {
err = s.ServeUserSearchPage(ctx, c, id, q, offset) err = s.ServeUserSearchPage(ctx, c, id, q, offset)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError)
s.ServeErrorPage(ctx, c, err) s.ServeErrorPage(ctx, c, err)
return return
} }
@ -218,6 +237,7 @@ func NewHandler(s Service, staticDir string) http.Handler {
err := s.ServeAboutPage(ctx, c) err := s.ServeAboutPage(ctx, c)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError)
s.ServeErrorPage(ctx, c, err) s.ServeErrorPage(ctx, c, err)
return return
} }
@ -229,6 +249,7 @@ func NewHandler(s Service, staticDir string) http.Handler {
err := s.ServeEmojiPage(ctx, c) err := s.ServeEmojiPage(ctx, c)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError)
s.ServeErrorPage(ctx, c, err) s.ServeErrorPage(ctx, c, err)
return return
} }
@ -246,6 +267,7 @@ func NewHandler(s Service, staticDir string) http.Handler {
if len(offsetStr) > 1 { if len(offsetStr) > 1 {
offset, err = strconv.Atoi(offsetStr) offset, err = strconv.Atoi(offsetStr)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError)
s.ServeErrorPage(ctx, c, err) s.ServeErrorPage(ctx, c, err)
return return
} }
@ -253,6 +275,7 @@ func NewHandler(s Service, staticDir string) http.Handler {
err = s.ServeSearchPage(ctx, c, q, qType, offset) err = s.ServeSearchPage(ctx, c, q, qType, offset)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError)
s.ServeErrorPage(ctx, c, err) s.ServeErrorPage(ctx, c, err)
return return
} }
@ -264,6 +287,7 @@ func NewHandler(s Service, staticDir string) http.Handler {
err := s.ServeSettingsPage(ctx, c) err := s.ServeSettingsPage(ctx, c)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError)
s.ServeErrorPage(ctx, c, err) s.ServeErrorPage(ctx, c, err)
return return
} }
@ -276,6 +300,7 @@ func NewHandler(s Service, staticDir string) http.Handler {
url, sessionID, err := s.NewSession(ctx, instance) url, sessionID, err := s.NewSession(ctx, instance)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError)
s.ServeErrorPage(ctx, c, err) s.ServeErrorPage(ctx, c, err)
return return
} }
@ -297,6 +322,7 @@ func NewHandler(s Service, staticDir string) http.Handler {
_, err := s.Signin(ctx, c, "", token) _, err := s.Signin(ctx, c, "", token)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError)
s.ServeErrorPage(ctx, c, err) s.ServeErrorPage(ctx, c, err)
return return
} }
@ -309,6 +335,7 @@ func NewHandler(s Service, staticDir string) http.Handler {
c := newClient(w) c := newClient(w)
err := req.ParseMultipartForm(4 << 20) err := req.ParseMultipartForm(4 << 20)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError)
s.ServeErrorPage(context.Background(), c, err) s.ServeErrorPage(context.Background(), c, err)
return return
} }
@ -324,6 +351,7 @@ func NewHandler(s Service, staticDir string) http.Handler {
id, err := s.Post(ctx, c, content, replyToID, format, visibility, isNSFW, files) id, err := s.Post(ctx, c, content, replyToID, format, visibility, isNSFW, files)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError)
s.ServeErrorPage(ctx, c, err) s.ServeErrorPage(ctx, c, err)
return return
} }
@ -344,6 +372,7 @@ func NewHandler(s Service, staticDir string) http.Handler {
_, err := s.Like(ctx, c, id) _, err := s.Like(ctx, c, id)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError)
s.ServeErrorPage(ctx, c, err) s.ServeErrorPage(ctx, c, err)
return return
} }
@ -364,6 +393,7 @@ func NewHandler(s Service, staticDir string) http.Handler {
_, err := s.UnLike(ctx, c, id) _, err := s.UnLike(ctx, c, id)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError)
s.ServeErrorPage(ctx, c, err) s.ServeErrorPage(ctx, c, err)
return return
} }
@ -384,6 +414,7 @@ func NewHandler(s Service, staticDir string) http.Handler {
_, err := s.Retweet(ctx, c, id) _, err := s.Retweet(ctx, c, id)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError)
s.ServeErrorPage(ctx, c, err) s.ServeErrorPage(ctx, c, err)
return return
} }
@ -404,6 +435,7 @@ func NewHandler(s Service, staticDir string) http.Handler {
_, err := s.UnRetweet(ctx, c, id) _, err := s.UnRetweet(ctx, c, id)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError)
s.ServeErrorPage(ctx, c, err) s.ServeErrorPage(ctx, c, err)
return return
} }
@ -424,6 +456,7 @@ func NewHandler(s Service, staticDir string) http.Handler {
err := s.Follow(ctx, c, id) err := s.Follow(ctx, c, id)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError)
s.ServeErrorPage(ctx, c, err) s.ServeErrorPage(ctx, c, err)
return return
} }
@ -439,6 +472,7 @@ func NewHandler(s Service, staticDir string) http.Handler {
err := s.UnFollow(ctx, c, id) err := s.UnFollow(ctx, c, id)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError)
s.ServeErrorPage(ctx, c, err) s.ServeErrorPage(ctx, c, err)
return return
} }
@ -468,6 +502,7 @@ func NewHandler(s Service, staticDir string) http.Handler {
err := s.SaveSettings(ctx, c, settings) err := s.SaveSettings(ctx, c, settings)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError)
s.ServeErrorPage(ctx, c, err) s.ServeErrorPage(ctx, c, err)
return return
} }
@ -494,13 +529,13 @@ func NewHandler(s Service, staticDir string) http.Handler {
count, err := s.Like(ctx, c, id) count, err := s.Like(ctx, c, id)
if err != nil { if err != nil {
s.ServeErrorPage(ctx, c, err) serveJsonError(w, err)
return return
} }
err = serveJson(w, count) err = serveJson(w, count)
if err != nil { if err != nil {
s.ServeErrorPage(ctx, c, err) serveJsonError(w, err)
return return
} }
} }
@ -511,13 +546,13 @@ func NewHandler(s Service, staticDir string) http.Handler {
id, _ := mux.Vars(req)["id"] id, _ := mux.Vars(req)["id"]
count, err := s.UnLike(ctx, c, id) count, err := s.UnLike(ctx, c, id)
if err != nil { if err != nil {
s.ServeErrorPage(ctx, c, err) serveJsonError(w, err)
return return
} }
err = serveJson(w, count) err = serveJson(w, count)
if err != nil { if err != nil {
s.ServeErrorPage(ctx, c, err) serveJsonError(w, err)
return return
} }
} }
@ -529,13 +564,13 @@ func NewHandler(s Service, staticDir string) http.Handler {
count, err := s.Retweet(ctx, c, id) count, err := s.Retweet(ctx, c, id)
if err != nil { if err != nil {
s.ServeErrorPage(ctx, c, err) serveJsonError(w, err)
return return
} }
err = serveJson(w, count) err = serveJson(w, count)
if err != nil { if err != nil {
s.ServeErrorPage(ctx, c, err) serveJsonError(w, err)
return return
} }
} }
@ -547,13 +582,13 @@ func NewHandler(s Service, staticDir string) http.Handler {
count, err := s.UnRetweet(ctx, c, id) count, err := s.UnRetweet(ctx, c, id)
if err != nil { if err != nil {
s.ServeErrorPage(ctx, c, err) serveJsonError(w, err)
return return
} }
err = serveJson(w, count) err = serveJson(w, count)
if err != nil { if err != nil {
s.ServeErrorPage(ctx, c, err) serveJsonError(w, err)
return return
} }
} }