Commit 3f5984c9 authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

panic handler + construct

parent 47a6842e
...@@ -31,7 +31,15 @@ func StringToBytes(s string) ([]byte, error) { ...@@ -31,7 +31,15 @@ func StringToBytes(s string) ([]byte, error) {
return b, nil return b, nil
} }
func BytesToString(b []byte) (string, error) { func BytesToString(b []byte) (ret string, err error) {
// panic handler, in case we try accessing bytes incorrectly.
defer func() {
if e := recover(); e != nil {
ret = ""
err = e.(error)
}
}()
s := "" s := ""
for ; len(b) > 0 ; { for ; len(b) > 0 ; {
......
...@@ -4,7 +4,10 @@ type Multiaddr struct { ...@@ -4,7 +4,10 @@ type Multiaddr struct {
Bytes []byte Bytes []byte
} }
func NewString(s string) *Multiaddr { func NewString(s string) (*Multiaddr, error) {
m := &Multiaddr{} b, err := StringToBytes(s)
return m if err != nil {
return nil, err
}
return &Multiaddr{Bytes: b}, nil
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment