< Summary

Information
Class: IceRpc.DispatchException
Assembly: IceRpc
File(s): /home/runner/work/icerpc-csharp/icerpc-csharp/src/IceRpc/DispatchException.cs
Tag: 2300_35243572715
Line coverage
100%
Covered lines: 19
Uncovered lines: 0
Coverable lines: 19
Total lines: 69
Line coverage: 100%
Branch coverage
93%
Covered branches: 15
Total branches: 16
Branch coverage: 93.7%
Method coverage
100%
Covered methods: 5
Fully covered methods: 5
Total methods: 5
Method coverage: 100%
Full method coverage: 100%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_ConvertToInternalError()100%11100%
get_ErrorMessage()100%11100%
get_StatusCode()100%11100%
.ctor(...)75%44100%
FromException(...)100%1212100%

File(s)

/home/runner/work/icerpc-csharp/icerpc-csharp/src/IceRpc/DispatchException.cs

#LineLine coverage
 1// Copyright (c) ZeroC, Inc.
 2
 3using IceRpc.Internal;
 4
 5namespace IceRpc;
 6
 7/// <summary>Represents an exception thrown while dispatching a request. It's encoded as a response with a status code
 8/// greater than <see cref="StatusCode.Ok" />.</summary>
 9public sealed class DispatchException : Exception
 10{
 11    /// <summary>Gets or sets a value indicating whether the exception should be converted into a <see
 12    /// cref="DispatchException" /> with status code <see cref="StatusCode.InternalError" /> when thrown from a
 13    /// dispatch.</summary>
 14    /// <value>When <see langword="true" />, this exception is converted into dispatch exception with status code <see
 15    /// cref="StatusCode.InternalError" /> just before it's encoded. Defaults to <see langword="true" /> for an
 16    /// exception decoded from an <see cref="IncomingResponse" />, and <see langword="false" /> for an exception created
 17    /// by the application using a constructor of <see cref="DispatchException" />.</value>
 7218    public bool ConvertToInternalError { get; set; }
 19
 20    /// <summary>Gets the error message of the failure response that carries this exception.</summary>
 21    /// <value>The <see cref="Exception.Message" /> of this exception, followed by the type and the message of its
 22    /// <see cref="Exception.InnerException" /> when there is one.</value>
 3323    public string ErrorMessage => DispatchErrorMessage.Compose(Message, InnerException);
 24
 25    /// <summary>Gets the status code.</summary>
 26    /// <value>The <see cref="IceRpc.StatusCode" /> of this exception.</value>
 6627    public StatusCode StatusCode { get; }
 28
 29    /// <summary>Constructs a new instance of <see cref="DispatchException" />.</summary>
 30    /// <param name="statusCode">The status code of this exception. It must be greater than <see
 31    /// cref="StatusCode.Ok" />.</param>
 32    /// <param name="message">A message that describes the exception.</param>
 33    /// <param name="innerException">The exception that is the cause of the current exception.</param>
 34    /// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="statusCode" /> is equal to <see
 35    /// cref="StatusCode.Ok" />.</exception>
 36    public DispatchException(
 37        StatusCode statusCode,
 38        string? message = null,
 39        Exception? innerException = null)
 6240        : base(message ?? $"The dispatch failed with status code {statusCode}.", innerException) =>
 6241        StatusCode = statusCode > StatusCode.Ok ? statusCode :
 6242            throw new ArgumentOutOfRangeException(
 6243                nameof(statusCode),
 6244                $"The status code of a {nameof(DispatchException)} must be greater than {nameof(StatusCode.Ok)}.");
 45
 46    /// <summary>Converts an exception thrown by a dispatch into a dispatch exception.</summary>
 47    /// <param name="exception">The exception thrown by the dispatch.</param>
 48    /// <returns><paramref name="exception" /> when it is a <see cref="DispatchException" /> with
 49    /// <see cref="ConvertToInternalError" /> set to <see langword="false" />; otherwise, a new dispatch exception with
 50    /// <paramref name="exception" /> as its inner exception and a status code that depends on the type of
 51    /// <paramref name="exception" />: <see cref="StatusCode.InvalidData" /> for an <see cref="InvalidDataException" />,
 52    /// <see cref="StatusCode.NotSupported" /> for a <see cref="NotSupportedException" />,
 53    /// <see cref="StatusCode.TruncatedPayload" /> for an <see cref="IceRpcException" /> with error
 54    /// <see cref="IceRpcError.TruncatedData" />, and <see cref="StatusCode.InternalError" /> for any other exception,
 55    /// including a <see cref="DispatchException" /> with <see cref="ConvertToInternalError" /> set to
 56    /// <see langword="true" />.</returns>
 57    public static DispatchException FromException(Exception exception) =>
 3658        exception is DispatchException { ConvertToInternalError: false } dispatchException ?
 3659        dispatchException :
 3660        new DispatchException(
 3661            exception switch
 3662            {
 463                InvalidDataException => StatusCode.InvalidData,
 364                NotSupportedException => StatusCode.NotSupported,
 665                IceRpcException { IceRpcError: IceRpcError.TruncatedData } => StatusCode.TruncatedPayload,
 1266                _ => StatusCode.InternalError
 3667            },
 3668            innerException: exception);
 69}