Skip to content

Instantly share code, notes, and snippets.

@ByteDev
Last active August 12, 2021 03:25
Show Gist options
  • Save ByteDev/a3f44c1d90b006660ae3c647f6ec1ab8 to your computer and use it in GitHub Desktop.
Save ByteDev/a3f44c1d90b006660ae3c647f6ec1ab8 to your computer and use it in GitHub Desktop.
using System;
using System.Runtime.Serialization;

// ...

[Serializable]
public class EntityNotFoundException : Exception
{
    private const string DefaultMessage = "Entity does not exist.";

    public int EntityId { get; }

    public EntityNotFoundException() : base(DefaultMessage)
    {
    }

    public EntityNotFoundException(string message) : base(message)
    {
    }

    public EntityNotFoundException(string message, Exception innerException) : base(message, innerException)
    {
    }

    public EntityNotFoundException(int entityId) : this($"Entity does not exist with ID: '{entityId}'.")
    {
        EntityId = entityId;
    }

    protected EntityNotFoundException(SerializationInfo info, StreamingContext context)
        : base(info, context)
    {
        EntityId = (int)info.GetValue(nameof(EntityId), typeof(int));
    }

    public override void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        base.GetObjectData(info, context);

        info.AddValue(nameof(EntityId), EntityId, typeof(int));
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment