0

I'm trying to implement my own custom exceptions in dot net core.

This is what I have so far:

public class WSException: Exception
{
    // some custom stuff...
    private readonly string _developerMessage = "";
    public string DeveloperMessage { get { return _developerMessage; } }

    public WSException() {}

    public WSException(string message) : base(message) {
        this._developerMessage = message;
    }

    public WSException(string message, Exception inner) : base(message, inner) {
        this._developerMessage = message;
    }

    public WSException(Exception ex) : base(ex.Message, ex.InnerException) {
        _developerMessage = ex.Message;
        Source = ex.Source;
        //StackTrace = ex.StackTrace;  // cannot be assigned to, it's read only
    }

    public WSException(string message) : base(message) {
        this._developerMessage = (String.IsNullOrWhiteSpace(developerMessage) ? message : developerMessage);
    }
}

When I catch a general exception, I try to create one of my own (a WSException) to handle it in a common way, like this:

try {
    // whatever 
}
catch (WSException e) {
    HandleException(e);
}
catch (Exception e) {
    HandleException(new WSException(e));
}

When I do it like that, e.Source and e.StackTrace are null, and when I try to assign StackTrace I get a Propery or indexer 'Exception.StackTrace' cannot be assigned to --it is read only.

How should such I implement this constructor?

public WSException(Exception ex) : base(ex.Message, ex.InnerException) {
    _developerMessage = ex.Message;
    Source = ex.Source;
    //StackTrace = ex.StackTrace;  // cannot be assigned to, it's read only
}
opensas
  • 60,462
  • 79
  • 252
  • 386
  • You could keep the original exception as the inner exception (looks for an Exception constructor that takes InnerException as argument). Q: What purpose does your custom WSException type have? – 500 - Internal Server Error May 22 '18 at 14:48
  • My WSException is just a base exception to provide a common structure for all my exceptions, and to ultimately convert it to json to return it from a rest web service – opensas May 24 '18 at 01:31

1 Answers1

0

The workaround I found so far is to handle it when I'm serializing the error to json, something like this:

public class WSExceptionJsonConverter : JsonConverter
{
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        var ex = value as WSException;
        writer.WriteStartObject();

        // buch of properties[...]

        string stackTrace = null;
        if (ex.StackTrace != null) {
            stackTrace = ex.StackTrace;
        } else if (ex.InnerException != null && ex.InnerException.StackTrace != null) {
            stackTrace = ex.InnerException.StackTrace;
        } else {
            stackTrace = null;
        }
        writer.WritePropertyName("stacktrace");
        serializer.Serialize(writer, stackTrace.Split('\n'));

        writer.WriteEndObject();
    }

But it feels too hacky

opensas
  • 60,462
  • 79
  • 252
  • 386