browser.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package browser // import "miniflux.app/v2/internal/reader/browser"
  4. import (
  5. "miniflux.app/v2/internal/errors"
  6. "miniflux.app/v2/internal/http/client"
  7. )
  8. var (
  9. errRequestFailed = "Unable to open this link: %v"
  10. errServerFailure = "Unable to fetch this resource (Status Code = %d)"
  11. errEncoding = "Unable to normalize encoding: %q"
  12. errEmptyFeed = "This feed is empty"
  13. errResourceNotFound = "Resource not found (404), this feed doesn't exist anymore, check the feed URL"
  14. errNotAuthorized = "You are not authorized to access this resource (invalid username/password)"
  15. )
  16. // Exec executes a HTTP request and handles errors.
  17. func Exec(request *client.Client) (*client.Response, *errors.LocalizedError) {
  18. response, err := request.Get()
  19. if err != nil {
  20. if e, ok := err.(*errors.LocalizedError); ok {
  21. return nil, e
  22. }
  23. return nil, errors.NewLocalizedError(errRequestFailed, err)
  24. }
  25. if response.IsNotFound() {
  26. return nil, errors.NewLocalizedError(errResourceNotFound)
  27. }
  28. if response.IsNotAuthorized() {
  29. return nil, errors.NewLocalizedError(errNotAuthorized)
  30. }
  31. if response.HasServerFailure() {
  32. return nil, errors.NewLocalizedError(errServerFailure, response.StatusCode)
  33. }
  34. if response.StatusCode != 304 {
  35. // Content-Length = -1 when no Content-Length header is sent.
  36. if response.ContentLength == 0 {
  37. return nil, errors.NewLocalizedError(errEmptyFeed)
  38. }
  39. if err := response.EnsureUnicodeBody(); err != nil {
  40. return nil, errors.NewLocalizedError(errEncoding, err)
  41. }
  42. }
  43. return response, nil
  44. }