Create a ClientError type to distinguish between failure modes
This commit is contained in:
parent
1a79bdeb6b
commit
02f67b4259
2 changed files with 32 additions and 32 deletions
|
@ -38,26 +38,25 @@ func (s *Server) stats(w http.ResponseWriter, r *http.Request, p httprouter.Para
|
|||
return http.StatusOK, nil
|
||||
}
|
||||
|
||||
func (s *Server) serveAnnounce(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
|
||||
ann, err := NewAnnounce(s.config, r, p)
|
||||
writer := &Writer{w}
|
||||
|
||||
if err == models.ErrMalformedRequest {
|
||||
writer.WriteError(err)
|
||||
func handleError(err error, w *Writer) (int, error) {
|
||||
if _, ok := err.(*models.ClientError); ok {
|
||||
w.WriteError(err)
|
||||
stats.RecordEvent(stats.BlockedRequest)
|
||||
return http.StatusOK, nil
|
||||
} else if err != nil {
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
err = s.tracker.HandleAnnounce(ann, writer)
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
if err == models.ErrBadRequest {
|
||||
writer.WriteError(err)
|
||||
stats.RecordEvent(stats.BlockedRequest)
|
||||
return http.StatusOK, nil
|
||||
} else if err != nil {
|
||||
return http.StatusInternalServerError, err
|
||||
func (s *Server) serveAnnounce(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
|
||||
writer := &Writer{w}
|
||||
ann, err := NewAnnounce(s.config, r, p)
|
||||
if err != nil {
|
||||
return handleError(err, writer)
|
||||
}
|
||||
|
||||
if err = s.tracker.HandleAnnounce(ann, writer); err != nil {
|
||||
return handleError(err, writer)
|
||||
}
|
||||
|
||||
stats.RecordEvent(stats.Announce)
|
||||
|
@ -65,23 +64,17 @@ func (s *Server) serveAnnounce(w http.ResponseWriter, r *http.Request, p httprou
|
|||
}
|
||||
|
||||
func (s *Server) serveScrape(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
|
||||
scrape, err := NewScrape(s.config, r, p)
|
||||
writer := &Writer{w}
|
||||
|
||||
if err == models.ErrMalformedRequest {
|
||||
writer.WriteError(err)
|
||||
stats.RecordEvent(stats.BlockedRequest)
|
||||
return http.StatusOK, nil
|
||||
} else if err != nil {
|
||||
return http.StatusInternalServerError, err
|
||||
scrape, err := NewScrape(s.config, r, p)
|
||||
if err != nil {
|
||||
return handleError(err, writer)
|
||||
}
|
||||
|
||||
if err = s.tracker.HandleScrape(scrape, writer); err != nil {
|
||||
return http.StatusInternalServerError, err
|
||||
return handleError(err, writer)
|
||||
}
|
||||
|
||||
stats.RecordEvent(stats.Scrape)
|
||||
|
||||
return http.StatusOK, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -5,36 +5,43 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/chihaya/chihaya/config"
|
||||
)
|
||||
|
||||
type ClientError struct {
|
||||
message string
|
||||
}
|
||||
|
||||
var (
|
||||
// ErrMalformedRequest is returned when a request does not contain the
|
||||
// required parameters needed to create a model.
|
||||
ErrMalformedRequest = errors.New("malformed request")
|
||||
ErrMalformedRequest = &ClientError{"malformed request"}
|
||||
|
||||
// ErrBadRequest is returned when a request is invalid in the peer's
|
||||
// current state. For example, announcing a "completed" event while
|
||||
// not a leecher or a "stopped" event while not active.
|
||||
ErrBadRequest = errors.New("bad request")
|
||||
ErrBadRequest = &ClientError{"bad request"}
|
||||
|
||||
// ErrUserDNE is returned when a user does not exist.
|
||||
ErrUserDNE = errors.New("user does not exist")
|
||||
ErrUserDNE = &ClientError{"user does not exist"}
|
||||
|
||||
// ErrTorrentDNE is returned when a torrent does not exist.
|
||||
ErrTorrentDNE = errors.New("torrent does not exist")
|
||||
ErrTorrentDNE = &ClientError{"torrent does not exist"}
|
||||
|
||||
// ErrClientUnapproved is returned when a clientID is not in the whitelist.
|
||||
ErrClientUnapproved = errors.New("client is not approved")
|
||||
ErrClientUnapproved = &ClientError{"client is not approved"}
|
||||
|
||||
// ErrInvalidPasskey is returned when a passkey is not properly formatted.
|
||||
ErrInvalidPasskey = errors.New("passkey is invalid")
|
||||
ErrInvalidPasskey = &ClientError{"passkey is invalid"}
|
||||
)
|
||||
|
||||
func (e *ClientError) Error() string {
|
||||
return e.message
|
||||
}
|
||||
|
||||
// Peer is a participant in a swarm.
|
||||
type Peer struct {
|
||||
ID string `json:"id"`
|
||||
|
|
Loading…
Reference in a new issue