C
C#8mo ago
COLA

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 ?
9 Replies
x0rld
x0rld8mo ago
using the property initializer doesn't call your ctor
public unknownCapabilityException(string detail, int status)
{
base.type = "urn:ietf:params:jmap:error:unknownCapability";
base.title = "Unknown Capability";
}
public unknownCapabilityException(string detail, int status)
{
base.type = "urn:ietf:params:jmap:error:unknownCapability";
base.title = "Unknown Capability";
}
COLA
COLA7mo ago
even when doing
c#

public class unknownCapabilityException : RFC8807Error
{
public unknownCapabilityException(string detail, int status = 400)
{
base.type = "urn:ietf:params:jmap:error:unknownCapability";
base.title = "Unknown Capability";
base.detail = detail;
base.status = status;
}
}

...

var errorValue = new unknownCapabilityException(errorDetail, 405);
c#

public class unknownCapabilityException : RFC8807Error
{
public unknownCapabilityException(string detail, int status = 400)
{
base.type = "urn:ietf:params:jmap:error:unknownCapability";
base.title = "Unknown Capability";
base.detail = detail;
base.status = status;
}
}

...

var errorValue = new unknownCapabilityException(errorDetail, 405);
I get the same error message. Even worse, now even detail is undefined.
x0rld
x0rld7mo ago
just don't use required keyword for things not setup in the constructor
COLA
COLA7mo ago
I'll give this a try when my Visual Studio update is finished, thanks 🙂 They are required per the spec, I would have prefered if it was forced by the compiler to the consumers of the API, but I'm not going to fret too much if it's not possible
x0rld
x0rld7mo ago
I have the latest rider version and it doesn't compile even removing required on title and type
x0rld
x0rld7mo ago
No description
x0rld
x0rld7mo ago
the error is not logic
COLA
COLA7mo ago
Removing the required did the trick. I have warninng about property being maybe unset, but that's someone else problem Thanks for the help 🙂
x0rld
x0rld7mo ago
I think the inherithance fucked up the required keyword @COLAMAroro you have to add [SetsRequiredMembers] on your children constructor https://discord.com/channels/143867839282020352/598678594750775301/1175826056607969380
Want results from more Discord servers?
Add your server
More Posts