From d135fe31cac59e45954cb7d6689adb3d07b2db9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Denoix?= Date: Fri, 3 May 2024 10:38:18 +0200 Subject: [PATCH] fix(crawler): Correctly display error messages (#158) --- api/crawler/client.go | 9 +++++++-- pkg/cmd/crawler/crawl/crawl.go | 2 +- pkg/cmd/crawler/crawl/crawl_test.go | 18 ++++++++++++++++-- pkg/httpmock/stub.go | 7 +++++++ 4 files changed, 31 insertions(+), 5 deletions(-) diff --git a/api/crawler/client.go b/api/crawler/client.go index ecd5d76d..8ac9a101 100644 --- a/api/crawler/client.go +++ b/api/crawler/client.go @@ -65,10 +65,15 @@ func (c *Client) request(res interface{}, method string, path string, body inter for _, e := range errResp.Err.Errors { errs = append(errs, e.Message) } - return fmt.Errorf("%s: %s", errResp.Err.Message, errs) + return fmt.Errorf("[%s] %s", errResp.Err.Code, errs) } - return errors.New(errResp.Err.Message) + // Message might be empty + if errResp.Err.Message == "" { + return errors.New(errResp.Err.Code) + } else { + return fmt.Errorf("[%s] %s", errResp.Err.Code, errResp.Err.Message) + } } if res != nil { diff --git a/pkg/cmd/crawler/crawl/crawl.go b/pkg/cmd/crawler/crawl/crawl.go index 60baac0f..c04306ea 100644 --- a/pkg/cmd/crawler/crawl/crawl.go +++ b/pkg/cmd/crawler/crawl/crawl.go @@ -90,7 +90,7 @@ func runCrawlCmd(opts *CrawlOptions) error { _, err = client.CrawlURLs(opts.ID, opts.URLs, opts.Save, opts.SaveSpecified) opts.IO.StopProgressIndicator() if err != nil { - return err + return fmt.Errorf("%s Crawler API error: %w", cs.FailureIcon(), err) } if opts.IO.IsStdoutTTY() { diff --git a/pkg/cmd/crawler/crawl/crawl_test.go b/pkg/cmd/crawler/crawl/crawl_test.go index fa9f9809..d45c248d 100644 --- a/pkg/cmd/crawler/crawl/crawl_test.go +++ b/pkg/cmd/crawler/crawl/crawl_test.go @@ -113,6 +113,7 @@ func Test_runCrawlCmd(t *testing.T) { id string urls []string isTTY bool + wantErr string wantOut string }{ { @@ -139,19 +140,32 @@ func Test_runCrawlCmd(t *testing.T) { isTTY: true, wantOut: "✓ Successfully requested crawl for 2 URLs on crawler my-crawler\n", }, + { + name: "TTY, error (message+code)", + cli: "my-crawler --urls http://example.com", + id: "my-crawler", + urls: []string{"http://example.com"}, + isTTY: true, + wantErr: "X Crawler API error: [not-found] Crawler not-found not found", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { r := httpmock.Registry{} - r.Register(httpmock.REST("POST", "api/1/crawlers/"+tt.id+"/urls/crawl"), httpmock.JSONResponse(crawler.TaskIDResponse{TaskID: "taskID"})) + if tt.wantErr == "" { + r.Register(httpmock.REST("POST", "api/1/crawlers/"+tt.id+"/urls/crawl"), httpmock.JSONResponse(crawler.TaskIDResponse{TaskID: "taskID"})) + } else { + 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"}})) + } defer r.Verify(t) f, out := test.NewFactory(tt.isTTY, &r, nil, "") cmd := NewCrawlCmd(f, nil) out, err := test.Execute(cmd, tt.cli, out) if err != nil { - t.Fatal(err) + assert.Equal(t, tt.wantErr, err.Error()) + return } assert.Equal(t, tt.wantOut, out.String()) diff --git a/pkg/httpmock/stub.go b/pkg/httpmock/stub.go index 6af3507d..25648215 100644 --- a/pkg/httpmock/stub.go +++ b/pkg/httpmock/stub.go @@ -48,6 +48,13 @@ func ErrorResponse() Responder { } } +func ErrorResponseWithBody(body interface{}) Responder { + return func(req *http.Request) (*http.Response, error) { + b, _ := json.Marshal(body) + return httpResponse(400, req, bytes.NewBuffer(b)), nil + } +} + func httpResponse(status int, req *http.Request, body io.Reader) *http.Response { return &http.Response{ StatusCode: status,