New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

RecordException

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

RecordException

Simple package which allows C# records to inherit from Exception through the RecordException type.

Source
nugetNuGet
Version
0.1.2
Version published
Maintainers
1
Created
Source

C# records inheriting from Exception

Install 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:

  • If you are using some parameters in your message, prefer to override the Message property if you want the x with { Prop = newValue } syntax to work.
  • Even when you provide your own message, all properties will be shown bellow the stacktrace. If you don't like this, public override string ToString() => base.ToString();.
  • Don't be too surprised if this stops working with the next C# version 😅. Yes, records are not supposed to inherit from System.Exception. But they forgot to check base type of a base type ;)

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.

Keywords

record

FAQs

Package last updated on 07 Nov 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts