Improve http error logging
This commit is contained in:
@@ -119,7 +119,7 @@ func (s *Server) serveHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
case http.MethodGet:
|
case http.MethodGet:
|
||||||
s.handleGetStream(w, r)
|
s.handleGetStream(w, r)
|
||||||
default:
|
default:
|
||||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
s.writeError(w, r, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
@@ -132,12 +132,12 @@ func (s *Server) handleGetStream(w http.ResponseWriter, r *http.Request) {
|
|||||||
// TODO: Listen for all databases matching query criteria.
|
// TODO: Listen for all databases matching query criteria.
|
||||||
path := q.Get("path")
|
path := q.Get("path")
|
||||||
if path == "" {
|
if path == "" {
|
||||||
http.Error(w, "Database name required", http.StatusBadRequest)
|
s.writeError(w, r, "Database name required", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
db := s.server.DB(path)
|
db := s.server.DB(path)
|
||||||
if db == nil {
|
if db == nil {
|
||||||
http.Error(w, "Database not found", http.StatusNotFound)
|
s.writeError(w, r, "Database not found", http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -149,7 +149,7 @@ func (s *Server) handleGetStream(w http.ResponseWriter, r *http.Request) {
|
|||||||
// Determine starting position.
|
// Determine starting position.
|
||||||
pos := db.Pos()
|
pos := db.Pos()
|
||||||
if pos.Generation == "" {
|
if pos.Generation == "" {
|
||||||
http.Error(w, "No generation available", http.StatusServiceUnavailable)
|
s.writeError(w, r, "No generation available", http.StatusServiceUnavailable)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
pos.Offset = 0
|
pos.Offset = 0
|
||||||
@@ -160,7 +160,7 @@ func (s *Server) handleGetStream(w http.ResponseWriter, r *http.Request) {
|
|||||||
// Obtain iterator before snapshot so we don't miss any WAL segments.
|
// Obtain iterator before snapshot so we don't miss any WAL segments.
|
||||||
itr, err := db.WALSegments(r.Context(), pos.Generation)
|
itr, err := db.WALSegments(r.Context(), pos.Generation)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, fmt.Sprintf("Cannot obtain WAL iterator: %s", err), http.StatusInternalServerError)
|
s.writeError(w, r, fmt.Sprintf("Cannot obtain WAL iterator: %s", err), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer itr.Close()
|
defer itr.Close()
|
||||||
@@ -191,7 +191,7 @@ func (s *Server) handleGetStream(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
s.writeError(w, r, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -262,3 +262,8 @@ func (s *Server) handleGetStream(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Server) writeError(w http.ResponseWriter, r *http.Request, err string, code int) {
|
||||||
|
s.Logger.Printf("error: %s", err)
|
||||||
|
http.Error(w, err, code)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user