summaryrefslogtreecommitdiff
path: root/services/worker/internal/fetcher/errors.go
blob: abf5553f2eef9e5743da1d1f2a56842af8d50221 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package fetcher

import (
	"errors"
	"fmt"
	"net"
	"net/url"
	"strings"
)

type FetchError struct {
	StatusCode      int
	UserMessage     string
	Retryable       bool
	UnderlyingError error
}

func (fetchError *FetchError) Error() string {
	if fetchError.UnderlyingError != nil {
		return fmt.Sprintf("%s: %v", fetchError.UserMessage, fetchError.UnderlyingError)
	}

	return fetchError.UserMessage
}

func (fetchError *FetchError) Unwrap() error {
	return fetchError.UnderlyingError
}

func ClassifyError(originalError error, statusCode int) *FetchError {
	if statusCode >= 400 {
		return classifyHTTPStatusCode(statusCode, originalError)
	}

	return classifyNetworkError(originalError)
}

func classifyHTTPStatusCode(statusCode int, originalError error) *FetchError {
	switch statusCode {
	case 401:
		return &FetchError{
			StatusCode:      statusCode,
			UserMessage:     "authentication required - check feed credentials",
			Retryable:       false,
			UnderlyingError: originalError,
		}
	case 403:
		return &FetchError{
			StatusCode:      statusCode,
			UserMessage:     "access forbidden - feed may require different credentials",
			Retryable:       false,
			UnderlyingError: originalError,
		}
	case 404:
		return &FetchError{
			StatusCode:      statusCode,
			UserMessage:     "feed not found - URL may be incorrect or feed removed",
			Retryable:       false,
			UnderlyingError: originalError,
		}
	case 410:
		return &FetchError{
			StatusCode:      statusCode,
			UserMessage:     "feed permanently removed",
			Retryable:       false,
			UnderlyingError: originalError,
		}
	case 429:
		return &FetchError{
			StatusCode:      statusCode,
			UserMessage:     "rate limited by feed server - will retry later",
			Retryable:       true,
			UnderlyingError: originalError,
		}
	case 500, 502, 503, 504:
		return &FetchError{
			StatusCode:      statusCode,
			UserMessage:     fmt.Sprintf("feed server error (HTTP %d) - will retry later", statusCode),
			Retryable:       true,
			UnderlyingError: originalError,
		}
	default:
		return &FetchError{
			StatusCode:      statusCode,
			UserMessage:     fmt.Sprintf("unexpected HTTP status %d", statusCode),
			Retryable:       statusCode >= 500,
			UnderlyingError: originalError,
		}
	}
}

func classifyNetworkError(originalError error) *FetchError {
	if originalError == nil {
		return &FetchError{
			UserMessage: "unknown fetch error",
			Retryable:   true,
		}
	}

	var dnsError *net.DNSError

	if errors.As(originalError, &dnsError) {
		return &FetchError{
			UserMessage:     "DNS resolution failed - check feed URL",
			Retryable:       !dnsError.IsNotFound,
			UnderlyingError: originalError,
		}
	}

	var urlError *url.Error

	if errors.As(originalError, &urlError) {
		if urlError.Timeout() {
			return &FetchError{
				UserMessage:     "connection timed out - feed server may be slow",
				Retryable:       true,
				UnderlyingError: originalError,
			}
		}
	}

	var netOpError *net.OpError

	if errors.As(originalError, &netOpError) {
		return &FetchError{
			UserMessage:     "network connection error - will retry later",
			Retryable:       true,
			UnderlyingError: originalError,
		}
	}

	if strings.Contains(originalError.Error(), "certificate") || strings.Contains(originalError.Error(), "tls") {
		return &FetchError{
			UserMessage:     "TLS/certificate error - feed server has invalid certificate",
			Retryable:       false,
			UnderlyingError: originalError,
		}
	}

	return &FetchError{
		UserMessage:     "unexpected network error",
		Retryable:       true,
		UnderlyingError: originalError,
	}
}