aboutsummaryrefslogtreecommitdiffhomepage
path: root/reader/browser
diff options
context:
space:
mode:
authorGravatar Frédéric Guillot <fred@miniflux.net>2018-10-14 21:43:48 -0700
committerGravatar Frédéric Guillot <fred@miniflux.net>2018-10-14 21:43:48 -0700
commit778346b0b04bc52c89529668b37c1086bebe1674 (patch)
tree957b4d049210af1753bfa6690a2810f6634193ad /reader/browser
parent5870f0426002c8e26a9ff472b23e15d7bf1235f7 (diff)
Simplify feed fetcher
- Add browser package to handle HTTP errors - Reduce code duplication
Diffstat (limited to 'reader/browser')
-rw-r--r--reader/browser/browser.go55
1 files changed, 55 insertions, 0 deletions
diff --git a/reader/browser/browser.go b/reader/browser/browser.go
new file mode 100644
index 0000000..733d5f5
--- /dev/null
+++ b/reader/browser/browser.go
@@ -0,0 +1,55 @@
+// Copyright 2018 Frédéric Guillot. All rights reserved.
+// Use of this source code is governed by the Apache 2.0
+// license that can be found in the LICENSE file.
+
+package browser // import "miniflux.app/reader/browser"
+
+import (
+ "miniflux.app/errors"
+ "miniflux.app/http/client"
+)
+
+var (
+ errRequestFailed = "Unable to open this link: %v"
+ errServerFailure = "Unable to fetch this resource (Status Code = %d)"
+ errEncoding = "Unable to normalize encoding: %q"
+ errEmptyFeed = "This feed is empty"
+ errResourceNotFound = "Resource not found (404), this feed doesn't exists anymore, check the feed URL"
+ errNotAuthorized = "You are not authorized to access this resource (invalid username/password)"
+)
+
+// Exec executes a HTTP request and handles errors.
+func Exec(request *client.Client) (*client.Response, *errors.LocalizedError) {
+ response, err := request.Get()
+ if err != nil {
+ if e, ok := err.(*errors.LocalizedError); ok {
+ return nil, e
+ }
+ return nil, errors.NewLocalizedError(errRequestFailed, err)
+ }
+
+ if response.IsNotFound() {
+ return nil, errors.NewLocalizedError(errResourceNotFound)
+ }
+
+ if response.IsNotAuthorized() {
+ return nil, errors.NewLocalizedError(errNotAuthorized)
+ }
+
+ if response.HasServerFailure() {
+ return nil, errors.NewLocalizedError(errServerFailure, response.StatusCode)
+ }
+
+ if response.StatusCode != 304 {
+ // Content-Length = -1 when no Content-Length header is sent.
+ if response.ContentLength == 0 {
+ return nil, errors.NewLocalizedError(errEmptyFeed)
+ }
+
+ if err := response.EnsureUnicodeBody(); err != nil {
+ return nil, errors.NewLocalizedError(errEncoding, err)
+ }
+ }
+
+ return response, nil
+}