http: make do() return errors instead of panic

pull/1639/head
joe-conigliaro 2019-08-18 00:02:01 +10:00 committed by Alexander Medvednikov
parent 1274cdf0af
commit 547241c13c
1 changed files with 5 additions and 5 deletions

View File

@ -96,7 +96,7 @@ pub fn (req mut Request) add_header(key, val string) {
// req.h = h // req.h = h
} }
pub fn (req &Request) do() Response { pub fn (req &Request) do() ?Response {
if req.typ == 'POST' { if req.typ == 'POST' {
// req.headers << 'Content-Type: application/x-www-form-urlencoded' // req.headers << 'Content-Type: application/x-www-form-urlencoded'
} }
@ -104,12 +104,12 @@ pub fn (req &Request) do() Response {
//h := '$key: $val' //h := '$key: $val'
} }
url := urllib.parse(req.url) or { 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')} // return Response{} //error('ff')}
} }
is_ssl := url.scheme == 'https' is_ssl := url.scheme == 'https'
if !is_ssl { if !is_ssl {
panic('non https requests are not supported right now') return error('non https requests are not supported right now')
} }
// first request // first request
@ -120,11 +120,11 @@ pub fn (req &Request) do() Response {
mut no_redirects := 0 mut no_redirects := 0
for resp.status_code in [301, 302, 303, 307, 308] { for resp.status_code in [301, 302, 303, 307, 308] {
if no_redirects == max_redirects { 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'] h_loc := resp.headers['Location']
r_url := urllib.parse(h_loc) or { 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('/') p = r_url.path.trim_left('/')
u = if r_url.query().size > 0 { '/$p?${r_url.query().encode()}' } else { '/$p' } u = if r_url.query().size > 0 { '/$p?${r_url.query().encode()}' } else { '/$p' }