-
Notifications
You must be signed in to change notification settings - Fork 0
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 #10 from Zastai/more-http-utils
More HTTP utilities
- Loading branch information
Showing
2 changed files
with
68 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using System.Net; | ||
using System.Net.Http; | ||
|
||
using JetBrains.Annotations; | ||
|
||
namespace MetaBrainz.Common; | ||
|
||
/// <summary>An error reported by an HTTP response.</summary> | ||
[PublicAPI] | ||
public class HttpError : Exception { | ||
|
||
/// <summary>Creates a new HTTP error.</summary> | ||
/// <param name="response">The response to take the status code and reason from.</param> | ||
public HttpError(HttpResponseMessage response) : this(response.StatusCode, response.ReasonPhrase) { } | ||
|
||
/// <summary>Creates a new HTTP error.</summary> | ||
/// <param name="status">The status code for the error.</param> | ||
/// <param name="reason">The reason phrase associated with the error.</param> | ||
/// <param name="cause">The exception that caused this one, if any.</param> | ||
public HttpError(HttpStatusCode status, string? reason, Exception? cause = null) : base(null, cause) { | ||
this.Reason = reason; | ||
this.Status = status; | ||
} | ||
|
||
/// <summary>Gets a textual representation of the HTTP error.</summary> | ||
/// <returns>A string of the form <c>HTTP nnn/StatusName 'REASON'</c>.</returns> | ||
public override string Message | ||
=> this.Reason is null ? $"HTTP {(int) this.Status}/{this.Status}" : $"HTTP {(int) this.Status}/{this.Status} '{this.Reason}'"; | ||
|
||
/// <summary>The reason phrase associated with the error.</summary> | ||
public string? Reason { get; } | ||
|
||
/// <summary>The status code for the error.</summary> | ||
public HttpStatusCode Status { get; } | ||
|
||
} |
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