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
|
return http.StatusOK, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) serveAnnounce(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
|
func handleError(err error, w *Writer) (int, error) {
|
||||||
ann, err := NewAnnounce(s.config, r, p)
|
if _, ok := err.(*models.ClientError); ok {
|
||||||
writer := &Writer{w}
|
w.WriteError(err)
|
||||||
|
|
||||||
if err == models.ErrMalformedRequest {
|
|
||||||
writer.WriteError(err)
|
|
||||||
stats.RecordEvent(stats.BlockedRequest)
|
stats.RecordEvent(stats.BlockedRequest)
|
||||||
return http.StatusOK, nil
|
return http.StatusOK, nil
|
||||||
} else if err != nil {
|
}
|
||||||
|
|
||||||
return http.StatusInternalServerError, err
|
return http.StatusInternalServerError, err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = s.tracker.HandleAnnounce(ann, writer)
|
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 == models.ErrBadRequest {
|
if err = s.tracker.HandleAnnounce(ann, writer); err != nil {
|
||||||
writer.WriteError(err)
|
return handleError(err, writer)
|
||||||
stats.RecordEvent(stats.BlockedRequest)
|
|
||||||
return http.StatusOK, nil
|
|
||||||
} else if err != nil {
|
|
||||||
return http.StatusInternalServerError, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
stats.RecordEvent(stats.Announce)
|
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) {
|
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}
|
writer := &Writer{w}
|
||||||
|
scrape, err := NewScrape(s.config, r, p)
|
||||||
if err == models.ErrMalformedRequest {
|
if err != nil {
|
||||||
writer.WriteError(err)
|
return handleError(err, writer)
|
||||||
stats.RecordEvent(stats.BlockedRequest)
|
|
||||||
return http.StatusOK, nil
|
|
||||||
} else if err != nil {
|
|
||||||
return http.StatusInternalServerError, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = s.tracker.HandleScrape(scrape, writer); err != nil {
|
if err = s.tracker.HandleScrape(scrape, writer); err != nil {
|
||||||
return http.StatusInternalServerError, err
|
return handleError(err, writer)
|
||||||
}
|
}
|
||||||
|
|
||||||
stats.RecordEvent(stats.Scrape)
|
stats.RecordEvent(stats.Scrape)
|
||||||
|
|
||||||
return http.StatusOK, nil
|
return http.StatusOK, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,36 +5,43 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"net"
|
"net"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/chihaya/chihaya/config"
|
"github.com/chihaya/chihaya/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ClientError struct {
|
||||||
|
message string
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// ErrMalformedRequest is returned when a request does not contain the
|
// ErrMalformedRequest is returned when a request does not contain the
|
||||||
// required parameters needed to create a model.
|
// 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
|
// ErrBadRequest is returned when a request is invalid in the peer's
|
||||||
// current state. For example, announcing a "completed" event while
|
// current state. For example, announcing a "completed" event while
|
||||||
// not a leecher or a "stopped" event while not active.
|
// 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 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 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 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 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.
|
// Peer is a participant in a swarm.
|
||||||
type Peer struct {
|
type Peer struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
|
|
Loading…
Reference in a new issue