Skip to content

Commit d135fe3

Browse files
author
Clément Denoix
authored
fix(crawler): Correctly display error messages (#158)
1 parent e1cff9f commit d135fe3

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

api/crawler/client.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,15 @@ func (c *Client) request(res interface{}, method string, path string, body inter
6565
for _, e := range errResp.Err.Errors {
6666
errs = append(errs, e.Message)
6767
}
68-
return fmt.Errorf("%s: %s", errResp.Err.Message, errs)
68+
return fmt.Errorf("[%s] %s", errResp.Err.Code, errs)
6969
}
7070

71-
return errors.New(errResp.Err.Message)
71+
// Message might be empty
72+
if errResp.Err.Message == "" {
73+
return errors.New(errResp.Err.Code)
74+
} else {
75+
return fmt.Errorf("[%s] %s", errResp.Err.Code, errResp.Err.Message)
76+
}
7277
}
7378

7479
if res != nil {

pkg/cmd/crawler/crawl/crawl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func runCrawlCmd(opts *CrawlOptions) error {
9090
_, err = client.CrawlURLs(opts.ID, opts.URLs, opts.Save, opts.SaveSpecified)
9191
opts.IO.StopProgressIndicator()
9292
if err != nil {
93-
return err
93+
return fmt.Errorf("%s Crawler API error: %w", cs.FailureIcon(), err)
9494
}
9595

9696
if opts.IO.IsStdoutTTY() {

pkg/cmd/crawler/crawl/crawl_test.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ func Test_runCrawlCmd(t *testing.T) {
113113
id string
114114
urls []string
115115
isTTY bool
116+
wantErr string
116117
wantOut string
117118
}{
118119
{
@@ -139,19 +140,32 @@ func Test_runCrawlCmd(t *testing.T) {
139140
isTTY: true,
140141
wantOut: "✓ Successfully requested crawl for 2 URLs on crawler my-crawler\n",
141142
},
143+
{
144+
name: "TTY, error (message+code)",
145+
cli: "my-crawler --urls http://example.com",
146+
id: "my-crawler",
147+
urls: []string{"http://example.com"},
148+
isTTY: true,
149+
wantErr: "X Crawler API error: [not-found] Crawler not-found not found",
150+
},
142151
}
143152

144153
for _, tt := range tests {
145154
t.Run(tt.name, func(t *testing.T) {
146155
r := httpmock.Registry{}
147-
r.Register(httpmock.REST("POST", "api/1/crawlers/"+tt.id+"/urls/crawl"), httpmock.JSONResponse(crawler.TaskIDResponse{TaskID: "taskID"}))
156+
if tt.wantErr == "" {
157+
r.Register(httpmock.REST("POST", "api/1/crawlers/"+tt.id+"/urls/crawl"), httpmock.JSONResponse(crawler.TaskIDResponse{TaskID: "taskID"}))
158+
} else {
159+
r.Register(httpmock.REST("POST", "api/1/crawlers/"+tt.id+"/urls/crawl"), httpmock.ErrorResponseWithBody(crawler.ErrResponse{Err: crawler.Err{Code: "not-found", Message: "Crawler not-found not found"}}))
160+
}
148161
defer r.Verify(t)
149162

150163
f, out := test.NewFactory(tt.isTTY, &r, nil, "")
151164
cmd := NewCrawlCmd(f, nil)
152165
out, err := test.Execute(cmd, tt.cli, out)
153166
if err != nil {
154-
t.Fatal(err)
167+
assert.Equal(t, tt.wantErr, err.Error())
168+
return
155169
}
156170

157171
assert.Equal(t, tt.wantOut, out.String())

pkg/httpmock/stub.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ func ErrorResponse() Responder {
4848
}
4949
}
5050

51+
func ErrorResponseWithBody(body interface{}) Responder {
52+
return func(req *http.Request) (*http.Response, error) {
53+
b, _ := json.Marshal(body)
54+
return httpResponse(400, req, bytes.NewBuffer(b)), nil
55+
}
56+
}
57+
5158
func httpResponse(status int, req *http.Request, body io.Reader) *http.Response {
5259
return &http.Response{
5360
StatusCode: status,

0 commit comments

Comments
 (0)