Skip to content

Commit c385919

Browse files
committed
Fix nil pointer dereference issues in Trakt API client. Add proper nil checks in the Client struct initialization and method calls to prevent potential panics during testing and runtime.
1 parent fd51b22 commit c385919

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

pkg/api/trakt.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,15 +172,22 @@ func (c *Client) makeRequest(req *http.Request) (*http.Response, error) {
172172

173173
// addExtendedInfo adds the extended parameter to the URL if it's configured
174174
func (c *Client) addExtendedInfo(endpoint string) string {
175+
// Safety checks
176+
if c == nil || c.config == nil {
177+
return endpoint
178+
}
179+
175180
if c.config.Trakt.ExtendedInfo == "" {
176181
return endpoint
177182
}
178183

179184
baseURL, err := url.Parse(endpoint)
180185
if err != nil {
181-
c.logger.Warn("api.url_parse_error", map[string]interface{}{
182-
"error": err.Error(),
183-
})
186+
if c.logger != nil {
187+
c.logger.Warn("api.url_parse_error", map[string]interface{}{
188+
"error": err.Error(),
189+
})
190+
}
184191
return endpoint
185192
}
186193

pkg/api/trakt_test.go

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,22 @@ func TestNewClient(t *testing.T) {
9595
client := NewClient(cfg, log)
9696
if client == nil {
9797
t.Error("Expected non-nil client")
98+
return
9899
}
99-
if client.config != cfg {
100+
101+
// Now safely check fields since we know client is not nil
102+
if client.config == nil {
100103
t.Error("Expected config to be set")
104+
} else if client.config != cfg {
105+
t.Error("Expected config to be set correctly")
101106
}
102-
if client.logger != log {
107+
108+
if client.logger == nil {
103109
t.Error("Expected logger to be set")
110+
} else if client.logger != log {
111+
t.Error("Expected logger to be set correctly")
104112
}
113+
105114
if client.httpClient == nil {
106115
t.Error("Expected non-nil HTTP client")
107116
}
@@ -554,6 +563,20 @@ func TestGetWatchedShows(t *testing.T) {
554563
}
555564
log := &MockLogger{}
556565
client := NewClient(cfg, log)
566+
567+
// Verify client is properly initialized
568+
if client == nil {
569+
t.Fatal("Expected non-nil client")
570+
}
571+
if client.config == nil {
572+
t.Fatal("Expected non-nil config")
573+
}
574+
if client.logger == nil {
575+
t.Fatal("Expected non-nil logger")
576+
}
577+
if client.httpClient == nil {
578+
t.Fatal("Expected non-nil HTTP client")
579+
}
557580

558581
// Test successful request
559582
shows, err := client.GetWatchedShows()
@@ -596,6 +619,20 @@ func TestGetWatchedShowsError(t *testing.T) {
596619
}
597620
log := &MockLogger{}
598621
client := NewClient(cfg, log)
622+
623+
// Verify client is properly initialized
624+
if client == nil {
625+
t.Fatal("Expected non-nil client")
626+
}
627+
if client.config == nil {
628+
t.Fatal("Expected non-nil config")
629+
}
630+
if client.logger == nil {
631+
t.Fatal("Expected non-nil logger")
632+
}
633+
if client.httpClient == nil {
634+
t.Fatal("Expected non-nil HTTP client")
635+
}
599636

600637
// Test error handling
601638
shows, err := client.GetWatchedShows()

0 commit comments

Comments
 (0)