Skip to content

Commit 8c2fbbe

Browse files
committed
换go1.13错误检查
1 parent 87265d6 commit 8c2fbbe

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

ghttp/ghttp.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package ghttp
22

33
import (
44
"encoding/json"
5+
"errors"
56
"fmt"
67
"github.com/guonaihong/flag"
78
"github.com/guonaihong/gurl/core"
@@ -12,9 +13,7 @@ import (
1213
"github.com/guonaihong/gurl/utils"
1314
"io"
1415
_ "io/ioutil"
15-
"net"
1616
"net/http"
17-
"net/url"
1817
"os"
1918
"runtime"
2019
"strings"
@@ -172,15 +171,9 @@ func CmdErr(err error) {
172171
return
173172
}
174173

175-
if uerr, ok := err.(*url.Error); ok {
176-
if noerr, ok := uerr.Err.(*net.OpError); ok {
177-
if scerr, ok := noerr.Err.(*os.SyscallError); ok {
178-
if scerr.Err == syscall.ECONNREFUSED {
179-
fmt.Printf("gurl: (7) couldn't connect to host\n")
180-
os.Exit(7)
181-
}
182-
}
183-
}
174+
if errors.Is(err, syscall.ECONNREFUSED) {
175+
fmt.Printf("gurl: (7) couldn't connect to host\n")
176+
return
184177
}
185178

186179
fmt.Printf("%s\n", err)

ghttp/ghttp_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package ghttp
2+
3+
import (
4+
"errors"
5+
"github.com/stretchr/testify/assert"
6+
"net/http"
7+
"syscall"
8+
"testing"
9+
)
10+
11+
func Test_Ghttp_CmdErr(t *testing.T) {
12+
_, err := http.Get("http://127.0.0.1:3333")
13+
assert.True(t, errors.Is(err, syscall.ECONNREFUSED))
14+
}

0 commit comments

Comments
 (0)