From 1841de74e7a42090f7fba844f1c9a7987de412d1 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Thu, 7 Aug 2014 18:06:09 +0000 Subject: [PATCH] errors now []byte --- rsload.go | 2 +- values.go | 25 +++++++++++++++++-------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/rsload.go b/rsload.go index f2b8c1f..7b19d2c 100644 --- a/rsload.go +++ b/rsload.go @@ -110,7 +110,7 @@ func main() { response := <-responses if response.ok() { switch r := response.val().(type) { - case Error: + case ErrorVal: if options.verbose { fmt.Fprintf(os.Stderr, "%q -> %q\n", request, response.val()) } else { diff --git a/values.go b/values.go index fd964e3..238dd7f 100644 --- a/values.go +++ b/values.go @@ -106,8 +106,7 @@ func readValue(r io.Reader) (value, error) { case start_string: return StringVal(line), nil case start_error: - line = line[:len(line)-2] - return Error(line[1:]), nil + return ErrorVal(line), nil case start_integer: line = line[:len(line)-2] return Integer(line[1:]), nil @@ -141,13 +140,23 @@ func String(s string) value { // ------------------------------------------------------------------------------ -type Error string +type ErrorVal []byte -func (e Error) Write(w io.Writer) (int, error) { - w.Write([]byte{'-'}) - w.Write([]byte(e)) - w.Write([]byte{'\r', '\n'}) - return 0, nil +func (e ErrorVal) Write(w io.Writer) (int, error) { + // w.Write([]byte{'-'}) + // w.Write([]byte(e)) + // w.Write([]byte{'\r', '\n'}) + // return 0, nil + return w.Write(e) +} + +func Error(s string) value { + b := make(ErrorVal, len(s)+3) + b[0] = '-' + copy(b[1:], []byte(s)) + b[len(b)-2] = '\r' + b[len(b)-1] = '\n' + return b } // ------------------------------------------------------------------------------