aboutsummaryrefslogtreecommitdiffstats
path: root/swarm/api/http/server.go
diff options
context:
space:
mode:
authorholisticode <holistic.computing@gmail.com>2017-09-09 02:29:09 +0800
committerFelix Lange <fjl@users.noreply.github.com>2017-09-09 02:29:09 +0800
commitac193e36ce4bce752717124433a8ce84c347dbf7 (patch)
treeaa94c7b2db1557d05db6bb8e83828749026255cf /swarm/api/http/server.go
parent5ba9225fe3b06d5512976b808bc87d92d03d54ba (diff)
downloadgo-tangerine-ac193e36ce4bce752717124433a8ce84c347dbf7.tar
go-tangerine-ac193e36ce4bce752717124433a8ce84c347dbf7.tar.gz
go-tangerine-ac193e36ce4bce752717124433a8ce84c347dbf7.tar.bz2
go-tangerine-ac193e36ce4bce752717124433a8ce84c347dbf7.tar.lz
go-tangerine-ac193e36ce4bce752717124433a8ce84c347dbf7.tar.xz
go-tangerine-ac193e36ce4bce752717124433a8ce84c347dbf7.tar.zst
go-tangerine-ac193e36ce4bce752717124433a8ce84c347dbf7.zip
swarm/api/http: add error pages (#14967)
Diffstat (limited to 'swarm/api/http/server.go')
-rw-r--r--swarm/api/http/server.go37
1 files changed, 21 insertions, 16 deletions
diff --git a/swarm/api/http/server.go b/swarm/api/http/server.go
index 0b4ec7e18..a637b8735 100644
--- a/swarm/api/http/server.go
+++ b/swarm/api/http/server.go
@@ -332,7 +332,7 @@ func (s *Server) HandleGetRaw(w http.ResponseWriter, r *Request) {
return api.SkipManifest
})
if entry == nil {
- http.NotFound(w, &r.Request)
+ s.NotFound(w, r, fmt.Errorf("Manifest entry could not be loaded"))
return
}
key = storage.Key(common.Hex2Bytes(entry.Hash))
@@ -341,8 +341,7 @@ func (s *Server) HandleGetRaw(w http.ResponseWriter, r *Request) {
// check the root chunk exists by retrieving the file's size
reader := s.api.Retrieve(key)
if _, err := reader.Size(nil); err != nil {
- s.logDebug("key not found %s: %s", key, err)
- http.NotFound(w, &r.Request)
+ s.NotFound(w, r, fmt.Errorf("Root chunk not found %s: %s", key, err))
return
}
@@ -534,16 +533,20 @@ func (s *Server) HandleGetFile(w http.ResponseWriter, r *Request) {
return
}
- reader, contentType, _, err := s.api.Get(key, r.uri.Path)
+ reader, contentType, status, err := s.api.Get(key, r.uri.Path)
if err != nil {
- s.Error(w, r, err)
+ switch status {
+ case http.StatusNotFound:
+ s.NotFound(w, r, err)
+ default:
+ s.Error(w, r, err)
+ }
return
}
// check the root chunk exists by retrieving the file's size
if _, err := reader.Size(nil); err != nil {
- s.logDebug("file not found %s: %s", r.uri, err)
- http.NotFound(w, &r.Request)
+ s.NotFound(w, r, fmt.Errorf("File not found %s: %s", r.uri, err))
return
}
@@ -556,14 +559,14 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.logDebug("HTTP %s request URL: '%s', Host: '%s', Path: '%s', Referer: '%s', Accept: '%s'", r.Method, r.RequestURI, r.URL.Host, r.URL.Path, r.Referer(), r.Header.Get("Accept"))
uri, err := api.Parse(strings.TrimLeft(r.URL.Path, "/"))
+ req := &Request{Request: *r, uri: uri}
if err != nil {
s.logError("Invalid URI %q: %s", r.URL.Path, err)
- http.Error(w, fmt.Sprintf("Invalid bzz URI: %s", err), http.StatusBadRequest)
+ s.BadRequest(w, req, fmt.Sprintf("Invalid URI %q: %s", r.URL.Path, err))
return
}
s.logDebug("%s request received for %s", r.Method, uri)
- req := &Request{Request: *r, uri: uri}
switch r.Method {
case "POST":
if uri.Raw() {
@@ -579,7 +582,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// strictly a traditional PUT request which replaces content
// at a URI, and POST is more ubiquitous)
if uri.Raw() {
- http.Error(w, fmt.Sprintf("No PUT to %s allowed.", uri), http.StatusBadRequest)
+ ShowError(w, r, fmt.Sprintf("No PUT to %s allowed.", uri), http.StatusBadRequest)
return
} else {
s.HandlePostFiles(w, req)
@@ -587,7 +590,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
case "DELETE":
if uri.Raw() {
- http.Error(w, fmt.Sprintf("No DELETE to %s allowed.", uri), http.StatusBadRequest)
+ ShowError(w, r, fmt.Sprintf("No DELETE to %s allowed.", uri), http.StatusBadRequest)
return
}
s.HandleDelete(w, req)
@@ -611,7 +614,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.HandleGetFile(w, req)
default:
- http.Error(w, "Method "+r.Method+" is not supported.", http.StatusMethodNotAllowed)
+ ShowError(w, r, fmt.Sprintf("Method "+r.Method+" is not supported.", uri), http.StatusMethodNotAllowed)
}
}
@@ -643,11 +646,13 @@ func (s *Server) logError(format string, v ...interface{}) {
}
func (s *Server) BadRequest(w http.ResponseWriter, r *Request, reason string) {
- s.logDebug("bad request %s %s: %s", r.Method, r.uri, reason)
- http.Error(w, reason, http.StatusBadRequest)
+ ShowError(w, &r.Request, fmt.Sprintf("Bad request %s %s: %s", r.Method, r.uri, reason), http.StatusBadRequest)
}
func (s *Server) Error(w http.ResponseWriter, r *Request, err error) {
- s.logError("error serving %s %s: %s", r.Method, r.uri, err)
- http.Error(w, err.Error(), http.StatusInternalServerError)
+ ShowError(w, &r.Request, fmt.Sprintf("Error serving %s %s: %s", r.Method, r.uri, err), http.StatusInternalServerError)
+}
+
+func (s *Server) NotFound(w http.ResponseWriter, r *Request, err error) {
+ ShowError(w, &r.Request, fmt.Sprintf("NOT FOUND error serving %s %s: %s", r.Method, r.uri, err), http.StatusNotFound)
}