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/julienschmidt/httprouter"
"github.com/chihaya/chihaya/stats" "github.com/chihaya/chihaya/stats"
"github.com/chihaya/chihaya/tracker"
"github.com/chihaya/chihaya/tracker/models" "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) torrent, err := conn.FindTorrent(infohash)
if err == tracker.ErrTorrentDNE { if err == models.ErrTorrentDNE {
return http.StatusNotFound, err return http.StatusNotFound, err
} else if err != nil { } else if err != nil {
return http.StatusInternalServerError, err return http.StatusInternalServerError, err
@ -144,7 +143,7 @@ func (s *Server) delTorrent(w http.ResponseWriter, r *http.Request, p httprouter
} }
err = conn.DeleteTorrent(infohash) err = conn.DeleteTorrent(infohash)
if err == tracker.ErrTorrentDNE { if err == models.ErrTorrentDNE {
return http.StatusNotFound, err return http.StatusNotFound, err
} else if err != nil { } else if err != nil {
return http.StatusInternalServerError, err 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")) user, err := conn.FindUser(p.ByName("passkey"))
if err == tracker.ErrUserDNE { if err == models.ErrUserDNE {
return http.StatusNotFound, err return http.StatusNotFound, err
} else if err != nil { } else if err != nil {
return http.StatusInternalServerError, err 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")) err = conn.DeleteUser(p.ByName("passkey"))
if err == tracker.ErrUserDNE { if err == models.ErrUserDNE {
return http.StatusNotFound, err return http.StatusNotFound, err
} else if err != nil { } else if err != nil {
return http.StatusInternalServerError, err return http.StatusInternalServerError, err

View file

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

View file

@ -5,7 +5,6 @@
package tracker package tracker
import ( import (
"errors"
"fmt" "fmt"
"time" "time"
@ -13,18 +12,7 @@ import (
"github.com/chihaya/chihaya/tracker/models" "github.com/chihaya/chihaya/tracker/models"
) )
var ( var drivers = make(map[string]Driver)
// 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)
)
// Driver represents an interface to pool of connections to models used for // Driver represents an interface to pool of connections to models used for
// the tracker. // the tracker.

View file

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

View file

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