title: How do I set the ServiceStack ResponseStatus StatusCode?
body: <p>I have developed a custom exception which I throw from my ServiceStack service. The status code and description are mapped correctly, but the inner 'statusCode' value always appears to be '0'.</p>
  
  <p>Here is how I have implemented my exception:</p>
  
  <pre><code>public class TestException : Exception, IHasStatusCode, IHasStatusDescription, IResponseStatusConvertible
  {
      private readonly int m_InternalErrorCode;
      private readonly string m_ArgumentName;
      private readonly string m_DetailedError;
  
  
      public int StatusCode =&gt; 422;
      public string StatusDescription =&gt; Message;
  
      public TestException(int internalErrorCode, string argumentName, string detailedError)
          : base("The request was semantically incorrect or was incomplete.")
      {
          m_InternalErrorCode = internalErrorCode;
          m_ArgumentName = argumentName;
          m_DetailedError = detailedError;
      }
  
      public ResponseStatus ToResponseStatus()
      {
          return new ResponseStatus
          {
              ErrorCode = StatusCode.ToString(),
              Message = StatusDescription,
              Errors = new List&lt;ResponseError&gt;
              {
                  new ResponseError
                  {
                      ErrorCode = m_InternalErrorCode.ToString(),
                      FieldName = m_ArgumentName,
                      Message = m_DetailedError
                  }
              }
          };
      }
  }
  </code></pre>
  
  <p>When I throw my exception from my ServiceStack service<br>
       <code>throw new TestException(123, "Thing in error", "Detailed error message");</code><br>
  I get a HTTP status code of 422 with a corresponding description (reason/phrase) set as expected when I view the response in my client (browser/postman etc), but the content (when I specify ContentType=application/json in the header) looks like this...</p>
  
  <pre><code>{
    "statusCode": 0,
    "responseStatus": {
      "errorCode": "422",
      "message": "The request was semantically incorrect or was incomplete.",
      "stackTrace": "StackTrace ommitted for berivity",
      "errors": [
        {
          "errorCode": "123",
          "fieldName": "Thing in error",
          "message": "Detailed error message"
        }
      ]
    }
  }
  </code></pre>
  
  <p>As you can see in the json response above, the status code is '0'. My question is - how do I set this value? I am guessing that it should be the same as that of the the HTTP response (422 in the example above).</p>
  
  <p><strong>Update: Thanks to Mythz for pointing me to the answer</strong>
  I updated my response base class like this:  </p>
  
  <pre><code>public abstract class ResponseBase : IHasResponseStatus, IHasStatusCode
  {
      private int m_StatusCode;
  
      public int StatusCode
      {
          get
          {
              if (m_StatusCode == 0)
              {
                  if (ResponseStatus != null)
                  {
                      if (int.TryParse(ResponseStatus.ErrorCode, out int code))
                          return code;
                  }
              }
              return m_StatusCode;
          }
          set
          {
              m_StatusCode = value;
          }
      }
  
      public ResponseStatus ResponseStatus { get; set; }
  }
  </code></pre>