提交 49792b23 作者: Juan Batiz-Benet

refactored cast errors to use a util

上级 f6c1cefe
......@@ -3,7 +3,6 @@ package http
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
......@@ -11,10 +10,9 @@ import (
"strings"
cmds "github.com/jbenet/go-ipfs/commands"
u "github.com/jbenet/go-ipfs/util"
)
var castError = errors.New("cast error")
const (
ApiUrlFormat = "http://%s%s/%s?%s"
ApiPath = "/api/v0" // TODO: make configurable
......@@ -70,7 +68,7 @@ func getQuery(req cmds.Request) (string, io.Reader, error) {
for k, v := range req.Options() {
str, ok := v.(string)
if !ok {
return "", nil, castError
return "", nil, u.ErrCast()
}
query.Set(k, str)
}
......@@ -87,7 +85,7 @@ func getQuery(req cmds.Request) (string, io.Reader, error) {
if argDef.Type == cmds.ArgString {
str, ok := arg.(string)
if !ok {
return "", nil, castError
return "", nil, u.ErrCast()
}
query.Add("arg", str)
......@@ -99,7 +97,7 @@ func getQuery(req cmds.Request) (string, io.Reader, error) {
var ok bool
inputStream, ok = arg.(io.Reader)
if !ok {
return "", nil, castError
return "", nil, u.ErrCast()
}
}
}
......
package commands
import (
"errors"
"fmt"
"io"
"reflect"
......@@ -9,6 +8,7 @@ import (
"github.com/jbenet/go-ipfs/config"
"github.com/jbenet/go-ipfs/core"
u "github.com/jbenet/go-ipfs/util"
)
type optMap map[string]interface{}
......@@ -176,7 +176,7 @@ func (r *request) ConvertOptions() error {
convert := converters[opt.Type]
str, ok := v.(string)
if !ok {
return errors.New("cast error")
return u.ErrCast()
}
val, err := convert(str)
if err != nil {
......
......@@ -17,6 +17,7 @@ import (
dag "github.com/jbenet/go-ipfs/merkledag"
pinning "github.com/jbenet/go-ipfs/pin"
ft "github.com/jbenet/go-ipfs/unixfs"
u "github.com/jbenet/go-ipfs/util"
)
// Error indicating the max depth has been exceded.
......@@ -41,7 +42,7 @@ MerkleDAG. A smarter partial add with a staging area (like git)
remains to be implemented.
`,
Run: func(req cmds.Request) (interface{}, error) {
var added AddOutput
added := &AddOutput{}
n := req.Context().Node
recursive, err := req.Option("r").Bool()
......@@ -167,7 +168,7 @@ remains to be implemented.
cmds.Text: func(res cmds.Response) ([]byte, error) {
val, ok := res.Output().(*AddOutput)
if !ok {
return nil, errors.New("cast error")
return nil, u.ErrCast()
}
var buf bytes.Buffer
......
......@@ -2,7 +2,6 @@ package commands
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
......@@ -45,7 +44,7 @@ It outputs to stdout, and <key> is a base58 encoded multihash.`,
key, ok := req.Arguments()[0].(string)
if !ok {
return nil, errors.New("cast error")
return nil, u.ErrCast()
}
if !u.IsValidHash(key) {
......@@ -81,7 +80,7 @@ It reads from stdin, and <key> is a base58 encoded multihash.`,
in, ok := req.Arguments()[0].(io.Reader)
if !ok {
return nil, errors.New("cast error")
return nil, u.ErrCast()
}
data, err := ioutil.ReadAll(in)
......
package commands
import (
"errors"
"fmt"
"strings"
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
u "github.com/jbenet/go-ipfs/util"
cmds "github.com/jbenet/go-ipfs/commands"
config "github.com/jbenet/go-ipfs/config"
)
......@@ -152,7 +152,7 @@ func bootstrapInputToPeers(input []interface{}) ([]*config.BootstrapPeer, error)
for _, v := range input {
addr, ok := v.(string)
if !ok {
return nil, errors.New("cast error")
return nil, u.ErrCast()
}
addrS, peeridS := split(addr)
......
......@@ -12,6 +12,7 @@ import (
cmds "github.com/jbenet/go-ipfs/commands"
config "github.com/jbenet/go-ipfs/config"
u "github.com/jbenet/go-ipfs/util"
)
type ConfigField struct {
......@@ -45,7 +46,7 @@ var configCmd = &cmds.Command{
key, ok := args[0].(string)
if !ok {
return nil, errors.New("cast error")
return nil, u.ErrCast()
}
filename, err := config.Filename(req.Context().ConfigRoot)
......@@ -58,7 +59,7 @@ var configCmd = &cmds.Command{
var ok bool
value, ok = args[1].(string)
if !ok {
return nil, errors.New("cast error")
return nil, u.ErrCast()
}
return setConfig(filename, key, value)
......
package internal
import (
"errors"
"io"
)
var CastErr = errors.New("cast error")
u "github.com/jbenet/go-ipfs/util"
)
func CastToReaders(slice []interface{}) ([]io.Reader, error) {
readers := make([]io.Reader, 0)
for _, arg := range slice {
reader, ok := arg.(io.Reader)
if !ok {
return nil, CastErr
return nil, u.ErrCast()
}
readers = append(readers, reader)
}
......@@ -24,7 +23,7 @@ func CastToStrings(slice []interface{}) ([]string, error) {
for _, maybe := range slice {
str, ok := maybe.(string)
if !ok {
return nil, CastErr
return nil, u.ErrCast()
}
strs = append(strs, str)
}
......
......@@ -8,8 +8,9 @@ import (
"io/ioutil"
cmds "github.com/jbenet/go-ipfs/commands"
"github.com/jbenet/go-ipfs/core"
core "github.com/jbenet/go-ipfs/core"
dag "github.com/jbenet/go-ipfs/merkledag"
u "github.com/jbenet/go-ipfs/util"
)
// ErrObjectTooLarge is returned when too much data was read from stdin. current limit 512k
......@@ -51,7 +52,7 @@ output is the raw data of the object.
key, ok := req.Arguments()[0].(string)
if !ok {
return nil, errors.New("cast error")
return nil, u.ErrCast()
}
return objectData(n, key)
......@@ -71,7 +72,7 @@ It outputs to stdout, and <key> is a base58 encoded multihash.`,
key, ok := req.Arguments()[0].(string)
if !ok {
return nil, errors.New("cast error")
return nil, u.ErrCast()
}
return objectLinks(n, key)
......@@ -99,7 +100,7 @@ This command outputs data in the following encodings:
key, ok := req.Arguments()[0].(string)
if !ok {
return nil, errors.New("cast error")
return nil, u.ErrCast()
}
object, err := objectGet(n, key)
......@@ -151,12 +152,12 @@ Data should be in the format specified by <encoding>.
input, ok := req.Arguments()[0].(io.Reader)
if !ok {
return nil, errors.New("cast error")
return nil, u.ErrCast()
}
encoding, ok := req.Arguments()[1].(string)
if !ok {
return nil, errors.New("cast error")
return nil, u.ErrCast()
}
output, err := objectPut(n, input, encoding)
......
......@@ -4,6 +4,7 @@ import (
"errors"
cmds "github.com/jbenet/go-ipfs/commands"
u "github.com/jbenet/go-ipfs/util"
)
var resolveCmd = &cmds.Command{
......@@ -49,7 +50,7 @@ Resolve te value of another name:
var ok bool
name, ok = req.Arguments()[0].(string)
if !ok {
return nil, errors.New("cast error")
return nil, u.ErrCast()
}
}
......
package testutil
import (
"testing"
crand "crypto/rand"
"testing"
"github.com/jbenet/go-ipfs/peer"
......
......@@ -8,6 +8,7 @@ import (
"math/rand"
"os"
"path/filepath"
"runtime/debug"
"strings"
"time"
......@@ -40,6 +41,14 @@ func TildeExpansion(filename string) (string, error) {
return homedir.Expand(filename)
}
// ErrCast is returned when a cast fails AND the program should not panic.
func ErrCast() error {
debug.PrintStack()
return errCast
}
var errCast = errors.New("cast error")
// ExpandPathnames takes a set of paths and turns them into absolute paths
func ExpandPathnames(paths []string) ([]string, error) {
var out []string
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论