fix(bloomberg): avoid collection timeouts

This commit is contained in:
2026-11-24 09:41:26 +01:00
parent d129af0d56
commit 1981018ae1
11 changed files with 641 additions and 73 deletions

View File

@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
@ -50,27 +51,30 @@ func (b *Bloomberg) ScrapeWithCredentials(ctx context.Context, username, passwor
payload, _ := json.Marshal(scraperRequest{Username: username, Password: password})
req, err := http.NewRequestWithContext(ctx, http.MethodPost, b.scraperURL+"/bloomberg/scrape", bytes.NewReader(payload))
if err != nil {
return nil, err
return nil, errors.New("bloomberg: request construction failed")
}
req.Header.Set("Content-Type", "application/json")
resp, err := b.client.Do(req)
if err != nil {
return nil, fmt.Errorf("scraper service unreachable: %w", err)
return nil, scraperFailure(ctx, err)
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("scraper service HTTP %d: %s", resp.StatusCode, body)
return nil, fmt.Errorf("bloomberg: scraper HTTP %d", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, scraperFailure(ctx, err)
}
var result scraperResponse
if err := json.Unmarshal(body, &result); err != nil {
return nil, fmt.Errorf("parse scraper response: %w", err)
return nil, errors.New("bloomberg: invalid response")
}
if result.Error != "" {
return nil, fmt.Errorf("bloomberg: %s", result.Error)
return nil, errors.New("bloomberg: scraper failure")
}
var articles []scraper.Article
@ -90,7 +94,6 @@ func (b *Bloomberg) ScrapeWithCredentials(ctx context.Context, username, passwor
syms := scraper.DetectSymbols(title, symbols)
articles = append(articles, scraper.Article{
Title: title,
Content: title,
URL: url,
PublishedAt: date,
Symbols: syms,
@ -99,3 +102,18 @@ func (b *Bloomberg) ScrapeWithCredentials(ctx context.Context, username, passwor
fmt.Printf("bloomberg: %d articles fetched\n", len(articles))
return articles, nil
}
// Preserve only typed timeout/cancellation identity, never the raw transport error.
func scraperFailure(ctx context.Context, err error) error {
if errors.Is(ctx.Err(), context.DeadlineExceeded) || errors.Is(err, context.DeadlineExceeded) {
return fmt.Errorf("bloomberg: timeout: %w", context.DeadlineExceeded)
}
if errors.Is(ctx.Err(), context.Canceled) || errors.Is(err, context.Canceled) {
return fmt.Errorf("bloomberg: canceled: %w", context.Canceled)
}
var timeout interface{ Timeout() bool }
if errors.As(err, &timeout) && timeout.Timeout() {
return fmt.Errorf("bloomberg: timeout: %w", context.DeadlineExceeded)
}
return errors.New("bloomberg: transport failure")
}

View File

@ -0,0 +1,58 @@
package bloomberg
import (
"context"
"errors"
"io"
"net/http"
"net/url"
"strings"
"testing"
)
func TestTicket35AbsentContentIsEmptyAndScraperErrorsAreSanitized(t *testing.T) {
cases := []struct {
name, body string
status int
err error
want string
}{
{name: "success", status: 200, body: `{"articles":[{"title":"fixture title","url":"https://fixture.invalid/article"}]}`},
{name: "http", status: 503, body: "PRIVATE_BODY https://private.invalid", want: "HTTP 503"},
{name: "service", status: 200, body: `{"error":"PRIVATE_BODY https://private.invalid"}`, want: "scraper failure"},
{name: "transport", err: &url.Error{Op: "POST", URL: "https://PRIVATE_URL.invalid", Err: errors.New("PRIVATE_BODY")}, want: "transport failure"},
{name: "timeout", err: &url.Error{Op: "POST", URL: "https://PRIVATE_URL.invalid", Err: context.DeadlineExceeded}, want: "timeout"},
{name: "canceled", err: &url.Error{Op: "POST", URL: "https://PRIVATE_URL.invalid", Err: context.Canceled}, want: "canceled"},
{name: "json", status: 200, body: `{"PRIVATE_BODY":`, want: "invalid response"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
b := New("http://PRIVATE_URL.invalid")
b.client = &http.Client{Transport: dateTransport(func(*http.Request) (*http.Response, error) {
if tc.err != nil {
return nil, tc.err
}
return &http.Response{StatusCode: tc.status, Body: io.NopCloser(strings.NewReader(tc.body)), Header: make(http.Header)}, nil
})}
articles, err := b.ScrapeWithCredentials(context.Background(), "PRIVATE_USER", "PRIVATE_SECRET", nil)
if tc.want == "" {
if err != nil || len(articles) != 1 || articles[0].Content != "" || articles[0].PublishedAt != nil {
t.Fatal("invented content/date", err)
}
return
}
if err == nil || !strings.Contains(err.Error(), tc.want) || articles != nil {
t.Fatalf("unexpected result: %v", err)
}
if strings.Contains(err.Error(), "PRIVATE") || strings.Contains(err.Error(), "http://") || strings.Contains(err.Error(), "https://") {
t.Fatal("raw scraper error leaked")
}
if tc.name == "timeout" && !errors.Is(err, context.DeadlineExceeded) {
t.Fatal("timeout identity lost")
}
if tc.name == "canceled" && !errors.Is(err, context.Canceled) {
t.Fatal("cancellation identity lost")
}
})
}
}