提交 49792b23 作者: Juan Batiz-Benet

refactored cast errors to use a util

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