Files
SpacetimeDB/crates/bindings-csharp/Runtime/Exceptions.cs
rekhoff 39f01289e5 C# implementation of Transactions for Procedures (#3809)
# Description of Changes

Implements the C# equivalent of #3638

This implement uses inheritance, where abstract base classes (like
`ProcedureContextBase` in `ProcedureContext.cs`) store the core of the
implementation, and then generated wrappers (like `ProcedureContext` in
the generated FFI.cs file) inherit from them.

For error handling, we work like Rust's implementation of `Result<T,E>`
but we require `where E : Exception` because of how exceptions work in
C#. Transaction-level failures come back as a `TxOutcome` and user
errors should follow the `Result<T,E>` pattern. In this implementation,
we have `UnwrapOrThrow()` throws exceptions directly because of C#'s
error handling pattern.

Unlike the Rust implementation's direct `Result` propagation, we are
using an `AbortGuard` pattern (in `ProcedureContext.cs`) for exception
handling, which uses `IDisposable` for automatic cleanup.

Most changes should have fairly similar Rust-equivalents beyond that.
For module authors, the changes here allow for the transation logic to
work like:
```csharp
ctx.TryWithTx<ResultType, Exception>(tx => {
    // transaction logic
    return Result<ResultType, Exception>.Ok(result);
});
```
This change includes a number of tests added to the
`sdks/csharp/examples~/regression-tests/`'s `server` and `client` to
validate the behavior of the changes. `server` changes provide further
usage examples for module authors.

# API and ABI breaking changes

Should not be a breaking change

# Expected complexity level and risk

2

# Testing

- [x] Created Regression Tests that show transitions in procedures
working in various ways, all of which pass.
2025-12-18 18:41:47 +00:00

109 lines
2.9 KiB
C#

namespace SpacetimeDB;
using SpacetimeDB.Internal;
public abstract class StdbException : Exception
{
public abstract override string Message { get; }
}
public class NotInTransactionException : StdbException
{
public override string Message => "ABI call can only be made while in a transaction";
}
public class BsatnDecodeException : StdbException
{
public override string Message => "Couldn't decode the BSATN to the expected type";
}
public class NoSuchTableException : StdbException
{
public override string Message => "No such table";
}
public class NoSuchIndexException : StdbException
{
public override string Message => "No such index";
}
public class IndexNotUniqueException : StdbException
{
public override string Message => "The index was not unique";
}
public class NoSuchRowException : StdbException
{
public override string Message => "The row was not found, e.g., in an update call";
}
public class UniqueConstraintViolationException : StdbException
{
public override string Message => "Value with given unique identifier already exists";
}
public class ScheduleAtDelayTooLongException : StdbException
{
public override string Message => "Specified delay in scheduling row was too long";
}
public class BufferTooSmallException : StdbException
{
public override string Message => "The provided buffer is not large enough to store the data";
}
public class NoSuchIterException : StdbException
{
public override string Message => "The provided row iterator does not exist";
}
public class NoSuchLogStopwatch : StdbException
{
public override string Message => "The provided stopwatch does not exist";
}
public class NoSuchBytesException : StdbException
{
public override string Message => "The provided bytes source or sink does not exist";
}
public class NoSpaceException : StdbException
{
public override string Message => "The provided bytes sink has no more room left";
}
public class AutoIncOverflowException : StdbException
{
public override string Message => "The auto-increment sequence overflowed";
}
public class TransactionWouldBlockException : StdbException
{
public override string Message => "Attempted operation while another transaction is open";
}
public class TransactionNotAnonymousException : StdbException
{
public override string Message => "The transaction is not anonymous";
}
public class TransactionIsReadOnlyException : StdbException
{
public override string Message => "The transaction is read-only";
}
public class TransactionIsMutableException : StdbException
{
public override string Message =>
"ABI call can only be made while inside a read-only transaction";
}
public class UnknownException : StdbException
{
private readonly Errno code;
internal UnknownException(Errno code) => this.code = code;
public override string Message => $"SpacetimeDB error code {code}";
}