-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from mark-monteiro/improve-error-logging
- Loading branch information
Showing
7 changed files
with
59 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
Jellyfin.Plugin.Webhook/Extensions/HttpResponseMessageExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |