{"record":{"id":"dbd66d8cf025ba2c","repo":"clockworklabs/SpacetimeDB","slug":"e-tostring","errorCode":null,"errorMessage":"e.ToString()","messagePattern":"e\\.ToString\\(\\)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"crates/bindings-csharp/BSATN.Runtime/Builtins.cs","lineNumber":658,"sourceCode":"\n    public TResult Match<TResult>(Func<T, TResult> onOk, Func<E, TResult> onErr) =>\n        this switch\n        {\n            OkR(var v) => onOk(v),\n            ErrR(var e) => onErr(e),\n            _ => throw new InvalidOperationException(\"Unknown Result variant.\"),\n        };\n\n    public static Result<T, E> Ok(T value) => new OkR(value);\n\n    public static Result<T, E> Err(E error) => new ErrR(error);\n\n    public T UnwrapOrThrow()\n    {\n        return this switch\n        {\n            OkR(var v) => v,\n            ErrR(var e) when e is not null => throw new Exception(e.ToString()),\n            ErrR(_) => throw new InvalidOperationException(\n                \"Result failed without an error object.\"\n            ),\n            _ => throw new InvalidOperationException(\"Unknown Result variant.\"),\n        };\n    }\n\n    public T UnwrapOr(T defaultValue) =>\n        this switch\n        {\n            OkR(var v) => v,\n            _ => defaultValue,\n        };\n\n    public T UnwrapOrElse(Func<E, T> f) =>\n        this switch\n        {\n            OkR(var v) => v,","sourceCodeStart":640,"sourceCodeEnd":676,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/6dee26c6efc2856793e12b148a59742964f5d783/crates/bindings-csharp/BSATN.Runtime/Builtins.cs#L640-L676","documentation":"Result<T,E> is the SDK's error-handling type: Ok carries a value, Err carries an error. UnwrapOrThrow returns the Ok payload; on an Err whose error object is non-null it throws a plain Exception whose message is e.ToString() (the error's own string form). An Err with a null error object instead throws InvalidOperationException.","triggerScenarios":"Calling UnwrapOrThrow on a Result produced by a failed operation — a rejected reducer call or client API that returned Err(error) — without first checking the variant.","commonSituations":"Prototypes or tests unwrapping results that actually contain business errors (validation failure, permission denied, module rejection); assuming success after an async call without inspecting the outcome.","solutions":["Check the Result variant before unwrapping, and handle the Err payload explicitly","If a fallback is acceptable, use result.UnwrapOr(defaultValue) which never throws","When you must unwrap, catch the Exception and read its Message — it is the Err value's ToString()","Fix the underlying condition the error object reports (usually a module- or server-side rejection)"],"exampleFix":"// before\nvar value = result.UnwrapOrThrow(); // throws Exception with e.ToString()\n\n// after\nvar value = result.UnwrapOr(fallback); // no throw; fallback used on Err","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try\n{\n    return result.UnwrapOrThrow();\n}\ncatch (InvalidOperationException)\n{\n    // Err with a null error object: log and rethrow as a domain error\n    throw;\n}\ncatch (Exception ex)\n{\n    // Message is the Err value's ToString(): log it for diagnosis\n    logger.LogError(\"Result failed: {Message}\", ex.Message);\n    throw;\n}","preventionTips":["Prefer explicit variant checks over blind UnwrapOrThrow","Use UnwrapOr(defaultValue) when a fallback is acceptable","Log the Err payload where the Result is produced, not only where it is unwrapped","Treat the thrown message as the module/server rejection reason and fix the root cause"],"tags":["csharp","spacetimedb","result","unwrap","error-handling"],"backgroundTag":"result-unwrap-on-error","analyzedSha":"6dee26c6efc2856793e12b148a59742964f5d783","analyzedAt":"2026-08-20T06:08:37.179Z","contentChangedAt":"2026-08-20T06:08:37.179Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}