Move errors to tracker/models

This commit is contained in:
Justin Li 2014-07-23 00:37:30 -04:00
parent 6c7abdfa2d
commit 6396990e52
6 changed files with 36 additions and 38 deletions

View file

@ -13,7 +13,6 @@ import (
"github.com/julienschmidt/httprouter"
"github.com/chihaya/chihaya/stats"
"github.com/chihaya/chihaya/tracker"
"github.com/chihaya/chihaya/tracker/models"
)
@ -91,7 +90,7 @@ func (s *Server) getTorrent(w http.ResponseWriter, r *http.Request, p httprouter
}
torrent, err := conn.FindTorrent(infohash)
if err == tracker.ErrTorrentDNE {
if err == models.ErrTorrentDNE {
return http.StatusNotFound, err
} else if err != nil {
return http.StatusInternalServerError, err
@ -144,7 +143,7 @@ func (s *Server) delTorrent(w http.ResponseWriter, r *http.Request, p httprouter
}
err = conn.DeleteTorrent(infohash)
if err == tracker.ErrTorrentDNE {
if err == models.ErrTorrentDNE {
return http.StatusNotFound, err
} else if err != nil {
return http.StatusInternalServerError, err
@ -160,7 +159,7 @@ func (s *Server) getUser(w http.ResponseWriter, r *http.Request, p httprouter.Pa
}
user, err := conn.FindUser(p.ByName("passkey"))
if err == tracker.ErrUserDNE {
if err == models.ErrUserDNE {
return http.StatusNotFound, err
} else if err != nil {
return http.StatusInternalServerError, err
@ -208,7 +207,7 @@ func (s *Server) delUser(w http.ResponseWriter, r *http.Request, p httprouter.Pa
}
err = conn.DeleteUser(p.ByName("passkey"))
if err == tracker.ErrUserDNE {
if err == models.ErrUserDNE {
return http.StatusNotFound, err
} else if err != nil {
return http.StatusInternalServerError, err

View file

@ -23,7 +23,7 @@ func (tkr *Tracker) HandleAnnounce(ann *models.Announce, w Writer) error {
if tkr.cfg.Whitelist {
err = conn.FindClient(ann.ClientID())
if err == ErrClientUnapproved {
if err == models.ErrClientUnapproved {
w.WriteError(err)
return nil
} else if err != nil {
@ -34,7 +34,7 @@ func (tkr *Tracker) HandleAnnounce(ann *models.Announce, w Writer) error {
var user *models.User
if tkr.cfg.Private {
user, err = conn.FindUser(ann.Passkey)
if err == ErrUserDNE {
if err == models.ErrUserDNE {
w.WriteError(err)
return nil
} else if err != nil {
@ -45,7 +45,7 @@ func (tkr *Tracker) HandleAnnounce(ann *models.Announce, w Writer) error {
var torrent *models.Torrent
torrent, err = conn.FindTorrent(ann.Infohash)
switch {
case !tkr.cfg.Private && err == ErrTorrentDNE:
case !tkr.cfg.Private && err == models.ErrTorrentDNE:
torrent = &models.Torrent{
Infohash: ann.Infohash,
Seeders: models.PeerMap{},
@ -58,7 +58,7 @@ func (tkr *Tracker) HandleAnnounce(ann *models.Announce, w Writer) error {
}
stats.RecordEvent(stats.NewTorrent)
case tkr.cfg.Private && err == ErrTorrentDNE:
case tkr.cfg.Private && err == models.ErrTorrentDNE:
w.WriteError(err)
return nil

View file

@ -5,7 +5,6 @@
package tracker
import (
"errors"
"fmt"
"time"
@ -13,18 +12,7 @@ import (
"github.com/chihaya/chihaya/tracker/models"
)
var (
// ErrUserDNE is returned when a user does not exist.
ErrUserDNE = errors.New("user does not exist")
// ErrTorrentDNE is returned when a torrent does not exist.
ErrTorrentDNE = errors.New("torrent does not exist")
// ErrClientUnapproved is returned when a clientID is not in the whitelist.
ErrClientUnapproved = errors.New("client is not approved")
// ErrInvalidPasskey is returned when a passkey is not properly formatted.
ErrInvalidPasskey = errors.New("passkey is invalid")
drivers = make(map[string]Driver)
)
var drivers = make(map[string]Driver)
// Driver represents an interface to pool of connections to models used for
// the tracker.

View file

@ -9,7 +9,6 @@ import (
"time"
"github.com/chihaya/chihaya/stats"
"github.com/chihaya/chihaya/tracker"
"github.com/chihaya/chihaya/tracker/models"
)
@ -28,7 +27,7 @@ func (c *Conn) FindUser(passkey string) (*models.User, error) {
user, exists := c.users[passkey]
if !exists {
return nil, tracker.ErrUserDNE
return nil, models.ErrUserDNE
}
return &*user, nil
}
@ -39,7 +38,7 @@ func (c *Conn) FindTorrent(infohash string) (*models.Torrent, error) {
torrent, exists := c.torrents[infohash]
if !exists {
return nil, tracker.ErrTorrentDNE
return nil, models.ErrTorrentDNE
}
return &*torrent, nil
}
@ -50,7 +49,7 @@ func (c *Conn) FindClient(peerID string) error {
_, ok := c.whitelist[peerID]
if !ok {
return tracker.ErrClientUnapproved
return models.ErrClientUnapproved
}
return nil
}
@ -61,7 +60,7 @@ func (c *Conn) IncrementTorrentSnatches(infohash string) error {
t, exists := c.torrents[infohash]
if !exists {
return tracker.ErrTorrentDNE
return models.ErrTorrentDNE
}
t.Snatches++
@ -74,7 +73,7 @@ func (c *Conn) IncrementUserSnatches(userID string) error {
u, exists := c.users[userID]
if !exists {
return tracker.ErrUserDNE
return models.ErrUserDNE
}
u.Snatches++
@ -87,7 +86,7 @@ func (c *Conn) TouchTorrent(infohash string) error {
t, ok := c.torrents[infohash]
if !ok {
return tracker.ErrTorrentDNE
return models.ErrTorrentDNE
}
t.LastAction = time.Now().Unix()
@ -100,7 +99,7 @@ func (c *Conn) AddLeecher(infohash string, p *models.Peer) error {
t, ok := c.torrents[infohash]
if !ok {
return tracker.ErrTorrentDNE
return models.ErrTorrentDNE
}
t.Leechers[p.ID] = *p
@ -113,7 +112,7 @@ func (c *Conn) AddSeeder(infohash string, p *models.Peer) error {
t, ok := c.torrents[infohash]
if !ok {
return tracker.ErrTorrentDNE
return models.ErrTorrentDNE
}
t.Seeders[p.ID] = *p
@ -126,7 +125,7 @@ func (c *Conn) DeleteLeecher(infohash, peerkey string) error {
t, ok := c.torrents[infohash]
if !ok {
return tracker.ErrTorrentDNE
return models.ErrTorrentDNE
}
delete(t.Leechers, peerkey)
@ -139,7 +138,7 @@ func (c *Conn) DeleteSeeder(infohash, peerkey string) error {
t, ok := c.torrents[infohash]
if !ok {
return tracker.ErrTorrentDNE
return models.ErrTorrentDNE
}
delete(t.Seeders, peerkey)
@ -152,7 +151,7 @@ func (c *Conn) PutLeecher(infohash string, p *models.Peer) error {
t, ok := c.torrents[infohash]
if !ok {
return tracker.ErrTorrentDNE
return models.ErrTorrentDNE
}
t.Leechers[p.ID] = *p
@ -165,7 +164,7 @@ func (c *Conn) PutSeeder(infohash string, p *models.Peer) error {
t, ok := c.torrents[infohash]
if !ok {
return tracker.ErrTorrentDNE
return models.ErrTorrentDNE
}
t.Seeders[p.ID] = *p
@ -196,7 +195,7 @@ func (c *Conn) PurgeInactiveTorrent(infohash string) error {
torrent, exists := c.torrents[infohash]
if !exists {
return tracker.ErrTorrentDNE
return models.ErrTorrentDNE
}
if torrent.PeerCount() == 0 {

View file

@ -21,6 +21,18 @@ var (
// current state. For example, announcing a "completed" event while
// not a leecher or a "stopped" event while not active.
ErrBadRequest = errors.New("bad request")
// ErrUserDNE is returned when a user does not exist.
ErrUserDNE = errors.New("user does not exist")
// ErrTorrentDNE is returned when a torrent does not exist.
ErrTorrentDNE = errors.New("torrent does not exist")
// ErrClientUnapproved is returned when a clientID is not in the whitelist.
ErrClientUnapproved = errors.New("client is not approved")
// ErrInvalidPasskey is returned when a passkey is not properly formatted.
ErrInvalidPasskey = errors.New("passkey is invalid")
)
// Peer is a participant in a swarm.

View file

@ -18,7 +18,7 @@ func (tkr *Tracker) HandleScrape(scrape *models.Scrape, w Writer) (err error) {
if tkr.cfg.Private {
_, err = conn.FindUser(scrape.Passkey)
if err == ErrUserDNE {
if err == models.ErrUserDNE {
w.WriteError(err)
return nil
} else if err != nil {
@ -29,7 +29,7 @@ func (tkr *Tracker) HandleScrape(scrape *models.Scrape, w Writer) (err error) {
var torrents []*models.Torrent
for _, infohash := range scrape.Infohashes {
torrent, err := conn.FindTorrent(infohash)
if err == ErrTorrentDNE {
if err == models.ErrTorrentDNE {
w.WriteError(err)
return nil
} else if err != nil {