Skip to content

Commit

Permalink
return distinct error type on input error
Browse files Browse the repository at this point in the history
  • Loading branch information
cenkalti committed Sep 27, 2019
1 parent 598879b commit c1127ce
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 22 deletions.
40 changes: 40 additions & 0 deletions torrent/error.go
@@ -0,0 +1,40 @@
package torrent

import (
"github.com/cenkalti/rain/internal/announcer"
)

// InputError is returned from Session.AddTorrent and Session.AddURI methods when there is problem with the input.
type InputError struct {
err error
}

func newInputError(err error) *InputError {
return &InputError{
err: err,
}
}

func (e *InputError) Error() string {
return "input error: " + e.err.Error()
}

func (e *InputError) Unwrap() error {
return e.err
}

type AnnounceError struct {
err *announcer.AnnounceError
}

func (e *AnnounceError) Error() string {
return e.err.Message
}

func (e *AnnounceError) Unwrap() error {
return e.err.Err
}

func (e *AnnounceError) Unknown() bool {
return e.err.Unknown
}
10 changes: 5 additions & 5 deletions torrent/session_add.go
Expand Up @@ -52,7 +52,7 @@ func (s *Session) addTorrentStopped(r io.Reader, opt *AddTorrentOptions) (*Torre
r = io.LimitReader(r, int64(s.config.MaxTorrentSize))
mi, err := s.parseMetaInfo(r)
if err != nil {
return nil, err
return nil, newInputError(err)
}
id, port, sto, err := s.add(opt)
if err != nil {
Expand Down Expand Up @@ -109,7 +109,7 @@ func (s *Session) AddURI(uri string, opt *AddTorrentOptions) (*Torrent, error) {

u, err := url.Parse(uri)
if err != nil {
return nil, err
return nil, newInputError(err)
}
switch u.Scheme {
case "http", "https":
Expand Down Expand Up @@ -140,12 +140,12 @@ func (s *Session) addURL(u string, opt *AddTorrentOptions) (*Torrent, error) {
}
resp, err := client.Get(u)
if err != nil {
return nil, err
return nil, newInputError(err)
}
defer resp.Body.Close()

if resp.ContentLength > int64(s.config.MaxTorrentSize) {
return nil, fmt.Errorf("torrent too large: %d", resp.ContentLength)
return nil, newInputError(fmt.Errorf("torrent too large: %d", resp.ContentLength))
}
r := io.LimitReader(resp.Body, int64(s.config.MaxTorrentSize))
return s.AddTorrent(r, opt)
Expand All @@ -154,7 +154,7 @@ func (s *Session) addURL(u string, opt *AddTorrentOptions) (*Torrent, error) {
func (s *Session) addMagnet(link string, opt *AddTorrentOptions) (*Torrent, error) {
ma, err := magnet.New(link)
if err != nil {
return nil, err
return nil, newInputError(err)
}
id, port, sto, err := s.add(opt)
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions torrent/session_rpc_handler.go
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"io"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -45,6 +46,10 @@ func (h *rpcHandler) AddTorrent(args *rpctypes.AddTorrentRequest, reply *rpctype
ID: args.AddTorrentOptions.ID,
}
t, err := h.session.AddTorrent(r, opt)
var e *InputError
if errors.As(err, &e) {
return jsonrpc2.NewError(2, e.Error())
}
if err != nil {
return err
}
Expand All @@ -58,6 +63,10 @@ func (h *rpcHandler) AddURI(args *rpctypes.AddURIRequest, reply *rpctypes.AddURI
ID: args.AddTorrentOptions.ID,
}
t, err := h.session.AddURI(args.URI, opt)
var e *InputError
if errors.As(err, &e) {
return jsonrpc2.NewError(2, e.Error())
}
if err != nil {
return err
}
Expand Down
17 changes: 0 additions & 17 deletions torrent/torrent_commands.go
Expand Up @@ -5,7 +5,6 @@ import (
"net"
"time"

"github.com/cenkalti/rain/internal/announcer"
"github.com/cenkalti/rain/internal/magnet"
"github.com/cenkalti/rain/internal/metainfo"
"github.com/cenkalti/rain/internal/tracker"
Expand Down Expand Up @@ -194,22 +193,6 @@ type Tracker struct {
NextAnnounce time.Time
}

type AnnounceError struct {
err *announcer.AnnounceError
}

func (e *AnnounceError) Error() string {
return e.err.Message
}

func (e *AnnounceError) Unwrap() error {
return e.err.Err
}

func (e *AnnounceError) Unknown() bool {
return e.err.Unknown
}

type trackersRequest struct {
Response chan []Tracker
}
Expand Down

0 comments on commit c1127ce

Please sign in to comment.