diff options
| author | Jason Cameron <git@jasoncameron.dev> | 2025-04-13 15:59:58 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-04-13 15:59:58 -0400 |
| commit | 9865e3ded82998e810c8eb0dca6195eada7c446d (patch) | |
| tree | d6e02819c93641496d26ec6da2b808d2351045b3 /internal/ogtags | |
| parent | 3438595f32650c5e355063e70398436fd5957056 (diff) | |
| download | anubis-9865e3ded82998e810c8eb0dca6195eada7c446d.tar.xz anubis-9865e3ded82998e810c8eb0dca6195eada7c446d.zip | |
fix(fetch): improve error handling for Content-Type parsing (#253)
* fix(fetch): improve error handling for Content-Type parsing
Signed-off-by: Jason Cameron <git@jasoncameron.dev>
* fix(fetch): rename OgHandledError to ErrOgHandled for statichcheck to like me
Signed-off-by: Jason Cameron <git@jasoncameron.dev>
---------
Signed-off-by: Jason Cameron <git@jasoncameron.dev>
Diffstat (limited to 'internal/ogtags')
| -rw-r--r-- | internal/ogtags/cache.go | 4 | ||||
| -rw-r--r-- | internal/ogtags/fetch.go | 12 |
2 files changed, 9 insertions, 7 deletions
diff --git a/internal/ogtags/cache.go b/internal/ogtags/cache.go index 0d1a615..b3e35e4 100644 --- a/internal/ogtags/cache.go +++ b/internal/ogtags/cache.go @@ -23,8 +23,8 @@ func (c *OGTagCache) GetOGTags(url *url.URL) (map[string]string, error) { if errors.Is(err, syscall.ECONNREFUSED) { slog.Debug("Connection refused, returning empty tags") return nil, nil - } else if errors.Is(err, ErrNotFound) { - // not even worth a debug log... + } else if errors.Is(err, ErrOgHandled) { + // Error was handled in fetchHTMLDocument, return empty tags return nil, nil } if err != nil { diff --git a/internal/ogtags/fetch.go b/internal/ogtags/fetch.go index 3ea9aac..d4db711 100644 --- a/internal/ogtags/fetch.go +++ b/internal/ogtags/fetch.go @@ -11,8 +11,8 @@ import ( ) var ( - ErrNotFound = errors.New("page not found") /*todo: refactor into common errors lib? */ - emptyMap = map[string]string{} // used to indicate an empty result in the cache. Can't use nil as it would be a cache miss. + ErrOgHandled = errors.New("og: handled error") // used to indicate that the error was handled and should not be logged + emptyMap = map[string]string{} // used to indicate an empty result in the cache. Can't use nil as it would be a cache miss. ) func (c *OGTagCache) fetchHTMLDocument(urlStr string) (*html.Node, error) { @@ -31,7 +31,7 @@ func (c *OGTagCache) fetchHTMLDocument(urlStr string) (*html.Node, error) { if resp.StatusCode != http.StatusOK { slog.Debug("og: received non-OK status code", "url", urlStr, "status", resp.StatusCode) c.cache.Set(urlStr, emptyMap, c.ogTimeToLive) // Cache empty result for non-successful status codes - return nil, ErrNotFound + return nil, fmt.Errorf("%w: page not found", ErrOgHandled) } // Check content type @@ -43,11 +43,13 @@ func (c *OGTagCache) fetchHTMLDocument(urlStr string) (*html.Node, error) { mediaType, _, err := mime.ParseMediaType(ct) if err != nil { // Malformed Content-Type header - return nil, fmt.Errorf("invalid Content-Type '%s': %w", ct, err) + slog.Debug("og: malformed Content-Type header", "url", urlStr, "contentType", ct) + return nil, fmt.Errorf("%w malformed Content-Type header: %w", ErrOgHandled, err) } if mediaType != "text/html" && mediaType != "application/xhtml+xml" { - return nil, fmt.Errorf("unsupported Content-Type: %s", mediaType) + slog.Debug("og: unsupported Content-Type", "url", urlStr, "contentType", mediaType) + return nil, fmt.Errorf("%w unsupported Content-Type: %s", ErrOgHandled, mediaType) } } |
