| | 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> |
| 141 | 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> |
| 788 | 18 | | public IDictionary<ResponseFieldKey, OutgoingFieldValue> Fields { get; set; } = |
| 5584 | 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> |
| 12295 | 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) |
| 5455 | 31 | | : base(request.Protocol) |
| 5455 | 32 | | { |
| 5455 | 33 | | request.Response = this; |
| 5455 | 34 | | StatusCode = StatusCode.Ok; |
| 5455 | 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) |
| 129 | 49 | | : base(request.Protocol) |
| 129 | 50 | | { |
| 129 | 51 | | request.Response = this; |
| 129 | 52 | | StatusCode = statusCode > StatusCode.Ok ? statusCode : |
| 129 | 53 | | throw new ArgumentException( |
| 129 | 54 | | $"The status code for an exception must be greater than {nameof(StatusCode.Ok)}.", |
| 129 | 55 | | nameof(statusCode)); |
| | 56 | |
|
| 129 | 57 | | string errorMessage = message ?? $"The dispatch failed with status code {statusCode}."; |
| 129 | 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 | | } |
| 129 | 62 | | ErrorMessage = errorMessage; |
| 129 | 63 | | } |
| | 64 | | } |