From 2f6b0c77ea0932691169c20ff12087ee9f9a52e9 Mon Sep 17 00:00:00 2001 From: Qstick Date: Sun, 18 Nov 2018 22:27:16 -0500 Subject: [PATCH] Fixed: Updated error message if services respond with html Co-Authored-By: taloth --- src/NzbDrone.Common/Http/HttpClient.cs | 10 ++++++++++ src/NzbDrone.Common/Http/HttpException.cs | 12 +++++++++--- .../Http/UnexpectedHtmlContentException.cs | 13 +++++++++++++ src/NzbDrone.Common/NzbDrone.Common.csproj | 1 + 4 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 src/NzbDrone.Common/Http/UnexpectedHtmlContentException.cs diff --git a/src/NzbDrone.Common/Http/HttpClient.cs b/src/NzbDrone.Common/Http/HttpClient.cs index 45ff53ae3..58896af48 100644 --- a/src/NzbDrone.Common/Http/HttpClient.cs +++ b/src/NzbDrone.Common/Http/HttpClient.cs @@ -267,6 +267,7 @@ namespace NzbDrone.Common.Http public HttpResponse Get(HttpRequest request) where T : new() { var response = Get(request); + CheckResponseContentType(response); return new HttpResponse(response); } @@ -285,7 +286,16 @@ namespace NzbDrone.Common.Http public HttpResponse Post(HttpRequest request) where T : new() { var response = Post(request); + CheckResponseContentType(response); return new HttpResponse(response); } + + private void CheckResponseContentType(HttpResponse response) + { + if (response.Headers.ContentType != null && response.Headers.ContentType.Contains("text/html")) + { + throw new UnexpectedHtmlContentException(response); + } + } } } diff --git a/src/NzbDrone.Common/Http/HttpException.cs b/src/NzbDrone.Common/Http/HttpException.cs index 759a104c1..005fab57a 100644 --- a/src/NzbDrone.Common/Http/HttpException.cs +++ b/src/NzbDrone.Common/Http/HttpException.cs @@ -7,13 +7,19 @@ namespace NzbDrone.Common.Http public HttpRequest Request { get; private set; } public HttpResponse Response { get; private set; } - public HttpException(HttpRequest request, HttpResponse response) - : base(string.Format("HTTP request failed: [{0}:{1}] [{2}] at [{3}]", (int)response.StatusCode, response.StatusCode, request.Method, request.Url)) + public HttpException(HttpRequest request, HttpResponse response, string message) + : base(message) { Request = request; Response = response; } + public HttpException(HttpRequest request, HttpResponse response) + : this(request, response, string.Format("HTTP request failed: [{0}:{1}] [{2}] at [{3}]", (int)response.StatusCode, response.StatusCode, request.Method, request.Url)) + { + + } + public HttpException(HttpResponse response) : this(response.Request, response) { @@ -30,4 +36,4 @@ namespace NzbDrone.Common.Http return base.ToString(); } } -} \ No newline at end of file +} diff --git a/src/NzbDrone.Common/Http/UnexpectedHtmlContentException.cs b/src/NzbDrone.Common/Http/UnexpectedHtmlContentException.cs new file mode 100644 index 000000000..fedf94761 --- /dev/null +++ b/src/NzbDrone.Common/Http/UnexpectedHtmlContentException.cs @@ -0,0 +1,13 @@ +using System; + +namespace NzbDrone.Common.Http +{ + public class UnexpectedHtmlContentException : HttpException + { + public UnexpectedHtmlContentException(HttpResponse response) + : base(response.Request, response, $"Site responded with browser content instead of api data. This disruption may be temporary, please try again later. [{response.Request.Url}]") + { + + } + } +} diff --git a/src/NzbDrone.Common/NzbDrone.Common.csproj b/src/NzbDrone.Common/NzbDrone.Common.csproj index bd08eacb1..6764dc70e 100644 --- a/src/NzbDrone.Common/NzbDrone.Common.csproj +++ b/src/NzbDrone.Common/NzbDrone.Common.csproj @@ -182,6 +182,7 @@ +