提交 d87dc4df 编写于 作者: M Mislav Marohnić

[api] More consistent error output on HTTP failures

- HTTP response is now printed on stdout regardless of HTTP status
- No longer print an extra newline after HTTP response body
- No more `Error: HTTP {STATUS}` message on stderr
- hub exits with status 22 instead of 1
上级 c87b8d8e
......@@ -178,25 +178,24 @@ func apiCommand(cmd *Command, args *Args) {
args.NoForward()
if response.StatusCode >= 300 {
ui.Errorf("Error: HTTP %s\n", strings.TrimSpace(response.Status))
out := ui.Stdout
colorize := ui.IsTerminal(os.Stdout)
success := response.StatusCode < 300
parseJSON := args.Flag.Bool("--flat")
if !success {
jsonType, _ := regexp.MatchString(`[/+]json(?:;|$)`, response.Header.Get("Content-Type"))
colorize := ui.IsTerminal(os.Stderr)
if args.Flag.Bool("--flat") && jsonType {
utils.JSONPath(ui.Stderr, response.Body, colorize)
} else {
io.Copy(ui.Stderr, response.Body)
ui.Errorln()
}
os.Exit(1)
parseJSON = parseJSON && jsonType
}
colorize := ui.IsTerminal(os.Stdout)
if args.Flag.Bool("--flat") {
utils.JSONPath(ui.Stdout, response.Body, colorize)
if parseJSON {
utils.JSONPath(out, response.Body, colorize)
} else {
io.Copy(ui.Stdout, response.Body)
ui.Println()
io.Copy(out, response.Body)
}
if !success {
os.Exit(22)
}
}
......
......@@ -14,7 +14,7 @@ Feature: hub api
When I successfully run `hub api hello/world`
Then the output should contain exactly:
"""
{"name":"Ed"}\n
{"name":"Ed"}
"""
Scenario: GET Enterprise resource
......@@ -30,7 +30,7 @@ Feature: hub api
When I successfully run `hub api hello/world`
Then the output should contain exactly:
"""
{"name":"Ed"}\n
{"name":"Ed"}
"""
Scenario: Non-success response
......@@ -42,13 +42,12 @@ Feature: hub api
}
"""
When I run `hub api hello/world`
Then the exit status should be 1
And the stdout should contain exactly ""
And the stderr should contain exactly:
Then the exit status should be 22
And the stdout should contain exactly:
"""
Error: HTTP 400 Bad Request
{"name":"Ed"}\n
{"name":"Ed"}
"""
And the stderr should contain exactly ""
Scenario: Non-success response flat output
Given the GitHub API server:
......@@ -59,13 +58,12 @@ Feature: hub api
}
"""
When I run `hub api -t hello/world`
Then the exit status should be 1
And the stdout should contain exactly ""
And the stderr should contain exactly:
Then the exit status should be 22
And the stdout should contain exactly:
"""
Error: HTTP 400 Bad Request
.name Ed\n
"""
And the stderr should contain exactly ""
Scenario: Non-success response doesn't choke on non-JSON
Given the GitHub API server:
......@@ -77,13 +75,12 @@ Feature: hub api
}
"""
When I run `hub api -t hello/world`
Then the exit status should be 1
And the stdout should contain exactly ""
And the stderr should contain exactly:
Then the exit status should be 22
And the stdout should contain exactly:
"""
Error: HTTP 400 Bad Request
Something went wrong\n
Something went wrong
"""
And the stderr should contain exactly ""
Scenario: GET query string
Given the GitHub API server:
......@@ -95,7 +92,7 @@ Feature: hub api
When I successfully run `hub api -XGET -Fname=Ed -Fnum=12 -Fbool=false -Fvoid=null hello/world`
Then the output should contain exactly:
"""
{"bool":"false","name":"Ed","num":"12","void":""}\n
{"bool":"false","name":"Ed","num":"12","void":""}
"""
Scenario: GET full URL
......@@ -109,7 +106,7 @@ Feature: hub api
When I successfully run `hub api https://api.github.com/hello/world`
Then the output should contain exactly:
"""
{"name":"Faye"}\n
{"name":"Faye"}
"""
Scenario: Avoid leaking token to a 3rd party
......@@ -123,7 +120,7 @@ Feature: hub api
When I successfully run `hub api http://example.com/hello/world`
Then the output should contain exactly:
"""
{"name":"Jet"}\n
{"name":"Jet"}
"""
Scenario: Custom headers
......@@ -137,7 +134,7 @@ Feature: hub api
When I successfully run `hub api hello/world -H 'x-foo:bar' -H 'Accept: text/json'`
Then the output should contain exactly:
"""
{"accept":"text/json","foo":"bar"}\n
{"accept":"text/json","foo":"bar"}
"""
Scenario: POST fields
......@@ -150,7 +147,7 @@ Feature: hub api
When I successfully run `hub api -f name=@hubot -Fnum=12 -Fbool=false -Fvoid=null hello/world`
Then the output should contain exactly:
"""
{"bool":false,"name":"@hubot","num":12,"void":null}\n
{"bool":false,"name":"@hubot","num":12,"void":null}
"""
Scenario: POST raw fields
......@@ -163,7 +160,7 @@ Feature: hub api
When I successfully run `hub api -fnum=12 -fbool=false hello/world`
Then the output should contain exactly:
"""
{"bool":"false","num":"12"}\n
{"bool":"false","num":"12"}
"""
Scenario: POST from stdin
......@@ -195,7 +192,7 @@ Feature: hub api
When I successfully run `hub api -F query='query {}' -Fname=Jet -Fsize=2 graphql`
Then the output should contain exactly:
"""
{"name":"Jet","size":2}\n
{"name":"Jet","size":2}
"""
Scenario: Repo context
......@@ -209,7 +206,7 @@ Feature: hub api
When I successfully run `hub api repos/{owner}/{repo}/commits`
Then the output should contain exactly:
"""
{"commits":12}\n
{"commits":12}
"""
Scenario: Repo context in graphql
......@@ -282,10 +279,9 @@ Feature: hub api
And I successfully run `hub api -t count --cache 5`
Then the output should contain exactly:
"""
.count 1
.count 2
.count 2
Error: HTTP 400 Bad Request
.count 1\n
.count 2\n
"""
Scenario: Avoid caching response if the OAuth token changes
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册