diff --git a/http/routes.go b/http/routes.go index 63e8d68..9780f1a 100644 --- a/http/routes.go +++ b/http/routes.go @@ -23,6 +23,10 @@ func handleError(err error) (int, error) { return http.StatusOK, nil } else if _, ok := err.(models.ClientError); ok { stats.RecordEvent(stats.ClientError) + + if _, ok := err.(models.NotFoundError); ok { + return http.StatusNotFound, nil + } return http.StatusBadRequest, nil } 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) - if err == models.ErrTorrentDNE { - return http.StatusNotFound, err - } else if err != nil { - return http.StatusInternalServerError, err + if err != nil { + return handleError(err) } 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 } - err = conn.DeleteTorrent(infohash) - if err == models.ErrTorrentDNE { - return http.StatusNotFound, err - } - - return handleError(err) + return handleError(conn.DeleteTorrent(infohash)) } 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 } - err = conn.DeleteUser(p.ByName("passkey")) - if err == models.ErrUserDNE { - return http.StatusNotFound, err - } - - return handleError(err) + return handleError(conn.DeleteUser(p.ByName("passkey"))) } func (s *Server) putClient(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) { diff --git a/tracker/models/models.go b/tracker/models/models.go index 1eb0320..280bcf7 100644 --- a/tracker/models/models.go +++ b/tracker/models/models.go @@ -22,10 +22,10 @@ var ( ErrBadRequest = ClientError("bad request") // 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 = ClientError("torrent does not exist") + ErrTorrentDNE = NotFoundError("torrent does not exist") // ErrClientUnapproved is returned when a clientID is not in the whitelist. ErrClientUnapproved = ClientError("client is not approved") @@ -35,8 +35,10 @@ var ( ) 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. type Peer struct {