From 32473eeafe37826693032984caa6c9f5879cdd36 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Tue, 12 Nov 2019 18:23:53 +0200 Subject: [PATCH] net and http: more consistent error messages --- vlib/http/http.v | 8 ++++---- vlib/http/http_client.v | 2 +- vlib/net/net.v | 2 +- vlib/net/socket.v | 22 +++++++++++----------- vlib/net/urllib/urllib.v | 28 ++++++++++++++-------------- 5 files changed, 31 insertions(+), 31 deletions(-) diff --git a/vlib/http/http.v b/vlib/http/http.v index 3ff72f9e56..92f675099e 100644 --- a/vlib/http/http.v +++ b/vlib/http/http.v @@ -56,7 +56,7 @@ pub fn post(url, data string) ?Response { pub fn new_request(typ, _url, _data string) ?Request { if _url == '' { - return error('bad url') + return error('http.new_request: empty url') } mut url := _url mut data := _data @@ -125,7 +125,7 @@ pub fn (req &Request) do() ?Response { for key, val in req.headers { //h := '$key: $val' } - url := urllib.parse(req.url) or { return error('http.request.do: invalid URL $req.url') } + url := urllib.parse(req.url) or { return error('http.request.do: invalid URL "$req.url"') } mut rurl := url mut resp := Response{} mut no_redirects := 0 @@ -136,7 +136,7 @@ pub fn (req &Request) do() ?Response { if ! (resp.status_code in [301, 302, 303, 307, 308]) { break } // follow any redirects redirect_url := resp.headers['Location'] - qrurl := urllib.parse( redirect_url ) or { return error('http.request.do: invalid URL in redirect $redirect_url') } + qrurl := urllib.parse( redirect_url ) or { return error('http.request.do: invalid URL in redirect "$redirect_url"') } rurl = qrurl no_redirects++ } @@ -167,7 +167,7 @@ fn (req &Request) method_and_url_to_response(method string, url net_dot_urllib.U } return res } - return error('http.request.do: unsupported scheme: $scheme') + return error('http.request.method_and_url_to_response: unsupported scheme: "$scheme"') } fn parse_response(resp string) Response { diff --git a/vlib/http/http_client.v b/vlib/http/http_client.v index 0fd701c77a..8cfed88451 100644 --- a/vlib/http/http_client.v +++ b/vlib/http/http_client.v @@ -13,7 +13,7 @@ fn (req &Request) http_do(port int, method, host_name, path string) ?Response { client.send( s.str, s.len ) or {} for { readbytes := client.crecv( rbuffer, bufsize ) - if readbytes < 0 { return error('http_do error reading response. readbytes: $readbytes') } + if readbytes < 0 { return error('http.request.http_do: error reading response. readbytes=$readbytes') } if readbytes == 0 { break } sb.write( tos(rbuffer, readbytes) ) } diff --git a/vlib/net/net.v b/vlib/net/net.v index c04f402fc4..b6dfca6599 100644 --- a/vlib/net/net.v +++ b/vlib/net/net.v @@ -7,7 +7,7 @@ pub fn hostname() ?string { // The host name is returned as a null-terminated string. res := C.gethostname(&name, 256) if res != 0 { - return error('net.hostname() cannot get the host name') + return error('net.hostname: failed with $res') } return tos_clone(name) } diff --git a/vlib/net/socket.v b/vlib/net/socket.v index 016433c184..a9e147fc9d 100644 --- a/vlib/net/socket.v +++ b/vlib/net/socket.v @@ -46,7 +46,7 @@ pub fn socket(family int, _type int, proto int) ?Socket { // same port after the application exits. C.setsockopt(sockfd, C.SOL_SOCKET, C.SO_REUSEADDR, &one, sizeof(int)) if sockfd == 0 { - return error('socket: init failed') + return error('net.socket: failed') } s := Socket { sockfd: sockfd @@ -65,7 +65,7 @@ pub fn socket_udp() ?Socket { pub fn (s Socket) setsockopt(level int, optname int, optvalue &int) ?int { res := C.setsockopt(s.sockfd, level, optname, optvalue, C.sizeof(optvalue)) if res < 0 { - return error('socket: setsockopt failed') + return error('net.setsocketopt: failed with $res') } return int(res) } @@ -79,7 +79,7 @@ pub fn (s Socket) bind(port int) ?int { size := 16 // sizeof(C.sockaddr_in) res := int(C.bind(s.sockfd, &addr, size)) if res < 0 { - return error('socket: bind failed') + return error('net.bind: failed with $res') } return res } @@ -89,7 +89,7 @@ pub fn (s Socket) listen() ?int { backlog := 128 res := int(C.listen(s.sockfd, backlog)) if res < 0 { - return error('socket: listen failed') + return error('net.listen: failed with $res') } $if debug { println('listen res = $res') @@ -105,7 +105,7 @@ pub fn (s Socket) listen_backlog(backlog int) ?int { } res := C.listen(s.sockfd, n) if res < 0 { - return error('socket: listen_backlog failed') + return error('net.listen_backlog: failed with $res') } return int(res) } @@ -136,7 +136,7 @@ pub fn (s Socket) accept() ?Socket { size := 128 // sizeof(sockaddr_storage) sockfd := C.accept(s.sockfd, &addr, &size) if sockfd < 0 { - return error('socket: accept failed') + return error('net.accept: failed with $sockfd') } c := Socket { sockfd: sockfd @@ -165,12 +165,12 @@ pub fn (s Socket) connect(address string, port int) ?int { info_res := C.getaddrinfo(address.str, sport.str, &hints, &info) if info_res != 0 { error_message := os.get_error_msg(net.error_code()) - return error('socket: getaddrinfo failed ($error_message)') + return error('net.connect: getaddrinfo failed "$error_message"') } res := int(C.connect(s.sockfd, info.ai_addr, info.ai_addrlen)) if res < 0 { error_message := os.get_error_msg(net.error_code()) - return error('socket: connect failed ($error_message)') + return error('net.connect: connect failed "$error_message"') } return int(res) } @@ -190,7 +190,7 @@ pub fn dial(address string, port int) ?Socket { pub fn (s Socket) send(buf byteptr, len int) ?int { res := int( C.send(s.sockfd, buf, len, 0) ) if res < 0 { - return error('socket: send failed') + return error('net.send: failed with $res') } return res } @@ -224,7 +224,7 @@ pub fn (s Socket) close() ?int { // TODO: should shutdown throw an error? close will // continue even if shutdown failed // if shutdown_res < 0 { -// return error('socket: shutdown failed') +// return error('net.close: shutdown failed with $shutdown_res') // } mut res := 0 @@ -235,7 +235,7 @@ pub fn (s Socket) close() ?int { res = C.close(s.sockfd) } if res < 0 { - return error('socket: close failed') + return error('net.close: failed with $res') } return 0 diff --git a/vlib/net/urllib/urllib.v b/vlib/net/urllib/urllib.v index 9412fd038f..8f42c4d047 100644 --- a/vlib/net/urllib/urllib.v +++ b/vlib/net/urllib/urllib.v @@ -24,12 +24,12 @@ enum EncodingMode { } const ( - err_msg_escape = 'invalid URL escape' - err_msg_parse = 'error parsing url' + err_msg_escape = 'unescape: invalid URL escape' + err_msg_parse = 'parse: failed parsing url' ) fn error_msg(message, val string) string { - mut msg := 'net.urllib: $message' + mut msg := 'net.urllib.$message' if val != '' { msg = '$msg ($val)' } return msg } @@ -196,7 +196,7 @@ fn unescape(s_ string, mode EncodingMode) ?string { i++ } else { if (mode == .encode_host || mode == .encode_zone) && s[i] < 0x80 && should_escape(s[i], mode) { - error(error_msg('invalid character in host name', s[i..i+1])) + error(error_msg('unescape: invalid character in host name', s[i..i+1])) } i++ } @@ -397,7 +397,7 @@ fn split_by_scheme(rawurl string) ?[]string { } else if c == `:` { if i == 0 { - return error(error_msg('missing protocol scheme', '')) + return error(error_msg('split_by_scheme: missing protocol scheme', '')) } return [rawurl[..i], rawurl[i+1..]] } @@ -468,11 +468,11 @@ fn parse_request_uri(rawurl string) ?URL { // If via_request is false, all forms of relative URLs are allowed. fn parse_url(rawurl string, via_request bool) ?URL { if string_contains_ctl_byte(rawurl) { - return error(error_msg('invalid control character in URL', rawurl)) + return error(error_msg('parse_url: invalid control character in URL', rawurl)) } if rawurl == '' && via_request { - return error(error_msg('empty URL', '')) + return error(error_msg('parse_url: empty URL', rawurl)) } mut url := URL{} @@ -507,7 +507,7 @@ fn parse_url(rawurl string, via_request bool) ?URL { return url } if via_request { - return error(error_msg('invalid URI for request', '')) + return error(error_msg('parse_url: invalid URI for request', '')) } // Avoid confusion with malformed schemes, like cache_object:foo/bar. @@ -520,7 +520,7 @@ fn parse_url(rawurl string, via_request bool) ?URL { slash := rest.index('/') if colon >= 0 && (slash < 0 || colon < slash) { // First path segment has colon. Not allowed in relative URL. - return error(error_msg('first path segment in URL cannot contain colon', '')) + return error(error_msg('parse_url: first path segment in URL cannot contain colon', '')) } } @@ -568,7 +568,7 @@ fn parse_authority(authority string) ?ParseAuthorityRes { } mut userinfo := authority[..i] if !valid_userinfo(userinfo) { - return error(error_msg('invalid userinfo', '')) + return error(error_msg('parse_authority: invalid userinfo', '')) } if !userinfo.contains(':') { u := unescape(userinfo, .encode_user_password) or { @@ -602,11 +602,11 @@ fn parse_host(host string) ?string { // E.g., '[fe80::1]', '[fe80::1%25en0]', '[fe80::1]:80'. mut i := host.last_index(']') if i < 0 { - return error(error_msg('missing \']\' in host', '')) + return error(error_msg('parse_host: missing \']\' in host', '')) } mut colon_port := host[i+1..] if !valid_optional_port(colon_port) { - return error(error_msg('invalid port $colon_port after host ', '')) + return error(error_msg('parse_host: invalid port $colon_port after host ', '')) } // RFC 6874 defines that %25 (%-encoded percent) introduces @@ -632,7 +632,7 @@ fn parse_host(host string) ?string { if i != -1 { colon_port = host[i..] if !valid_optional_port(colon_port) { - return error(error_msg('invalid port $colon_port after host ', '')) + return error(error_msg('parse_host: invalid port $colon_port after host ', '')) } } } @@ -872,7 +872,7 @@ fn parse_query_values(m mut Values, query string) ?bool { m.add(key, value) } if had_error { - return error(error_msg('error parsing query string', '')) + return error(error_msg('parse_query_values: failed parsing query string', '')) } return true }