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