浏览代码

Merge pull request #83 from vcabbage/display-http-errors

Display HTTP errors from server
Raul Silvera 8 年前
父节点
当前提交
e0cb01d99a
共有 2 个文件被更改,包括 26 次插入3 次删除
  1. 14
    1
      internal/driver/fetch.go
  2. 12
    2
      internal/symbolizer/symbolizer.go

+ 14
- 1
internal/driver/fetch.go 查看文件

18
 	"bytes"
18
 	"bytes"
19
 	"fmt"
19
 	"fmt"
20
 	"io"
20
 	"io"
21
+	"io/ioutil"
21
 	"net/http"
22
 	"net/http"
22
 	"net/url"
23
 	"net/url"
23
 	"os"
24
 	"os"
24
 	"os/exec"
25
 	"os/exec"
25
 	"path/filepath"
26
 	"path/filepath"
26
 	"strconv"
27
 	"strconv"
28
+	"strings"
27
 	"sync"
29
 	"sync"
28
 	"time"
30
 	"time"
29
 
31
 
391
 		return nil, fmt.Errorf("http fetch: %v", err)
393
 		return nil, fmt.Errorf("http fetch: %v", err)
392
 	}
394
 	}
393
 	if resp.StatusCode != http.StatusOK {
395
 	if resp.StatusCode != http.StatusOK {
394
-		return nil, fmt.Errorf("server response: %s", resp.Status)
396
+		defer resp.Body.Close()
397
+		return nil, statusCodeError(resp)
395
 	}
398
 	}
396
 
399
 
397
 	return resp.Body, nil
400
 	return resp.Body, nil
398
 }
401
 }
399
 
402
 
403
+func statusCodeError(resp *http.Response) error {
404
+	if resp.Header.Get("X-Go-Pprof") != "" && strings.Contains(resp.Header.Get("Content-Type"), "text/plain") {
405
+		// error is from pprof endpoint
406
+		if body, err := ioutil.ReadAll(resp.Body); err == nil {
407
+			return fmt.Errorf("server response: %s - %s", resp.Status, body)
408
+		}
409
+	}
410
+	return fmt.Errorf("server response: %s", resp.Status)
411
+}
412
+
400
 // isPerfFile checks if a file is in perf.data format. It also returns false
413
 // isPerfFile checks if a file is in perf.data format. It also returns false
401
 // if it encounters an error during the check.
414
 // if it encounters an error during the check.
402
 func isPerfFile(path string) bool {
415
 func isPerfFile(path string) bool {

+ 12
- 2
internal/symbolizer/symbolizer.go 查看文件

94
 	if err != nil {
94
 	if err != nil {
95
 		return nil, fmt.Errorf("http post %s: %v", source, err)
95
 		return nil, fmt.Errorf("http post %s: %v", source, err)
96
 	}
96
 	}
97
+	defer resp.Body.Close()
97
 	if resp.StatusCode != http.StatusOK {
98
 	if resp.StatusCode != http.StatusOK {
98
-		return nil, fmt.Errorf("server response: %s", resp.Status)
99
+		return nil, statusCodeError(resp)
99
 	}
100
 	}
100
-	defer resp.Body.Close()
101
 	return ioutil.ReadAll(resp.Body)
101
 	return ioutil.ReadAll(resp.Body)
102
 }
102
 }
103
 
103
 
104
+func statusCodeError(resp *http.Response) error {
105
+	if resp.Header.Get("X-Go-Pprof") != "" && strings.Contains(resp.Header.Get("Content-Type"), "text/plain") {
106
+		// error is from pprof endpoint
107
+		if body, err := ioutil.ReadAll(resp.Body); err == nil {
108
+			return fmt.Errorf("server response: %s - %s", resp.Status, body)
109
+		}
110
+	}
111
+	return fmt.Errorf("server response: %s", resp.Status)
112
+}
113
+
104
 // doLocalSymbolize adds symbol and line number information to all locations
114
 // doLocalSymbolize adds symbol and line number information to all locations
105
 // in a profile. mode enables some options to control
115
 // in a profile. mode enables some options to control
106
 // symbolization.
116
 // symbolization.