More refactoring of error handling

This commit is contained in:
Justin Li 2014-07-25 03:05:43 -04:00
parent 683e90631a
commit 818bef28d7
2 changed files with 13 additions and 19 deletions

View file

@ -23,6 +23,10 @@ func handleError(err error) (int, error) {
return http.StatusOK, nil return http.StatusOK, nil
} else if _, ok := err.(models.ClientError); ok { } else if _, ok := err.(models.ClientError); ok {
stats.RecordEvent(stats.ClientError) stats.RecordEvent(stats.ClientError)
if _, ok := err.(models.NotFoundError); ok {
return http.StatusNotFound, nil
}
return http.StatusBadRequest, nil return http.StatusBadRequest, nil
} }
return http.StatusInternalServerError, err return http.StatusInternalServerError, err
@ -107,10 +111,8 @@ func (s *Server) getTorrent(w http.ResponseWriter, r *http.Request, p httprouter
} }
torrent, err := conn.FindTorrent(infohash) torrent, err := conn.FindTorrent(infohash)
if err == models.ErrTorrentDNE { if err != nil {
return http.StatusNotFound, err return handleError(err)
} else if err != nil {
return http.StatusInternalServerError, err
} }
w.Header().Set("Content-Type", jsonContentType) w.Header().Set("Content-Type", jsonContentType)
@ -149,12 +151,7 @@ func (s *Server) delTorrent(w http.ResponseWriter, r *http.Request, p httprouter
return http.StatusNotFound, err return http.StatusNotFound, err
} }
err = conn.DeleteTorrent(infohash) return handleError(conn.DeleteTorrent(infohash))
if err == models.ErrTorrentDNE {
return http.StatusNotFound, err
}
return handleError(err)
} }
func (s *Server) getUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) { func (s *Server) getUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
@ -201,12 +198,7 @@ func (s *Server) delUser(w http.ResponseWriter, r *http.Request, p httprouter.Pa
return http.StatusInternalServerError, err return http.StatusInternalServerError, err
} }
err = conn.DeleteUser(p.ByName("passkey")) return handleError(conn.DeleteUser(p.ByName("passkey")))
if err == models.ErrUserDNE {
return http.StatusNotFound, err
}
return handleError(err)
} }
func (s *Server) putClient(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) { func (s *Server) putClient(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {

View file

@ -22,10 +22,10 @@ var (
ErrBadRequest = ClientError("bad request") ErrBadRequest = ClientError("bad request")
// ErrUserDNE is returned when a user does not exist. // ErrUserDNE is returned when a user does not exist.
ErrUserDNE = ClientError("user does not exist") ErrUserDNE = NotFoundError("user does not exist")
// ErrTorrentDNE is returned when a torrent does not exist. // ErrTorrentDNE is returned when a torrent does not exist.
ErrTorrentDNE = ClientError("torrent does not exist") ErrTorrentDNE = NotFoundError("torrent does not exist")
// ErrClientUnapproved is returned when a clientID is not in the whitelist. // ErrClientUnapproved is returned when a clientID is not in the whitelist.
ErrClientUnapproved = ClientError("client is not approved") ErrClientUnapproved = ClientError("client is not approved")
@ -35,8 +35,10 @@ var (
) )
type ClientError string type ClientError string
type NotFoundError ClientError
func (c ClientError) Error() string { return string(c) } func (e ClientError) Error() string { return string(e) }
func (e NotFoundError) Error() string { return string(e) }
// Peer is a participant in a swarm. // Peer is a participant in a swarm.
type Peer struct { type Peer struct {