aboutsummaryrefslogtreecommitdiffstats
path: root/http/router.go
diff options
context:
space:
mode:
Diffstat (limited to 'http/router.go')
-rw-r--r--http/router.go31
1 files changed, 14 insertions, 17 deletions
diff --git a/http/router.go b/http/router.go
index 27adc33..2e6b77b 100644
--- a/http/router.go
+++ b/http/router.go
@@ -15,32 +15,29 @@ type route struct {
handler appHandler
}
-type appHandler func(http.ResponseWriter, *http.Request) (interface{}, *httpError)
+type appHandler func(http.ResponseWriter, *http.Request) *httpError
func (fn appHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
- data, e := fn(w, r)
- w.Header().Set("Content-Type", "application/json")
- if e != nil { // e is *Error, not os.Error.
+ if e := fn(w, r); e != nil { // e is *httpError, not os.Error.
if e.Message == "" {
e.Message = e.err.Error()
}
- out, err := json.Marshal(e)
- if err != nil {
- panic(err)
- }
w.WriteHeader(e.Status)
- w.Write(out)
- } else if data != nil {
- out, err := json.Marshal(data)
- if err != nil {
- panic(err)
+ if w.Header().Get("Content-Type") == jsonMediaType {
+ out, err := json.Marshal(e)
+ if err != nil {
+ panic(err)
+ }
+ w.Write(out)
+ } else {
+ w.Write([]byte(e.Message))
}
- w.Write(out)
}
}
-func notFoundHandler(w http.ResponseWriter, r *http.Request) (interface{}, *httpError) {
- return nil, &httpError{
+func notFoundHandler(w http.ResponseWriter, r *http.Request) *httpError {
+ writeJSONHeader(w)
+ return &httpError{
Status: http.StatusNotFound,
Message: "Resource not found",
}
@@ -57,7 +54,7 @@ func (r *router) route(method, path string, handler appHandler) *route {
}
func (r *router) handler() http.Handler {
- return appHandler(func(w http.ResponseWriter, req *http.Request) (interface{}, *httpError) {
+ return appHandler(func(w http.ResponseWriter, req *http.Request) *httpError {
for _, route := range r.routes {
if route.match(req) {
return route.handler(w, req)