errors: remove errors package
This commit is contained in:
parent
50e5ff85af
commit
82b4395b11
6 changed files with 25 additions and 70 deletions
|
@ -1,41 +0,0 @@
|
|||
// Copyright 2016 The Chihaya Authors. All rights reserved.
|
||||
// Use of this source code is governed by the BSD 2-Clause license,
|
||||
// which can be found in the LICENSE file.
|
||||
|
||||
package errors
|
||||
|
||||
import "net/http"
|
||||
|
||||
type Error struct {
|
||||
message string
|
||||
public bool
|
||||
status int
|
||||
}
|
||||
|
||||
func (e *Error) Error() string {
|
||||
return e.message
|
||||
}
|
||||
|
||||
func (e *Error) Public() bool {
|
||||
return e.public
|
||||
}
|
||||
|
||||
func (e *Error) Status() int {
|
||||
return e.status
|
||||
}
|
||||
|
||||
func NewBadRequest(msg string) error {
|
||||
return &Error{
|
||||
message: msg,
|
||||
public: true,
|
||||
status: http.StatusBadRequest,
|
||||
}
|
||||
}
|
||||
|
||||
func NewMessage(msg string) error {
|
||||
return &Error{
|
||||
message: msg,
|
||||
public: true,
|
||||
status: http.StatusOK,
|
||||
}
|
||||
}
|
|
@ -9,9 +9,9 @@ import (
|
|||
"net/http"
|
||||
|
||||
"github.com/chihaya/chihaya"
|
||||
"github.com/chihaya/chihaya/errors"
|
||||
"github.com/chihaya/chihaya/pkg/event"
|
||||
"github.com/chihaya/chihaya/server/http/query"
|
||||
"github.com/chihaya/chihaya/tracker"
|
||||
)
|
||||
|
||||
func announceRequest(r *http.Request, cfg *httpConfig) (*chihaya.AnnounceRequest, error) {
|
||||
|
@ -24,11 +24,11 @@ func announceRequest(r *http.Request, cfg *httpConfig) (*chihaya.AnnounceRequest
|
|||
|
||||
eventStr, err := q.String("event")
|
||||
if err != nil {
|
||||
return nil, errors.NewBadRequest("failed to parse parameter: event")
|
||||
return nil, tracker.ClientError("failed to parse parameter: event")
|
||||
}
|
||||
request.Event, err = event.New(eventStr)
|
||||
if err != nil {
|
||||
return nil, errors.NewBadRequest("failed to provide valid client event")
|
||||
return nil, tracker.ClientError("failed to provide valid client event")
|
||||
}
|
||||
|
||||
compactStr, _ := q.String("compact")
|
||||
|
@ -36,32 +36,32 @@ func announceRequest(r *http.Request, cfg *httpConfig) (*chihaya.AnnounceRequest
|
|||
|
||||
infoHashes := q.InfoHashes()
|
||||
if len(infoHashes) < 1 {
|
||||
return nil, errors.NewBadRequest("no info_hash parameter supplied")
|
||||
return nil, tracker.ClientError("no info_hash parameter supplied")
|
||||
}
|
||||
if len(infoHashes) > 1 {
|
||||
return nil, errors.NewBadRequest("multiple info_hash parameters supplied")
|
||||
return nil, tracker.ClientError("multiple info_hash parameters supplied")
|
||||
}
|
||||
request.InfoHash = infoHashes[0]
|
||||
|
||||
peerID, err := q.String("peer_id")
|
||||
if err != nil {
|
||||
return nil, errors.NewBadRequest("failed to parse parameter: peer_id")
|
||||
return nil, tracker.ClientError("failed to parse parameter: peer_id")
|
||||
}
|
||||
request.PeerID = chihaya.PeerID(peerID)
|
||||
|
||||
request.Left, err = q.Uint64("left")
|
||||
if err != nil {
|
||||
return nil, errors.NewBadRequest("failed to parse parameter: left")
|
||||
return nil, tracker.ClientError("failed to parse parameter: left")
|
||||
}
|
||||
|
||||
request.Downloaded, err = q.Uint64("downloaded")
|
||||
if err != nil {
|
||||
return nil, errors.NewBadRequest("failed to parse parameter: downloaded")
|
||||
return nil, tracker.ClientError("failed to parse parameter: downloaded")
|
||||
}
|
||||
|
||||
request.Uploaded, err = q.Uint64("uploaded")
|
||||
if err != nil {
|
||||
return nil, errors.NewBadRequest("failed to parse parameter: uploaded")
|
||||
return nil, tracker.ClientError("failed to parse parameter: uploaded")
|
||||
}
|
||||
|
||||
numwant, _ := q.Uint64("numwant")
|
||||
|
@ -69,13 +69,13 @@ func announceRequest(r *http.Request, cfg *httpConfig) (*chihaya.AnnounceRequest
|
|||
|
||||
port, err := q.Uint64("port")
|
||||
if err != nil {
|
||||
return nil, errors.NewBadRequest("failed to parse parameter: port")
|
||||
return nil, tracker.ClientError("failed to parse parameter: port")
|
||||
}
|
||||
request.Port = uint16(port)
|
||||
|
||||
v4, v6, err := requestedIP(q, r, cfg)
|
||||
if err != nil {
|
||||
return nil, errors.NewBadRequest("failed to parse remote IP")
|
||||
return nil, tracker.ClientError("failed to parse remote IP")
|
||||
}
|
||||
request.IPv4 = v4
|
||||
request.IPv6 = v6
|
||||
|
@ -91,7 +91,7 @@ func scrapeRequest(r *http.Request, cfg *httpConfig) (*chihaya.ScrapeRequest, er
|
|||
|
||||
infoHashes := q.InfoHashes()
|
||||
if len(infoHashes) < 1 {
|
||||
return nil, errors.NewBadRequest("no info_hash parameter supplied")
|
||||
return nil, tracker.ClientError("no info_hash parameter supplied")
|
||||
}
|
||||
|
||||
request := &chihaya.ScrapeRequest{
|
||||
|
@ -146,7 +146,7 @@ func requestedIP(q *query.Query, r *http.Request, cfg *httpConfig) (v4, v6 net.I
|
|||
}
|
||||
|
||||
if v4 == nil && v6 == nil {
|
||||
err = errors.NewBadRequest("failed to parse IP address")
|
||||
err = tracker.ClientError("failed to parse IP address")
|
||||
}
|
||||
|
||||
return
|
||||
|
|
|
@ -8,22 +8,17 @@ import (
|
|||
"net/http"
|
||||
|
||||
"github.com/chihaya/chihaya"
|
||||
"github.com/chihaya/chihaya/errors"
|
||||
"github.com/chihaya/chihaya/pkg/bencode"
|
||||
"github.com/chihaya/chihaya/tracker"
|
||||
)
|
||||
|
||||
func writeError(w http.ResponseWriter, err error) error {
|
||||
message := "internal server error"
|
||||
chihayaErr, ok := err.(*errors.Error)
|
||||
|
||||
if ok {
|
||||
w.WriteHeader(chihayaErr.Status())
|
||||
|
||||
if chihayaErr.Public() {
|
||||
if _, clientErr := err.(tracker.ClientError); clientErr {
|
||||
message = err.Error()
|
||||
}
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
return bencode.NewEncoder(w).Encode(bencode.Dict{
|
||||
"failure reason": message,
|
||||
})
|
||||
|
|
|
@ -8,7 +8,7 @@ import (
|
|||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/chihaya/chihaya/errors"
|
||||
"github.com/chihaya/chihaya/tracker"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
|
@ -22,17 +22,15 @@ func TestWriteError(t *testing.T) {
|
|||
|
||||
for _, tt := range table {
|
||||
r := httptest.NewRecorder()
|
||||
err := writeError(r, errors.NewMessage(tt.reason))
|
||||
err := writeError(r, tracker.ClientError(tt.reason))
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, r.Body.String(), tt.expected)
|
||||
assert.Equal(t, r.Code, 200)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteStatus(t *testing.T) {
|
||||
r := httptest.NewRecorder()
|
||||
err := writeError(r, errors.NewBadRequest("something is missing"))
|
||||
err := writeError(r, tracker.ClientError("something is missing"))
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, r.Body.String(), "d14:failure reason20:something is missinge")
|
||||
assert.Equal(t, r.Code, 400)
|
||||
}
|
||||
|
|
|
@ -9,7 +9,6 @@ import (
|
|||
|
||||
"github.com/chihaya/chihaya"
|
||||
"github.com/chihaya/chihaya/config"
|
||||
"github.com/chihaya/chihaya/errors"
|
||||
"github.com/chihaya/chihaya/server/store"
|
||||
"github.com/chihaya/chihaya/tracker"
|
||||
)
|
||||
|
@ -20,7 +19,7 @@ func init() {
|
|||
|
||||
// ErrBlockedIP is returned by an announce middleware if any of the announcing
|
||||
// IPs is disallowed.
|
||||
var ErrBlockedIP = errors.NewMessage("disallowed IP address")
|
||||
var ErrBlockedIP = tracker.ClientError("disallowed IP address")
|
||||
|
||||
// blacklistAnnounceIP provides a middleware that only allows IPs to announce
|
||||
// that are not stored in an IPStore.
|
||||
|
|
|
@ -11,6 +11,10 @@ import (
|
|||
"github.com/chihaya/chihaya/config"
|
||||
)
|
||||
|
||||
type ClientError string
|
||||
|
||||
func (c ClientError) Error() string { return string(c) }
|
||||
|
||||
// Tracker represents a protocol independent, middleware-composed BitTorrent
|
||||
// tracker.
|
||||
type Tracker struct {
|
||||
|
|
Loading…
Add table
Reference in a new issue