|
| 1 | +using System; |
| 2 | +using System.Runtime.Serialization; |
| 3 | + |
| 4 | +namespace SpacetimeDB |
| 5 | +{ |
| 6 | + /// <summary> |
| 7 | + /// The base class for all SpacetimeDB SDK exceptions. |
| 8 | + /// This allows users to catch all SpacetimeDB-specific exceptions in one catch block |
| 9 | + /// or configure their debugger to ignore them. |
| 10 | + /// </summary> |
| 11 | + [Serializable] |
| 12 | + public class SpacetimeDBException : Exception |
| 13 | + { |
| 14 | + // Base HRESULT for SpacetimeDB exceptions (0x8A000000 is in the user-defined range) |
| 15 | + private const int SPACETIMEDB_HRESULT_BASE = unchecked((int)0x8A000000); |
| 16 | + |
| 17 | + // Specific HRESULTs for different exception types |
| 18 | + public const int SPACETIMEDB_EMPTY_REDUCER_NAME = SPACETIMEDB_HRESULT_BASE + 1; |
| 19 | + |
| 20 | + public SpacetimeDBException() |
| 21 | + : base() |
| 22 | + { |
| 23 | + HResult = SPACETIMEDB_HRESULT_BASE; |
| 24 | + } |
| 25 | + |
| 26 | + public SpacetimeDBException(string? message) |
| 27 | + : base(message) |
| 28 | + { |
| 29 | + HResult = SPACETIMEDB_HRESULT_BASE; |
| 30 | + } |
| 31 | + |
| 32 | + public SpacetimeDBException(string? message, Exception? innerException) |
| 33 | + : base(message, innerException) |
| 34 | + { |
| 35 | + HResult = SPACETIMEDB_HRESULT_BASE; |
| 36 | + } |
| 37 | + |
| 38 | + protected SpacetimeDBException(SerializationInfo info, StreamingContext context) |
| 39 | + : base(info, context) { } |
| 40 | + } |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// The exception that is thrown when one of the arguments provided to a method is not valid. |
| 44 | + /// This is the base class for all SpacetimeDB argument exceptions. |
| 45 | + /// </summary> |
| 46 | + [Serializable] |
| 47 | + public class SpacetimeDBArgumentException : SpacetimeDBException |
| 48 | + { |
| 49 | + private readonly string? _paramName; |
| 50 | + |
| 51 | + public SpacetimeDBArgumentException() |
| 52 | + : base("Value does not fall within the expected range.") { } |
| 53 | + |
| 54 | + public SpacetimeDBArgumentException(string? message) |
| 55 | + : base(message) { } |
| 56 | + |
| 57 | + public SpacetimeDBArgumentException(string? message, Exception? innerException) |
| 58 | + : base(message, innerException) { } |
| 59 | + |
| 60 | + public SpacetimeDBArgumentException( |
| 61 | + string? message, |
| 62 | + string? paramName, |
| 63 | + Exception? innerException |
| 64 | + ) |
| 65 | + : base(message, innerException) |
| 66 | + { |
| 67 | + _paramName = paramName; |
| 68 | + } |
| 69 | + |
| 70 | + public SpacetimeDBArgumentException(string? message, string? paramName) |
| 71 | + : base(message) |
| 72 | + { |
| 73 | + _paramName = paramName; |
| 74 | + } |
| 75 | + |
| 76 | + protected SpacetimeDBArgumentException(SerializationInfo info, StreamingContext context) |
| 77 | + : base(info, context) |
| 78 | + { |
| 79 | + _paramName = info.GetString("ParamName"); |
| 80 | + } |
| 81 | + |
| 82 | + public override void GetObjectData(SerializationInfo info, StreamingContext context) |
| 83 | + { |
| 84 | + base.GetObjectData(info, context); |
| 85 | + info.AddValue("ParamName", _paramName, typeof(string)); |
| 86 | + } |
| 87 | + |
| 88 | + public virtual string? ParamName => _paramName; |
| 89 | + } |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// The exception that is thrown when an empty reducer name is received from the server. |
| 93 | + /// This is a known condition that is handled internally by the SpacetimeDB client. |
| 94 | + /// </summary> |
| 95 | + [Serializable] |
| 96 | + public class SpacetimeDBEmptyReducerNameException : SpacetimeDBArgumentException |
| 97 | + { |
| 98 | + public SpacetimeDBEmptyReducerNameException() |
| 99 | + : base("Empty reducer name received from server", (string?)null) |
| 100 | + { |
| 101 | + HResult = SPACETIMEDB_EMPTY_REDUCER_NAME; |
| 102 | + } |
| 103 | + |
| 104 | + public SpacetimeDBEmptyReducerNameException(string? paramName) |
| 105 | + : base("Empty reducer name received from server", paramName) |
| 106 | + { |
| 107 | + HResult = SPACETIMEDB_EMPTY_REDUCER_NAME; |
| 108 | + } |
| 109 | + |
| 110 | + public SpacetimeDBEmptyReducerNameException(string? message, string? paramName) |
| 111 | + : base(message, paramName) |
| 112 | + { |
| 113 | + HResult = SPACETIMEDB_EMPTY_REDUCER_NAME; |
| 114 | + } |
| 115 | + |
| 116 | + public SpacetimeDBEmptyReducerNameException(string? message, Exception? innerException) |
| 117 | + : base(message, null, innerException) |
| 118 | + { |
| 119 | + HResult = SPACETIMEDB_EMPTY_REDUCER_NAME; |
| 120 | + } |
| 121 | + |
| 122 | + public SpacetimeDBEmptyReducerNameException( |
| 123 | + string? message, |
| 124 | + string? paramName, |
| 125 | + Exception? innerException |
| 126 | + ) |
| 127 | + : base(message, paramName, innerException) |
| 128 | + { |
| 129 | + HResult = SPACETIMEDB_EMPTY_REDUCER_NAME; |
| 130 | + } |
| 131 | + |
| 132 | + protected SpacetimeDBEmptyReducerNameException( |
| 133 | + SerializationInfo info, |
| 134 | + StreamingContext context |
| 135 | + ) |
| 136 | + : base(info, context) { } |
| 137 | + } |
| 138 | +} |
0 commit comments