Skip to content

Commit

Permalink
Merge pull request #51 from mark-monteiro/improve-error-logging
Browse files Browse the repository at this point in the history
  • Loading branch information
crobibero authored Aug 30, 2021
2 parents 0ac8317 + d56a9b8 commit 8c34998
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 42 deletions.
10 changes: 3 additions & 7 deletions Jellyfin.Plugin.Webhook/Destinations/Discord/DiscordClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Net.Mime;
using System.Text;
using System.Threading.Tasks;
using Jellyfin.Plugin.Webhook.Extensions;
using MediaBrowser.Common.Net;
using Microsoft.Extensions.Logging;

Expand Down Expand Up @@ -72,12 +73,7 @@ public async Task SendAsync(DiscordOption options, Dictionary<string, object> da
.CreateClient(NamedClient.Default)
.PostAsync(new Uri(options.WebhookUri), content)
.ConfigureAwait(false);
if (!response.IsSuccessStatusCode)
{
var responseStr = await response.Content.ReadAsStringAsync()
.ConfigureAwait(false);
_logger.LogWarning("Error sending notification: {Response}", responseStr);
}
await response.LogIfFailedAsync(_logger).ConfigureAwait(false);
}
catch (HttpRequestException e)
{
Expand All @@ -100,4 +96,4 @@ private static string GetMentionType(DiscordMentionType mentionType)
};
}
}
}
}
10 changes: 3 additions & 7 deletions Jellyfin.Plugin.Webhook/Destinations/Generic/GenericClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Net.Mime;
using System.Text;
using System.Threading.Tasks;
using Jellyfin.Plugin.Webhook.Extensions;
using MediaBrowser.Common.Net;
using Microsoft.Extensions.Logging;
using Microsoft.Net.Http.Headers;
Expand Down Expand Up @@ -83,17 +84,12 @@ public async Task SendAsync(GenericOption options, Dictionary<string, object> da
.CreateClient(NamedClient.Default)
.SendAsync(httpRequestMessage)
.ConfigureAwait(false);
if (!response.IsSuccessStatusCode)
{
var responseStr = await response.Content.ReadAsStringAsync()
.ConfigureAwait(false);
_logger.LogWarning("Error sending notification: {Response}", responseStr);
}
await response.LogIfFailedAsync(_logger).ConfigureAwait(false);
}
catch (HttpRequestException e)
{
_logger.LogWarning(e, "Error sending notification");
}
}
}
}
}
10 changes: 3 additions & 7 deletions Jellyfin.Plugin.Webhook/Destinations/Gotify/GotifyClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Net.Mime;
using System.Text;
using System.Threading.Tasks;
using Jellyfin.Plugin.Webhook.Extensions;
using MediaBrowser.Common.Net;
using Microsoft.Extensions.Logging;

Expand Down Expand Up @@ -57,17 +58,12 @@ public async Task SendAsync(GotifyOption options, Dictionary<string, object> dat
.CreateClient(NamedClient.Default)
.PostAsync(new Uri(options.WebhookUri.TrimEnd() + $"/message?token={options.Token}"), content)
.ConfigureAwait(false);
if (!response.IsSuccessStatusCode)
{
var responseStr = await response.Content.ReadAsStringAsync()
.ConfigureAwait(false);
_logger.LogWarning("Error sending notification: {Response}", responseStr);
}
await response.LogIfFailedAsync(_logger).ConfigureAwait(false);
}
catch (HttpRequestException e)
{
_logger.LogWarning(e, "Error sending notification");
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Net.Mime;
using System.Text;
using System.Threading.Tasks;
using Jellyfin.Plugin.Webhook.Extensions;
using MediaBrowser.Common.Net;
using Microsoft.Extensions.Logging;

Expand Down Expand Up @@ -59,17 +60,12 @@ public async Task SendAsync(PushbulletOption options, Dictionary<string, object>
.CreateClient(NamedClient.Default)
.SendAsync(requestOptions)
.ConfigureAwait(false);
if (!response.IsSuccessStatusCode)
{
var responseStr = await response.Content.ReadAsStringAsync()
.ConfigureAwait(false);
_logger.LogWarning("Error sending notification: {Response}", responseStr);
}
await response.LogIfFailedAsync(_logger).ConfigureAwait(false);
}
catch (HttpRequestException e)
{
_logger.LogWarning(e, "Error sending notification");
}
}
}
}
}
10 changes: 3 additions & 7 deletions Jellyfin.Plugin.Webhook/Destinations/Pushover/PushoverClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Net.Mime;
using System.Text;
using System.Threading.Tasks;
using Jellyfin.Plugin.Webhook.Extensions;
using MediaBrowser.Common.Net;
using Microsoft.Extensions.Logging;

Expand Down Expand Up @@ -81,17 +82,12 @@ public async Task SendAsync(PushoverOption options, Dictionary<string, object> d
.CreateClient(NamedClient.Default)
.PostAsync(string.IsNullOrEmpty(options.WebhookUri) ? PushoverOption.ApiUrl : new Uri(options.WebhookUri), content)
.ConfigureAwait(false);
if (!response.IsSuccessStatusCode)
{
var responseStr = await response.Content.ReadAsStringAsync()
.ConfigureAwait(false);
_logger.LogWarning("Error sending notification: {Response}", responseStr);
}
await response.LogIfFailedAsync(_logger).ConfigureAwait(false);
}
catch (HttpRequestException e)
{
_logger.LogWarning(e, "Error sending notification");
}
}
}
}
}
10 changes: 3 additions & 7 deletions Jellyfin.Plugin.Webhook/Destinations/Slack/SlackClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Net.Mime;
using System.Text;
using System.Threading.Tasks;
using Jellyfin.Plugin.Webhook.Extensions;
using MediaBrowser.Common.Net;
using Microsoft.Extensions.Logging;

Expand Down Expand Up @@ -57,17 +58,12 @@ public async Task SendAsync(SlackOption options, Dictionary<string, object> data
.CreateClient(NamedClient.Default)
.PostAsync(new Uri(options.WebhookUri), content)
.ConfigureAwait(false);
if (!response.IsSuccessStatusCode)
{
var responseStr = await response.Content.ReadAsStringAsync()
.ConfigureAwait(false);
_logger.LogWarning("Error sending notification: {Response}", responseStr);
}
await response.LogIfFailedAsync(_logger).ConfigureAwait(false);
}
catch (HttpRequestException e)
{
_logger.LogWarning(e, "Error sending notification");
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;

namespace Jellyfin.Plugin.Webhook.Extensions
{
/// <summary>
/// Extension methods for <see cref="HttpResponseMessage"/>.
/// </summary>
public static class HttpResponseMessageExtensions
{
/// <summary>
/// Log a warning message if the <paramref name="response"/> contains an error status code.
/// </summary>
/// <param name="response">The HTTP response to log if failed.</param>
/// <param name="logger">The logger to use to log the warning.</param>
/// <returns>A task representing the async operation.</returns>
public static async Task LogIfFailedAsync(this HttpResponseMessage response, ILogger logger)
{
// Don't log anything for successful responses
if (response.IsSuccessStatusCode)
{
return;
}

// Log the request that caused the failed response, if available
var request = response.RequestMessage;
if (request is not null)
{
var requestStr = request.Content is not null
? await request.Content.ReadAsStringAsync().ConfigureAwait(false)
: "<empty request body>";
logger.LogWarning("Notification failed with {Method} request to {Url}: {Content}", request.Method, request.RequestUri, requestStr);
}

// Log the response
var responseStr = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
logger.LogWarning("Notification failed with response status code {StatusCode}: {Content}", response.StatusCode, responseStr);
}
}
}

0 comments on commit 8c34998

Please sign in to comment.