| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using IceRpc.Internal; |
| | | 4 | | using System.Collections.Immutable; |
| | | 5 | | |
| | | 6 | | namespace IceRpc; |
| | | 7 | | |
| | | 8 | | /// <summary>Represents a response frame sent by the application.</summary> |
| | | 9 | | public sealed class OutgoingResponse : OutgoingFrame |
| | | 10 | | { |
| | | 11 | | /// <summary>Gets the error message of this response.</summary> |
| | | 12 | | /// <value>The error message of this response. It is guaranteed to be non-<see langword="null" /> when <see |
| | | 13 | | /// cref="StatusCode" /> is different from <see cref="StatusCode.Ok" />, and <see langword="null" /> when <see |
| | | 14 | | /// cref="StatusCode" /> is <see cref="StatusCode.Ok" />.</value> |
| | 176 | 15 | | public string? ErrorMessage { get; } |
| | | 16 | | |
| | | 17 | | /// <summary>Gets or sets the fields of this response.</summary> |
| | | 18 | | /// <value>The fields of this incoming response. Defaults to <see cref="ImmutableDictionary{TKey, TValue}.Empty" |
| | | 19 | | /// />.</value> |
| | 422 | 20 | | public IDictionary<ResponseFieldKey, OutgoingFieldValue> Fields { get; set; } = |
| | 2976 | 21 | | ImmutableDictionary<ResponseFieldKey, OutgoingFieldValue>.Empty; |
| | | 22 | | |
| | | 23 | | /// <summary>Gets the status code of this response.</summary> |
| | | 24 | | /// <value>The <see cref="IceRpc.StatusCode" /> of this response.</value> |
| | 6498 | 25 | | public StatusCode StatusCode { get; } |
| | | 26 | | |
| | | 27 | | /// <summary>Constructs an outgoing response with the <see cref="StatusCode.Ok" /> status code and a <see |
| | | 28 | | /// langword="null" /> error message.</summary> |
| | | 29 | | /// <param name="request">The incoming request.</param> |
| | | 30 | | /// <remarks>The constructor also associates this response with the request. If another response is already set on |
| | | 31 | | /// the request, its payload and payload continuation are completed.</remarks> |
| | | 32 | | public OutgoingResponse(IncomingRequest request) |
| | 2859 | 33 | | : base(request.Protocol) |
| | 2859 | 34 | | { |
| | 2859 | 35 | | request.Response = this; |
| | 2859 | 36 | | StatusCode = StatusCode.Ok; |
| | 2859 | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <summary>Constructs an outgoing response.</summary> |
| | | 40 | | /// <param name="request">The incoming request.</param> |
| | | 41 | | /// <param name="statusCode">The status code. It must be greater than <see cref="StatusCode.Ok" />.</param> |
| | | 42 | | /// <param name="message">The error message or null to use the default error message.</param> |
| | | 43 | | /// <param name="exception">The exception that is the cause of this failure.</param> |
| | | 44 | | /// <remarks>The constructor also associates this response with the request. If another response is already set on |
| | | 45 | | /// the request, its payload and payload continuation are completed.</remarks> |
| | | 46 | | public OutgoingResponse( |
| | | 47 | | IncomingRequest request, |
| | | 48 | | StatusCode statusCode, |
| | | 49 | | string? message = null, |
| | | 50 | | Exception? exception = null) |
| | 117 | 51 | | : base(request.Protocol) |
| | 117 | 52 | | { |
| | 117 | 53 | | StatusCode = statusCode > StatusCode.Ok ? statusCode : |
| | 117 | 54 | | throw new ArgumentException( |
| | 117 | 55 | | $"The status code for an exception must be greater than {nameof(StatusCode.Ok)}.", |
| | 117 | 56 | | nameof(statusCode)); |
| | | 57 | | |
| | 117 | 58 | | request.Response = this; |
| | | 59 | | |
| | 117 | 60 | | ErrorMessage = DispatchErrorMessage.Compose( |
| | 117 | 61 | | message ?? $"The dispatch failed with status code {statusCode}.", |
| | 117 | 62 | | exception); |
| | 117 | 63 | | } |
| | | 64 | | } |