Simple package which allows C# records to inherit from Exception through the RecordException type.
$ dotnet add package RecordExceptionrecords inheriting from ExceptionInstall from NuGet: dotnet add package RecordException
Then define your exceptions like this:
using RecordExceptions;
public record MissingIdException(int id): RecordException;
The exception message will automatically be MissingIdException: id = {id}.
Probably fine for debugging, but if you want to customize that, you can:
public record MissingIdException(int id): RecordException
{
public override string Message => $"Id {id} is missing";
}
The message may also be specified in the construtor:
public record OperationCancelledException: RecordException("User has cancelled the operation.")
You can also wrap exception into InnerException, it might be useful for adding relevant information:
public record SomethingBad(Exception InnerException): RecordException("Something really bad has happened", InnerException);
Notes:
x with { Prop = newValue } syntax to work.public override string ToString() => base.ToString();.License is MIT, so you can use it however you want to. If you need a record inheriting from another class, feel free to fork this repo.