Home > OS >  Can I throw a HttpResponseException from a console app?
Can I throw a HttpResponseException from a console app?

Time:08-27

I have a console app which receives a rest response, when that response is a 401 or 400 can/should I throw the HttpResponseException here, even though that requires installing the Asp.Net.WebApi.Core package into the project?

Or is there a better alternative? Not sure if it's okay to mix things from web api into a console app

CodePudding user response:

Without having a greater picture of your use case I would consider creating a custom exception that contains the data you might need for proper error handling. I can imagine having HttpStatusCode property for the status code in the response and a string property that would contain the body of the response. Maybe also some dictionary to save the headers you would find needed. See below for an example.

Note that it is a good practise to make custom exceptions serializable, i.e. add the [Serializable] attribute, implement the protected constructor and the override of the GetObjectData method. I tried to add some XML documentation to the example code to explain.

You may find quite a few articles that describe good practices when designing custom exception, e.g. here.

/// <summary>
/// Custom exception that describes a problem indicated by a remote web API
/// </summary>
[Serializable]
public class RemoteApiException : Exception
{
    /// <summary>
    /// Gets the HTTP status code provided in the response
    /// </summary>
    public HttpStatusCode StatusCode { get; private set; }

    /// <summary>
    /// Gets the response body content data as string
    /// </summary>
    public string? Body { get; private set; }

    /// <summary>
    /// Creates and initializes a new exception that describes a problem indicated by a remote web API
    /// </summary>
    /// <param name="message">The message associated with the exception</param>
    /// <param name="statusCode">The status code in the HTTP response from the remote web API</param>
    /// <param name="body">The content of the response body</param>
    public RemoteApiException(string message, HttpStatusCode statusCode, string? body)
        : base(message)
    {
        StatusCode = statusCode;
        Body = body;
    }

    #region Serialization related members

    /// <summary>
    /// Constructor that is used by the serializer when deserializing the exception
    /// </summary>
    /// <param name="info">The information associated with the serialization process</param>
    /// <param name="context">The context of the serialization</param>
    protected RemoteApiException(SerializationInfo info, StreamingContext context)
        : base(info, context)
    {
        StatusCode = (HttpStatusCode)info.GetInt32(nameof(StatusCode));
        Body = info.GetString(nameof(Body));
    }

    /// <summary>
    /// Enriches the serialization info with the custom property values when serializing the exception
    /// </summary>
    /// <param name="info">The serialization information data</param>
    /// <param name="context">The context of the serialization</param>
    public override void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        base.GetObjectData(info, context);
        info.AddValue(nameof(StatusCode), (int)StatusCode, typeof(int));
        if (!string.IsNullOrEmpty(Body))
        {
            info.AddValue(nameof(Body), Body, typeof(string));
        }
    }

    #endregion
}
  • Related