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