browser.go 1.6 KB

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