From 56309794bdf0e774a2a410c5ecdab16b735178f4 Mon Sep 17 00:00:00 2001 From: raylu Date: Wed, 2 Feb 2011 00:04:45 -0500 Subject: [PATCH] Fix error reporting in cmd-google.go:calc --- cmd-google.go | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/cmd-google.go b/cmd-google.go index 60f1e2e..a6591df 100644 --- a/cmd-google.go +++ b/cmd-google.go @@ -130,19 +130,41 @@ func calc(conn *irc.Conn, nick *irc.Nick, args, target string) { re := regexp.MustCompile(`{lhs: "(.*)",rhs: "(.*)",error: "(.*)",icc: (true|false)}`) result := re.FindSubmatch(b) - if len(result) != 5 || len(result[3]) > 1 { - say(conn, target, "%s: Error while calculating.", nick.Nick); return + if len(result) != 5 { + say(conn, target, "%s: Error while parsing.", nick.Nick) + return + } + if len(result[3]) > 1 { + output := fmt.Sprintf(`"%s"`, result[3]) + error := parseCalc(output) + if error != "" { + say(conn, target, "%s: Error: %s", nick.Nick, error) + } else { + say(conn, target, "%s: Error while calculating and error while decoding error.", nick.Nick) + } + return + } + if len(result[1]) == 0 || len(result[2]) == 0 { + say(conn, target, "%s: Error while calculating.", nick.Nick) + return } output := fmt.Sprintf(`"%s = %s"`, result[1], result[2]) - output, err = strconv.Unquote(output) - if err != nil { + output = parseCalc(output) + if output == "" { say(conn, target, "%s: Error while decoding.", nick.Nick); return } - output = html.UnescapeString(output) // see go issue 1233 say(conn, target, output) } +func parseCalc(output string) string { + unquote, err := strconv.Unquote(output) + if err != nil { + return "" + } + return html.UnescapeString(unquote) +} + // please disregard the reproduction of src/pkg/http/client.go:send below // it's definitely not to send a User-Agent for undocumented Google APIs func send(url string) ([]byte, os.Error) {