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) {
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 := ""
for ; len(b) > 0 ; {
......
......@@ -4,7 +4,10 @@ type Multiaddr struct {
Bytes []byte
}
func NewString(s string) *Multiaddr {
m := &Multiaddr{}
return m
func NewString(s string) (*Multiaddr, error) {
b, err := StringToBytes(s)
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