From 547241c13c26df3b06bd53545827af74a938751f Mon Sep 17 00:00:00 2001 From: joe-conigliaro Date: Sun, 18 Aug 2019 00:02:01 +1000 Subject: [PATCH] http: make do() return errors instead of panic --- vlib/http/http.v | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/vlib/http/http.v b/vlib/http/http.v index afa910dd2f..5e1f29f0b1 100644 --- a/vlib/http/http.v +++ b/vlib/http/http.v @@ -96,7 +96,7 @@ pub fn (req mut Request) add_header(key, val string) { // req.h = h } -pub fn (req &Request) do() Response { +pub fn (req &Request) do() ?Response { if req.typ == 'POST' { // req.headers << 'Content-Type: application/x-www-form-urlencoded' } @@ -104,12 +104,12 @@ pub fn (req &Request) do() Response { //h := '$key: $val' } url := urllib.parse(req.url) or { - panic('http.request.do: invalid URL $req.url') + return error('http.request.do: invalid URL $req.url') // return Response{} //error('ff')} } is_ssl := url.scheme == 'https' if !is_ssl { - panic('non https requests are not supported right now') + return error('non https requests are not supported right now') } // first request @@ -120,11 +120,11 @@ pub fn (req &Request) do() Response { mut no_redirects := 0 for resp.status_code in [301, 302, 303, 307, 308] { if no_redirects == max_redirects { - panic('http.request.do: maximum number of redirects reached ($max_redirects)') + return error('http.request.do: maximum number of redirects reached ($max_redirects)') } h_loc := resp.headers['Location'] r_url := urllib.parse(h_loc) or { - panic('http.request.do: cannot follow redirect, location header has invalid url $h_loc') + return error('http.request.do: cannot follow redirect, location header has invalid url $h_loc') } p = r_url.path.trim_left('/') u = if r_url.query().size > 0 { '/$p?${r_url.query().encode()}' } else { '/$p' }