COLA
COLA
Explore posts from servers
CC#
Created by COLA on 11/19/2023 in #help
Issues trying to create new error types
Hello everyone. I'm trying to create my own extensible error type, based on the RFC 7807.
c#
public class RFC8807Error: Exception
{
public required virtual string type { get; init; }
public required virtual string title { get; init; }
public virtual int status { get; init; } = 400;
public required virtual string detail { get; init; }
public virtual string? instance { get; init; }

private JsonSerializerOptions jsonOpts = new() { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull };
public string toJsonError() => JsonSerializer.Serialize(this, jsonOpts);
}
c#
public class RFC8807Error: Exception
{
public required virtual string type { get; init; }
public required virtual string title { get; init; }
public virtual int status { get; init; } = 400;
public required virtual string detail { get; init; }
public virtual string? instance { get; init; }

private JsonSerializerOptions jsonOpts = new() { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull };
public string toJsonError() => JsonSerializer.Serialize(this, jsonOpts);
}
The idea being, I could override it with sane defaults, and only having to deal with a subset of the arguments
c#
public class unknownCapabilityException : RFC8807Error
{
public unknownCapabilityException()
{
base.type = "urn:ietf:params:jmap:error:unknownCapability";
base.title = "Unknown Capability";
}
}
c#
public class unknownCapabilityException : RFC8807Error
{
public unknownCapabilityException()
{
base.type = "urn:ietf:params:jmap:error:unknownCapability";
base.title = "Unknown Capability";
}
}
c#
string errorDetail = $"JMAP requires POST, for all requests. Verb {req.Request.Method} is not supported.";
var errorValue = new unknownCapabilityException()
{
detail = errorDetail,
status = 405,
};
string errorJson = errorValue.toJsonError();

// ... Send the error JSON file back to the client later
c#
string errorDetail = $"JMAP requires POST, for all requests. Verb {req.Request.Method} is not supported.";
var errorValue = new unknownCapabilityException()
{
detail = errorDetail,
status = 405,
};
string errorJson = errorValue.toJsonError();

// ... Send the error JSON file back to the client later
The issue is that, my new unknownCapabilityException raises an CS9035, Required member ‘RFC8807Error.title’ must be set in the object initializer or attribute constructor. (and same for type), even tho it's set in the constructor of unknownCapabilityException... Any pointer on how I could fix this issue ?
15 replies