< Summary

Information
Class: LiveKit.Rtc.RpcError
Assembly: LivekitRtc
File(s): /home/runner/work/livekit-server-sdk-dotnet/livekit-server-sdk-dotnet/LivekitRtc/Rpc.cs
Line coverage
97%
Covered lines: 47
Uncovered lines: 1
Coverable lines: 48
Total lines: 237
Line coverage: 97.9%
Branch coverage
75%
Covered branches: 9
Total branches: 12
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
.ctor(...)100%11100%
.ctor(...)50%22100%
BuiltIn(...)100%11100%
FromProto(...)100%44100%
ToProto()75%44100%
GetDefaultMessage(...)50%2283.33%

File(s)

/home/runner/work/livekit-server-sdk-dotnet/livekit-server-sdk-dotnet/LivekitRtc/Rpc.cs

#LineLine coverage
 1// author: https://github.com/pabloFuente
 2
 3using System;
 4using System.Collections.Generic;
 5using System.Threading.Tasks;
 6
 7namespace LiveKit.Rtc
 8{
 9    /// <summary>
 10    /// Data passed to method handler for incoming RPC invocations.
 11    /// </summary>
 12    public class RpcInvocationData
 13    {
 14        /// <summary>
 15        /// Gets the unique request ID. Will match at both sides of the call, useful for debugging or logging.
 16        /// </summary>
 17        public string RequestId { get; }
 18
 19        /// <summary>
 20        /// Gets the unique participant identity of the caller.
 21        /// </summary>
 22        public string CallerIdentity { get; }
 23
 24        /// <summary>
 25        /// Gets the payload of the request. User-definable format, typically JSON.
 26        /// </summary>
 27        public string Payload { get; }
 28
 29        /// <summary>
 30        /// Gets the maximum time the caller will wait for a response in seconds.
 31        /// </summary>
 32        public double ResponseTimeout { get; }
 33
 34        /// <summary>
 35        /// Initializes a new RpcInvocationData.
 36        /// </summary>
 37        /// <param name="requestId">The request ID.</param>
 38        /// <param name="callerIdentity">The caller identity.</param>
 39        /// <param name="payload">The payload.</param>
 40        /// <param name="responseTimeout">The response timeout in seconds.</param>
 41        public RpcInvocationData(
 42            string requestId,
 43            string callerIdentity,
 44            string payload,
 45            double responseTimeout
 46        )
 47        {
 48            RequestId = requestId ?? throw new ArgumentNullException(nameof(requestId));
 49            CallerIdentity =
 50                callerIdentity ?? throw new ArgumentNullException(nameof(callerIdentity));
 51            Payload = payload ?? string.Empty;
 52            ResponseTimeout = responseTimeout;
 53        }
 54    }
 55
 56    /// <summary>
 57    /// RPC error codes.
 58    /// </summary>
 59    public enum RpcErrorCode
 60    {
 61        /// <summary>
 62        /// Application error in method handler.
 63        /// </summary>
 64        ApplicationError = 1500,
 65
 66        /// <summary>
 67        /// Connection timeout.
 68        /// </summary>
 69        ConnectionTimeout = 1501,
 70
 71        /// <summary>
 72        /// Response timeout.
 73        /// </summary>
 74        ResponseTimeout = 1502,
 75
 76        /// <summary>
 77        /// Recipient disconnected.
 78        /// </summary>
 79        RecipientDisconnected = 1503,
 80
 81        /// <summary>
 82        /// Response payload too large.
 83        /// </summary>
 84        ResponsePayloadTooLarge = 1504,
 85
 86        /// <summary>
 87        /// Failed to send.
 88        /// </summary>
 89        SendFailed = 1505,
 90
 91        /// <summary>
 92        /// Method not supported at destination.
 93        /// </summary>
 94        UnsupportedMethod = 1400,
 95
 96        /// <summary>
 97        /// Recipient not found.
 98        /// </summary>
 99        RecipientNotFound = 1401,
 100
 101        /// <summary>
 102        /// Request payload too large.
 103        /// </summary>
 104        RequestPayloadTooLarge = 1402,
 105
 106        /// <summary>
 107        /// RPC not supported by server.
 108        /// </summary>
 109        UnsupportedServer = 1403,
 110
 111        /// <summary>
 112        /// Unsupported RPC version.
 113        /// </summary>
 114        UnsupportedVersion = 1404,
 115    }
 116
 117    /// <summary>
 118    /// Specialized error handling for RPC methods.
 119    /// Instances of this type, when thrown in a method handler, will have their message
 120    /// serialized and sent across the wire.
 121    /// </summary>
 122    public class RpcError : Exception
 123    {
 1124        private static readonly Dictionary<RpcErrorCode, string> ErrorMessages = new Dictionary<
 1125            RpcErrorCode,
 1126            string
 1127        >
 1128        {
 1129            { RpcErrorCode.ApplicationError, "Application error in method handler" },
 1130            { RpcErrorCode.ConnectionTimeout, "Connection timeout" },
 1131            { RpcErrorCode.ResponseTimeout, "Response timeout" },
 1132            { RpcErrorCode.RecipientDisconnected, "Recipient disconnected" },
 1133            { RpcErrorCode.ResponsePayloadTooLarge, "Response payload too large" },
 1134            { RpcErrorCode.SendFailed, "Failed to send" },
 1135            { RpcErrorCode.UnsupportedMethod, "Method not supported at destination" },
 1136            { RpcErrorCode.RecipientNotFound, "Recipient not found" },
 1137            { RpcErrorCode.RequestPayloadTooLarge, "Request payload too large" },
 1138            { RpcErrorCode.UnsupportedServer, "RPC not supported by server" },
 1139            { RpcErrorCode.UnsupportedVersion, "Unsupported RPC version" },
 1140        };
 141
 142        /// <summary>
 143        /// Gets the error code.
 144        /// </summary>
 145        public int Code { get; }
 146
 147        /// <summary>
 148        /// Gets optional additional data associated with the error (JSON recommended).
 149        /// </summary>
 150        public string? RpcData { get; }
 151
 152        /// <summary>
 153        /// Creates an error object with the given code and message, plus an optional data payload.
 154        /// </summary>
 155        /// <param name="code">Your error code (Error codes 1001-1999 are reserved for built-in errors).</param>
 156        /// <param name="message">A readable error message.</param>
 157        /// <param name="data">Optional additional data associated with the error (JSON recommended).</param>
 158        public RpcError(int code, string message, string? data = null)
 10159            : base(message)
 10160        {
 161            Code = code;
 162            RpcData = data;
 10163        }
 164
 165        /// <summary>
 166        /// Creates an error from an error code enum.
 167        /// </summary>
 168        /// <param name="code">The error code.</param>
 169        /// <param name="message">Optional custom message.</param>
 170        /// <param name="data">Optional additional data.</param>
 171        public RpcError(RpcErrorCode code, string? message = null, string? data = null)
 1172            : base(message ?? GetDefaultMessage(code))
 1173        {
 174            Code = (int)code;
 175            RpcData = data;
 1176        }
 177
 178        /// <summary>
 179        /// Creates a built-in error from an error code.
 180        /// </summary>
 181        /// <param name="code">The error code.</param>
 182        /// <param name="data">Optional additional data.</param>
 183        /// <returns>A new RpcError instance.</returns>
 184        public static RpcError BuiltIn(RpcErrorCode code, string? data = null)
 1185        {
 1186            return new RpcError(code, null, data);
 1187        }
 188
 189        /// <summary>
 190        /// Creates an RpcError from a proto message.
 191        /// </summary>
 192        /// <param name="proto">The proto error.</param>
 193        /// <returns>A new RpcError instance.</returns>
 194        internal static RpcError FromProto(LiveKit.Proto.RpcError proto)
 9195        {
 196            // The FFI always sets the field, as "" when the remote sent no data
 9197            var data = proto.HasData && proto.Data.Length > 0 ? proto.Data : null;
 9198            return new RpcError((int)proto.Code, proto.Message, data);
 9199        }
 200
 201        /// <summary>
 202        /// Converts to a proto message.
 203        /// </summary>
 204        /// <returns>The proto error.</returns>
 205        internal LiveKit.Proto.RpcError ToProto()
 2206        {
 2207            var error = new LiveKit.Proto.RpcError
 2208            {
 2209                Code = (uint)Code,
 2210                Message = Message ?? string.Empty,
 2211            };
 212
 2213            if (RpcData != null)
 1214            {
 1215                error.Data = RpcData;
 1216            }
 217
 2218            return error;
 2219        }
 220
 221        private static string GetDefaultMessage(RpcErrorCode code)
 1222        {
 1223            if (ErrorMessages.TryGetValue(code, out var message))
 1224            {
 1225                return message;
 226            }
 0227            return "Unknown error";
 1228        }
 229    }
 230
 231    /// <summary>
 232    /// Delegate for RPC method handlers.
 233    /// </summary>
 234    /// <param name="data">The invocation data.</param>
 235    /// <returns>The response payload as a string.</returns>
 236    public delegate Task<string> RpcMethodHandler(RpcInvocationData data);
 237}