From 9c0f882ba0c7f8635ff7ab8007d134f7aefd69f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Guillot?= Date: Sat, 30 Jun 2018 12:42:12 -0700 Subject: [PATCH] Add specific 404 and 401 error messages --- http/client/response.go | 10 ++++++++++ locale/translations.go | 27 +++++++++++++++------------ locale/translations/de_DE.json | 2 +- locale/translations/fr_FR.json | 7 +++++-- locale/translations/nl_NL.json | 2 +- locale/translations/pl_PL.json | 2 +- locale/translations/zh_CN.json | 2 +- reader/feed/handler.go | 11 ++++++++++- reader/subscription/finder.go | 10 ++++++++++ 9 files changed, 54 insertions(+), 19 deletions(-) diff --git a/http/client/response.go b/http/client/response.go index f033d610..d4e3cdca 100644 --- a/http/client/response.go +++ b/http/client/response.go @@ -24,6 +24,16 @@ type Response struct { ContentLength int64 } +// IsNotFound returns true if the resource doesn't exists anymore. +func (r *Response) IsNotFound() bool { + return r.StatusCode == 404 || r.StatusCode == 410 +} + +// IsNotAuthorized returns true if the resource require authentication. +func (r *Response) IsNotAuthorized() bool { + return r.StatusCode == 401 +} + // HasServerFailure returns true if the status code represents a failure. func (r *Response) HasServerFailure() bool { return r.StatusCode >= 400 diff --git a/locale/translations.go b/locale/translations.go index e1e44abe..92e036dd 100755 --- a/locale/translations.go +++ b/locale/translations.go @@ -1,5 +1,5 @@ // Code generated by go generate; DO NOT EDIT. -// 2018-06-20 09:43:57.979429995 +0200 CEST m=+0.030917486 +// 2018-06-30 12:36:10.057382143 -0700 PDT m=+0.028630754 package locale @@ -91,7 +91,7 @@ var translations = map[string]string{ "Yes": "Ja", "No": "Nein", "This feed already exists (%s)": "Diese Abonnement existiert bereits (%s)", - "Unable to fetch feed (statusCode=%d)": "Abonnement konnte nicht abgerufen werden (code=%d)", + "Unable to fetch feed (Status Code = %d)": "Abonnement konnte nicht abgerufen werden (code=%d)", "Unable to open this link: %v": "Dieser Link konnte nicht geöffnet werden: %v", "Unable to analyze this page: %v": "Diese Seite konnte nicht analysiert werden: %v", "Unable to find any subscription.": "Es wurden keine Abonnements gefunden.", @@ -338,7 +338,7 @@ var translations = map[string]string{ "Yes": "Oui", "No": "Non", "This feed already exists (%s)": "Cet abonnement existe déjà (%s)", - "Unable to fetch feed (statusCode=%d)": "Impossible de récupérer cet abonnement (code=%d)", + "Unable to fetch feed (Status Code = %d)": "Impossible de récupérer cet abonnement (code=%d)", "Unable to open this link: %v": "Impossible d'ouvrir ce lien : %v", "Unable to analyze this page: %v": "Impossible d'analyzer cette page : %v", "Unable to find any subscription.": "Impossible de trouver un abonnement.", @@ -485,7 +485,10 @@ var translations = map[string]string{ "Unable to fetch request token from Pocket!": "Impossible de récupérer le jeton d'accès depuis Pocket !", "Advanced Options": "Options avancées", "Feed Username": "Nom d'utilisateur du flux", - "Feed Password": "Mot de passe du flux" + "Feed Password": "Mot de passe du flux", + "You are not authorized to access this resource (invalid username/password)": "Vous n'êtes pas autorisé à accéder à cette ressource (nom d'utilisateur / mot de passe incorrect)", + "Unable to fetch this resource (Status Code = %d)": "Impossible de récupérer cette ressource (code=%d)", + "Resource not found (404), this feed doesn't exists anymore, check the feed URL": "Page introuvable (404), cet abonnement n'existe plus, vérifiez l'adresse du flux" } `, "nl_NL": `{ @@ -574,7 +577,7 @@ var translations = map[string]string{ "Yes": "Ja", "No": "Nee", "This feed already exists (%s)": "Deze feed bestaat al (%s)", - "Unable to fetch feed (statusCode=%d)": "Kon feed niet update (code=%d)", + "Unable to fetch feed (Status Code = %d)": "Kon feed niet update (code=%d)", "Unable to open this link: %v": "Kon link niet volgen: %v", "Unable to analyze this page: %v": "Kon pagina niet analyseren: %v", "Unable to find any subscription.": "Kon geen feeds vinden.", @@ -798,7 +801,7 @@ var translations = map[string]string{ "Yes": "Tak", "No": "Nie", "This feed already exists (%s)": "Ten kanał już istnieje (%s)", - "Unable to fetch feed (statusCode=%d)": "Kanał nie mógł zostać pobrany (kod=%d)", + "Unable to fetch feed (Status Code = %d)": "Kanał nie mógł zostać pobrany (kod=%d)", "Unable to open this link: %v": "Nie można było otworzyć tego linku: %v", "Unable to analyze this page: %v": "Nie można przeanalizować tej strony: %v", "Unable to find any subscription.": "Nie znaleziono żadnych subskrypcji.", @@ -1020,7 +1023,7 @@ var translations = map[string]string{ "Yes": "是", "No": "否", "This feed already exists (%s)": "源已存在 (%s)", - "Unable to fetch feed (statusCode=%d)": "无法获取源 (错误代码=%d)", + "Unable to fetch feed (Status Code = %d)": "无法获取源 (错误代码=%d)", "Unable to open this link: %v": "无法打开这一链接: %v", "Unable to analyze this page: %v": "无法分析这一页面: %v", "Unable to find any subscription.": "找不到任何订阅.", @@ -1158,10 +1161,10 @@ var translations = map[string]string{ } var translationsChecksums = map[string]string{ - "de_DE": "120f472a466191939ea8d72c469f9703720cec24a28a3f79d400d5501820a8ce", + "de_DE": "bbc822f9acd0033eca10ac025229d375c9509977d5ba47afacbfb5b597bbc24c", "en_US": "6fe95384260941e8a5a3c695a655a932e0a8a6a572c1e45cb2b1ae8baa01b897", - "fr_FR": "734b9ee6edc65e5c076fc91999d81f6c586eeb52f420101049bff9b501cbae54", - "nl_NL": "1a73f1dd1c4c0d2c2adc8695cdd050c2dad81c14876caed3892b44adc2491265", - "pl_PL": "da709c14ff71f3b516eec66cb2758d89c5feab1472c94b2b518f425162a9f806", - "zh_CN": "d80594c1b67d15e9f4673d3d62fe4949e8606a5fdfb741d8a8921f21dceb8cf2", + "fr_FR": "7a451a1d09e051a847f554937e07a6af24b92f1da7f46da8c0ef2d4cc31bcec6", + "nl_NL": "bec647fdfb325a30050c50bdb9a29fee58b0705109d32c09b5ace77aa0777e7c", + "pl_PL": "2295f35a98c8f60cfc6bab241d26b224c06979cc9ca3740bb89c63c7596a0431", + "zh_CN": "f5fb0a9b7336c51e74d727a2fb294bab3514e3002376da7fd904e0d7caed1a1c", } diff --git a/locale/translations/de_DE.json b/locale/translations/de_DE.json index c6716280..51173f84 100644 --- a/locale/translations/de_DE.json +++ b/locale/translations/de_DE.json @@ -85,7 +85,7 @@ "Yes": "Ja", "No": "Nein", "This feed already exists (%s)": "Diese Abonnement existiert bereits (%s)", - "Unable to fetch feed (statusCode=%d)": "Abonnement konnte nicht abgerufen werden (code=%d)", + "Unable to fetch feed (Status Code = %d)": "Abonnement konnte nicht abgerufen werden (code=%d)", "Unable to open this link: %v": "Dieser Link konnte nicht geöffnet werden: %v", "Unable to analyze this page: %v": "Diese Seite konnte nicht analysiert werden: %v", "Unable to find any subscription.": "Es wurden keine Abonnements gefunden.", diff --git a/locale/translations/fr_FR.json b/locale/translations/fr_FR.json index 4c830b4a..5270b910 100644 --- a/locale/translations/fr_FR.json +++ b/locale/translations/fr_FR.json @@ -85,7 +85,7 @@ "Yes": "Oui", "No": "Non", "This feed already exists (%s)": "Cet abonnement existe déjà (%s)", - "Unable to fetch feed (statusCode=%d)": "Impossible de récupérer cet abonnement (code=%d)", + "Unable to fetch feed (Status Code = %d)": "Impossible de récupérer cet abonnement (code=%d)", "Unable to open this link: %v": "Impossible d'ouvrir ce lien : %v", "Unable to analyze this page: %v": "Impossible d'analyzer cette page : %v", "Unable to find any subscription.": "Impossible de trouver un abonnement.", @@ -232,5 +232,8 @@ "Unable to fetch request token from Pocket!": "Impossible de récupérer le jeton d'accès depuis Pocket !", "Advanced Options": "Options avancées", "Feed Username": "Nom d'utilisateur du flux", - "Feed Password": "Mot de passe du flux" + "Feed Password": "Mot de passe du flux", + "You are not authorized to access this resource (invalid username/password)": "Vous n'êtes pas autorisé à accéder à cette ressource (nom d'utilisateur / mot de passe incorrect)", + "Unable to fetch this resource (Status Code = %d)": "Impossible de récupérer cette ressource (code=%d)", + "Resource not found (404), this feed doesn't exists anymore, check the feed URL": "Page introuvable (404), cet abonnement n'existe plus, vérifiez l'adresse du flux" } diff --git a/locale/translations/nl_NL.json b/locale/translations/nl_NL.json index 8580fadb..2edd40d5 100644 --- a/locale/translations/nl_NL.json +++ b/locale/translations/nl_NL.json @@ -84,7 +84,7 @@ "Yes": "Ja", "No": "Nee", "This feed already exists (%s)": "Deze feed bestaat al (%s)", - "Unable to fetch feed (statusCode=%d)": "Kon feed niet update (code=%d)", + "Unable to fetch feed (Status Code = %d)": "Kon feed niet update (code=%d)", "Unable to open this link: %v": "Kon link niet volgen: %v", "Unable to analyze this page: %v": "Kon pagina niet analyseren: %v", "Unable to find any subscription.": "Kon geen feeds vinden.", diff --git a/locale/translations/pl_PL.json b/locale/translations/pl_PL.json index 2c6019db..b95a6544 100755 --- a/locale/translations/pl_PL.json +++ b/locale/translations/pl_PL.json @@ -87,7 +87,7 @@ "Yes": "Tak", "No": "Nie", "This feed already exists (%s)": "Ten kanał już istnieje (%s)", - "Unable to fetch feed (statusCode=%d)": "Kanał nie mógł zostać pobrany (kod=%d)", + "Unable to fetch feed (Status Code = %d)": "Kanał nie mógł zostać pobrany (kod=%d)", "Unable to open this link: %v": "Nie można było otworzyć tego linku: %v", "Unable to analyze this page: %v": "Nie można przeanalizować tej strony: %v", "Unable to find any subscription.": "Nie znaleziono żadnych subskrypcji.", diff --git a/locale/translations/zh_CN.json b/locale/translations/zh_CN.json index ccd5a6c1..e003ebbd 100644 --- a/locale/translations/zh_CN.json +++ b/locale/translations/zh_CN.json @@ -85,7 +85,7 @@ "Yes": "是", "No": "否", "This feed already exists (%s)": "源已存在 (%s)", - "Unable to fetch feed (statusCode=%d)": "无法获取源 (错误代码=%d)", + "Unable to fetch feed (Status Code = %d)": "无法获取源 (错误代码=%d)", "Unable to open this link: %v": "无法打开这一链接: %v", "Unable to analyze this page: %v": "无法分析这一页面: %v", "Unable to find any subscription.": "找不到任何订阅.", diff --git a/reader/feed/handler.go b/reader/feed/handler.go index 24fdadfa..f01b8772 100644 --- a/reader/feed/handler.go +++ b/reader/feed/handler.go @@ -21,12 +21,13 @@ import ( var ( errRequestFailed = "Unable to execute request: %v" - errServerFailure = "Unable to fetch feed (statusCode=%d)" + errServerFailure = "Unable to fetch feed (Status Code = %d)" errDuplicate = "This feed already exists (%s)" errNotFound = "Feed %d not found" errEncoding = "Unable to normalize encoding: %q" errCategoryNotFound = "Category not found for this user" errEmptyFeed = "This feed is empty" + errResourceNotFound = "Resource not found (404), this feed doesn't exists anymore, check the feed URL" ) // Handler contains all the logic to create and refresh feeds. @@ -152,6 +153,14 @@ func (h *Handler) RefreshFeed(userID, feedID int64) error { originalFeed.CheckedAt = time.Now() + if response.IsNotFound() { + err := errors.NewLocalizedError(errResourceNotFound) + originalFeed.ParsingErrorCount++ + originalFeed.ParsingErrorMsg = err.Localize(currentLanguage) + h.store.UpdateFeed(originalFeed) + return err + } + if response.HasServerFailure() { err := errors.NewLocalizedError(errServerFailure, response.StatusCode) originalFeed.ParsingErrorCount++ diff --git a/reader/subscription/finder.go b/reader/subscription/finder.go index 6b45cfbb..57fbd118 100644 --- a/reader/subscription/finder.go +++ b/reader/subscription/finder.go @@ -24,6 +24,8 @@ var ( errConnectionFailure = "Unable to open this link: %v" errUnreadableDoc = "Unable to analyze this page: %v" errEmptyBody = "This web page is empty" + errNotAuthorized = "You are not authorized to access this resource (invalid username/password)" + errServerFailure = "Unable to fetch this resource (Status Code = %d)" ) // FindSubscriptions downloads and try to find one or more subscriptions from an URL. @@ -40,6 +42,14 @@ func FindSubscriptions(websiteURL, username, password string) (Subscriptions, er return nil, errors.NewLocalizedError(errConnectionFailure, err) } + if response.IsNotAuthorized() { + return nil, errors.NewLocalizedError(errNotAuthorized) + } + + if response.HasServerFailure() { + return nil, errors.NewLocalizedError(errServerFailure, response.StatusCode) + } + // Content-Length = -1 when no Content-Length header is sent if response.ContentLength == 0 { return nil, errors.NewLocalizedError(errEmptyBody)