Skip to content

Commit c03ca3c

Browse files
Add better error behavior for sift (#161)
1 parent de6926b commit c03ca3c

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

tools/sift.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -440,18 +440,30 @@ func (c *siftClient) makeRequest(ctx context.Context, method, path string, body
440440

441441
response, err := c.client.Do(req)
442442
if err != nil {
443-
return nil, err
443+
return nil, fmt.Errorf("executing request: %w", err)
444444
}
445-
446-
reader := io.LimitReader(response.Body, 1024*1024*48)
447445
defer response.Body.Close()
448446

447+
// Check for non-200 status code (matching Loki client's logic)
448+
if response.StatusCode != http.StatusOK {
449+
bodyBytes, _ := io.ReadAll(response.Body) // Read full body on error
450+
return nil, fmt.Errorf("API request returned status code %d: %s", response.StatusCode, string(bodyBytes))
451+
}
452+
453+
// Read the response body with a limit to prevent memory issues
454+
reader := io.LimitReader(response.Body, 1024*1024*48) // 48MB limit
449455
buf, err := io.ReadAll(reader)
450456
if err != nil {
451457
return nil, fmt.Errorf("failed to read response body: %w", err)
452458
}
453459

454-
return buf, nil
460+
// Check if the response is empty (matching Loki client's logic)
461+
if len(buf) == 0 {
462+
return nil, fmt.Errorf("empty response from API")
463+
}
464+
465+
// Trim any whitespace that might cause JSON parsing issues (matching Loki client's logic)
466+
return bytes.TrimSpace(buf), nil
455467
}
456468

457469
// getSiftInvestigation is a helper method to get the current status of an investigation

0 commit comments

Comments
 (0)