Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix(crawler): Correctly display error messages #158

Merged
merged 1 commit into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions api/crawler/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/crawler/crawl/crawl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
18 changes: 16 additions & 2 deletions pkg/cmd/crawler/crawl/crawl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func Test_runCrawlCmd(t *testing.T) {
id string
urls []string
isTTY bool
wantErr string
wantOut string
}{
{
Expand All @@ -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())
Expand Down
7 changes: 7 additions & 0 deletions pkg/httpmock/stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading