C
C#

Issues trying to create new error types

Issues trying to create new error types

CCOLAMAroro11/19/2023
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 ?
Xx0rld11/19/2023
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";
}
CCOLAMAroro11/19/2023
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.
Xx0rld11/19/2023
just don't use required keyword for things not setup in the constructor
CCOLAMAroro11/19/2023
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
Xx0rld11/19/2023
I have the latest rider version and it doesn't compile even removing required on title and type
Xx0rld11/19/2023
No description
Xx0rld11/19/2023
the error is not logic
CCOLAMAroro11/19/2023
Removing the required did the trick. I have warninng about property being maybe unset, but that's someone else problem Thanks for the help 🙂
Xx0rld11/19/2023
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

Looking for more? Join the community!

C
C#

Issues trying to create new error types

Join Server
Want results from more Discord servers?
Add your server
Recommended Posts
I have a Visual-Studio Proj. in .NET Framework and need help fixing the "CS0120" error it throws up.Basically I want to make an App which you just run and it will automatically, by pressing just one bplease help!!! (winforms c#)Does anyone know how can i make the user move the last drawn shape on a picturebox (the picture box WPF ScrollViewer issuesHello, I have the problem that the scrollviewer in C# (WPF) does not work properly. I haven't define✅ Quick - Unity "using" directiveReally quick question guys, when trying to use methods from other scripts in Unity, let's say the ot✅ .Split() Returning Spaces Between Words in String[]Hi. I have an string array `string[] strArr` of words in format `[word1, word2]` and so on - just a I want to compile a program myself with less filesI was working on a open source project, and I want to compile the program myself so I setup a github✅ Finding Processes but it displays as invisibleSo I am making a system monitor, and I ran into a problem where when I click the button to display t✅ Getting artifact from GitHub Repo ends in SSL_ERROR_SSLHello guys, working on a console app to more or less download artifacts from our private GitHub Repo✅ DB ErrorHey guys, I am working on a project and I am trying to connect it to a DB. I am following the instruCalculate Field Offset Based On Members' NameI'd like to know if the offset calculation is correct. the input used is as follows```cs private coFollowing a video to create power ups, but its not picking them up and I dont know why.Heres the code. I dont know whats wrong with it, from what I can tell its exactly the same as the vConverting int to double to output with decimalsHello again! i got a little problem with converting int to double, i dont get it to output the decimDb ConnectionGood afternoon guys, I need some help setting up the database connection for my project. I have thCan't read property in code behind```cs @page "/weather" @attribute [StreamRendering] <PageTitle>Weather</PageTitle> <h1>Weather</h1RabbitMQ starting service issuePlease guys I had `RabbitMQ` running fine, but after restarting my computer the RabbitMQ won't be stException Unhandled : Could not load file or assembly 'Serilog.Extensions.HostingWhen I run my application, this exception is thrown even before the start of debugging. System.IO.Figot this code that i wrote the get all score and print end score but i didnt read right help meexem is 40% assigments is 30% and participation is 30%Assistance regarding deciding on what to use.Hello there, I want to port one of my macOS apps to a native windows app and would like to ask whichHelp deciding Next.js/Node.js vs .NET API back-endHello. I really want to use .NET as my API back-end for a practice app I'm building. However, I dohow do i make it so when i press it this happens and if i do it again another thing happensive made a code in winforms so when i press with my left mouse button it hides everything, how do i