refactor: improve error handling and code clarity in client methods

- Enhanced error messages in the client methods to provide more context on failures.
- Updated test cases to correct terminology and ensure accurate error expectations.
- Refactored function signatures in media service methods for better readability and consistency.
This commit is contained in:
0x524a
2025-12-02 01:38:50 -05:00
parent 2ea36220f7
commit e530575bc1
5 changed files with 59 additions and 14 deletions
+11 -4
View File
@@ -127,7 +127,7 @@ func normalizeEndpoint(endpoint string) (string, error) {
// Parse as full URL // Parse as full URL
parsedURL, err := url.Parse(endpoint) parsedURL, err := url.Parse(endpoint)
if err != nil { if err != nil {
return "", err return "", fmt.Errorf("failed to parse endpoint URL: %w", err)
} }
if parsedURL.Host == "" { if parsedURL.Host == "" {
return "", fmt.Errorf("URL missing host") return "", fmt.Errorf("URL missing host")
@@ -284,9 +284,11 @@ func (c *Client) downloadWithBasicAuth(ctx context.Context, downloadURL string)
if err != nil { if err != nil {
return nil, fmt.Errorf("download request failed: %w", err) return nil, fmt.Errorf("download request failed: %w", err)
} }
//nolint:errcheck // Close error in defer is intentionally ignored
defer func() { _ = resp.Body.Close() }() defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
//nolint:errcheck // Error response body preview - ignore read errors
bodyPreview, _ := io.ReadAll(resp.Body) bodyPreview, _ := io.ReadAll(resp.Body)
bodyStr := string(bodyPreview) bodyStr := string(bodyPreview)
if len(bodyStr) > 200 { if len(bodyStr) > 200 {
@@ -360,9 +362,11 @@ func (c *Client) downloadWithDigestAuth(ctx context.Context, downloadURL string)
if err != nil { if err != nil {
return nil, fmt.Errorf("digest auth request failed: %w", err) return nil, fmt.Errorf("digest auth request failed: %w", err)
} }
//nolint:errcheck // Close error in defer is intentionally ignored
defer func() { _ = resp.Body.Close() }() defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
//nolint:errcheck // Error response body preview - ignore read errors
bodyPreview, _ := io.ReadAll(resp.Body) bodyPreview, _ := io.ReadAll(resp.Body)
bodyStr := string(bodyPreview) bodyStr := string(bodyPreview)
if len(bodyStr) > 200 { if len(bodyStr) > 200 {
@@ -409,7 +413,7 @@ func (d *digestAuthTransport) RoundTrip(req *http.Request) (*http.Response, erro
// First request without auth to get the challenge // First request without auth to get the challenge
resp, err := d.transport.RoundTrip(req) resp, err := d.transport.RoundTrip(req)
if err != nil { if err != nil {
return resp, err return resp, fmt.Errorf("transport round trip failed: %w", err)
} }
// If we get 401, handle digest auth challenge // If we get 401, handle digest auth challenge
@@ -426,11 +430,14 @@ func (d *digestAuthTransport) RoundTrip(req *http.Request) (*http.Response, erro
// Retry with auth // Retry with auth
resp, err = d.transport.RoundTrip(newReq) resp, err = d.transport.RoundTrip(newReq)
return resp, err if err != nil {
return resp, fmt.Errorf("transport round trip with auth failed: %w", err)
}
return resp, nil
} }
} }
return resp, err return resp, nil
} }
// createDigestAuthHeader creates a digest auth header from the challenge // createDigestAuthHeader creates a digest auth header from the challenge
+1 -1
View File
@@ -1293,7 +1293,7 @@ func TestDownloadFileContextCancellation(t *testing.T) {
_, err = client.DownloadFile(ctx, server.URL) _, err = client.DownloadFile(ctx, server.URL)
if err == nil { if err == nil {
t.Error("DownloadFile() expected error for cancelled context") t.Error("DownloadFile() expected error for canceled context")
} }
if !strings.Contains(err.Error(), "context deadline exceeded") && !strings.Contains(err.Error(), "context canceled") { if !strings.Contains(err.Error(), "context deadline exceeded") && !strings.Contains(err.Error(), "context canceled") {
t.Errorf("Expected context error, got: %v", err) t.Errorf("Expected context error, got: %v", err)
+4
View File
@@ -143,6 +143,8 @@ func (c *Client) GetIPAddressFilter(ctx context.Context) (*IPAddressFilter, erro
} }
// SetIPAddressFilter sets the IP address filter settings on a device // SetIPAddressFilter sets the IP address filter settings on a device
//
//nolint:dupl // Similar structure to AddIPAddressFilter but different operation
func (c *Client) SetIPAddressFilter(ctx context.Context, filter *IPAddressFilter) error { func (c *Client) SetIPAddressFilter(ctx context.Context, filter *IPAddressFilter) error {
type SetIPAddressFilter struct { type SetIPAddressFilter struct {
XMLName xml.Name `xml:"tds:SetIPAddressFilter"` XMLName xml.Name `xml:"tds:SetIPAddressFilter"`
@@ -196,6 +198,8 @@ func (c *Client) SetIPAddressFilter(ctx context.Context, filter *IPAddressFilter
} }
// AddIPAddressFilter adds an IP filter address to a device // AddIPAddressFilter adds an IP filter address to a device
//
//nolint:dupl // Similar structure to SetIPAddressFilter but different operation
func (c *Client) AddIPAddressFilter(ctx context.Context, filter *IPAddressFilter) error { func (c *Client) AddIPAddressFilter(ctx context.Context, filter *IPAddressFilter) error {
type AddIPAddressFilter struct { type AddIPAddressFilter struct {
XMLName xml.Name `xml:"tds:AddIPAddressFilter"` XMLName xml.Name `xml:"tds:AddIPAddressFilter"`
+37 -9
View File
@@ -2749,7 +2749,10 @@ func (c *Client) GetAudioSourceConfiguration(ctx context.Context, configurationT
} }
// GetVideoSourceConfigurationOptions retrieves available options for video source configuration // GetVideoSourceConfigurationOptions retrieves available options for video source configuration
func (c *Client) GetVideoSourceConfigurationOptions(ctx context.Context, configurationToken, profileToken string) (*VideoSourceConfigurationOptions, error) { func (c *Client) GetVideoSourceConfigurationOptions(
ctx context.Context,
configurationToken, profileToken string,
) (*VideoSourceConfigurationOptions, error) {
endpoint := c.mediaEndpoint endpoint := c.mediaEndpoint
if endpoint == "" { if endpoint == "" {
endpoint = c.endpoint endpoint = c.endpoint
@@ -2809,7 +2812,10 @@ func (c *Client) GetVideoSourceConfigurationOptions(ctx context.Context, configu
} }
// GetAudioSourceConfigurationOptions retrieves available options for audio source configuration // GetAudioSourceConfigurationOptions retrieves available options for audio source configuration
func (c *Client) GetAudioSourceConfigurationOptions(ctx context.Context, configurationToken, profileToken string) (*AudioSourceConfigurationOptions, error) { func (c *Client) GetAudioSourceConfigurationOptions(
ctx context.Context,
configurationToken, profileToken string,
) (*AudioSourceConfigurationOptions, error) {
endpoint := c.mediaEndpoint endpoint := c.mediaEndpoint
if endpoint == "" { if endpoint == "" {
endpoint = c.endpoint endpoint = c.endpoint
@@ -2854,7 +2860,11 @@ func (c *Client) GetAudioSourceConfigurationOptions(ctx context.Context, configu
} }
// SetVideoSourceConfiguration sets video source configuration // SetVideoSourceConfiguration sets video source configuration
func (c *Client) SetVideoSourceConfiguration(ctx context.Context, config *VideoSourceConfiguration, forcePersistence bool) error { func (c *Client) SetVideoSourceConfiguration(
ctx context.Context,
config *VideoSourceConfiguration,
forcePersistence bool,
) error {
endpoint := c.mediaEndpoint endpoint := c.mediaEndpoint
if endpoint == "" { if endpoint == "" {
endpoint = c.endpoint endpoint = c.endpoint
@@ -2956,7 +2966,10 @@ func (c *Client) SetAudioSourceConfiguration(ctx context.Context, config *AudioS
} }
// GetCompatibleVideoEncoderConfigurations retrieves compatible video encoder configurations for a profile // GetCompatibleVideoEncoderConfigurations retrieves compatible video encoder configurations for a profile
func (c *Client) GetCompatibleVideoEncoderConfigurations(ctx context.Context, profileToken string) ([]*VideoEncoderConfiguration, error) { func (c *Client) GetCompatibleVideoEncoderConfigurations(
ctx context.Context,
profileToken string,
) ([]*VideoEncoderConfiguration, error) {
endpoint := c.mediaEndpoint endpoint := c.mediaEndpoint
if endpoint == "" { if endpoint == "" {
endpoint = c.endpoint endpoint = c.endpoint
@@ -3034,7 +3047,10 @@ func (c *Client) GetCompatibleVideoEncoderConfigurations(ctx context.Context, pr
} }
// GetCompatibleVideoSourceConfigurations retrieves compatible video source configurations for a profile // GetCompatibleVideoSourceConfigurations retrieves compatible video source configurations for a profile
func (c *Client) GetCompatibleVideoSourceConfigurations(ctx context.Context, profileToken string) ([]*VideoSourceConfiguration, error) { func (c *Client) GetCompatibleVideoSourceConfigurations(
ctx context.Context,
profileToken string,
) ([]*VideoSourceConfiguration, error) {
endpoint := c.mediaEndpoint endpoint := c.mediaEndpoint
if endpoint == "" { if endpoint == "" {
endpoint = c.endpoint endpoint = c.endpoint
@@ -3099,7 +3115,10 @@ func (c *Client) GetCompatibleVideoSourceConfigurations(ctx context.Context, pro
} }
// GetCompatibleAudioEncoderConfigurations retrieves compatible audio encoder configurations for a profile // GetCompatibleAudioEncoderConfigurations retrieves compatible audio encoder configurations for a profile
func (c *Client) GetCompatibleAudioEncoderConfigurations(ctx context.Context, profileToken string) ([]*AudioEncoderConfiguration, error) { func (c *Client) GetCompatibleAudioEncoderConfigurations(
ctx context.Context,
profileToken string,
) ([]*AudioEncoderConfiguration, error) {
endpoint := c.mediaEndpoint endpoint := c.mediaEndpoint
if endpoint == "" { if endpoint == "" {
endpoint = c.endpoint endpoint = c.endpoint
@@ -3543,7 +3562,10 @@ func (c *Client) GetAudioDecoderConfigurations(ctx context.Context) ([]*AudioDec
} }
// GetAudioDecoderConfiguration retrieves a specific audio decoder configuration // GetAudioDecoderConfiguration retrieves a specific audio decoder configuration
func (c *Client) GetAudioDecoderConfiguration(ctx context.Context, configurationToken string) (*AudioDecoderConfiguration, error) { func (c *Client) GetAudioDecoderConfiguration(
ctx context.Context,
configurationToken string,
) (*AudioDecoderConfiguration, error) {
endpoint := c.mediaEndpoint endpoint := c.mediaEndpoint
if endpoint == "" { if endpoint == "" {
endpoint = c.endpoint endpoint = c.endpoint
@@ -3671,7 +3693,10 @@ func (c *Client) GetVideoAnalyticsConfigurations(ctx context.Context) ([]*VideoA
} }
// GetVideoAnalyticsConfiguration retrieves a specific video analytics configuration // GetVideoAnalyticsConfiguration retrieves a specific video analytics configuration
func (c *Client) GetVideoAnalyticsConfiguration(ctx context.Context, configurationToken string) (*VideoAnalyticsConfiguration, error) { func (c *Client) GetVideoAnalyticsConfiguration(
ctx context.Context,
configurationToken string,
) (*VideoAnalyticsConfiguration, error) {
endpoint := c.mediaEndpoint endpoint := c.mediaEndpoint
if endpoint == "" { if endpoint == "" {
endpoint = c.endpoint endpoint = c.endpoint
@@ -3801,7 +3826,10 @@ func (c *Client) SetVideoAnalyticsConfiguration(ctx context.Context, config *Vid
} }
// GetVideoAnalyticsConfigurationOptions retrieves available options for video analytics configuration // GetVideoAnalyticsConfigurationOptions retrieves available options for video analytics configuration
func (c *Client) GetVideoAnalyticsConfigurationOptions(ctx context.Context, configurationToken, profileToken string) (*VideoAnalyticsConfigurationOptions, error) { func (c *Client) GetVideoAnalyticsConfigurationOptions(
ctx context.Context,
configurationToken, profileToken string,
) (*VideoAnalyticsConfigurationOptions, error) {
endpoint := c.mediaEndpoint endpoint := c.mediaEndpoint
if endpoint == "" { if endpoint == "" {
endpoint = c.endpoint endpoint = c.endpoint
+6
View File
@@ -110,6 +110,8 @@ func _DisabledTestHandleGetStatus(t *testing.T) {
} }
// TestHandleAbsoluteMove - DISABLED due to SOAP namespace requirements // TestHandleAbsoluteMove - DISABLED due to SOAP namespace requirements
//
//nolint:dupl // Disabled test functions have similar structure
func _DisabledTestHandleAbsoluteMove(t *testing.T) { func _DisabledTestHandleAbsoluteMove(t *testing.T) {
config := createTestConfig() config := createTestConfig()
server, _ := New(config) server, _ := New(config)
@@ -150,6 +152,8 @@ func _DisabledTestHandleAbsoluteMove(t *testing.T) {
} }
// TestHandleRelativeMove - DISABLED due to SOAP namespace requirements // TestHandleRelativeMove - DISABLED due to SOAP namespace requirements
//
//nolint:dupl // Disabled test functions have similar structure
func _DisabledTestHandleRelativeMove(t *testing.T) { func _DisabledTestHandleRelativeMove(t *testing.T) {
config := createTestConfig() config := createTestConfig()
server, _ := New(config) server, _ := New(config)
@@ -190,6 +194,8 @@ func _DisabledTestHandleRelativeMove(t *testing.T) {
} }
// TestHandleContinuousMove - DISABLED due to SOAP namespace requirements // TestHandleContinuousMove - DISABLED due to SOAP namespace requirements
//
//nolint:dupl // Disabled test functions have similar structure
func _DisabledTestHandleContinuousMove(t *testing.T) { func _DisabledTestHandleContinuousMove(t *testing.T) {
config := createTestConfig() config := createTestConfig()
server, _ := New(config) server, _ := New(config)