summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.Core/Messaging/ProtocolFaultResponseException.cs
blob: b5cab3b6761bcf5fc5e5f44cdea815d79a8fe695 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//-----------------------------------------------------------------------
// <copyright file="ProtocolFaultResponseException.cs" company="Andrew Arnott">
//     Copyright (c) Andrew Arnott. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.Messaging {
	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Net.Http;
	using System.Text;
	using System.Threading;
	using System.Threading.Tasks;
	using Validation;

	/// <summary>
	/// An exception to represent errors in the local or remote implementation of the protocol
	/// that includes the response message that should be returned to the HTTP client to comply
	/// with the protocol specification.
	/// </summary>
	public class ProtocolFaultResponseException : ProtocolException {
		/// <summary>
		/// The channel that produced the error response message, to be used in constructing the actual HTTP response.
		/// </summary>
		private readonly Channel channel;

		/// <summary>
		/// Initializes a new instance of the <see cref="ProtocolFaultResponseException"/> class
		/// such that it can be sent as a protocol message response to a remote caller.
		/// </summary>
		/// <param name="channel">The channel to use when encoding the response message.</param>
		/// <param name="errorResponse">The message to send back to the HTTP client.</param>
		/// <param name="faultedMessage">The message that was the cause of the exception.  May be null.</param>
		/// <param name="innerException">The inner exception.</param>
		/// <param name="message">The message for the exception.</param>
		protected internal ProtocolFaultResponseException(Channel channel, IDirectResponseProtocolMessage errorResponse, IProtocolMessage faultedMessage = null, Exception innerException = null, string message = null)
			: base(message ?? (innerException != null ? innerException.Message : null), faultedMessage, innerException) {
			Requires.NotNull(channel, "channel");
			Requires.NotNull(errorResponse, "errorResponse");
			this.channel = channel;
			this.ErrorResponseMessage = errorResponse;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="ProtocolFaultResponseException"/> class.
		/// </summary>
		/// <param name="info">The <see cref="System.Runtime.Serialization.SerializationInfo"/> 
		/// that holds the serialized object data about the exception being thrown.</param>
		/// <param name="context">The System.Runtime.Serialization.StreamingContext 
		/// that contains contextual information about the source or destination.</param>
		protected ProtocolFaultResponseException(
		  System.Runtime.Serialization.SerializationInfo info,
		  System.Runtime.Serialization.StreamingContext context)
			: base(info, context) {
			throw new NotImplementedException();
		}

		/// <summary>
		/// Gets the protocol message to send back to the client to report the error.
		/// </summary>
		public IDirectResponseProtocolMessage ErrorResponseMessage { get; private set; }

		/// <summary>
		/// Creates the HTTP response to forward to the client to report the error.
		/// </summary>
		/// <param name="cancellationToken">The cancellation token.</param>
		/// <returns>
		/// The HTTP response.
		/// </returns>
		public Task<HttpResponseMessage> CreateErrorResponseAsync(CancellationToken cancellationToken) {
			return this.channel.PrepareResponseAsync(this.ErrorResponseMessage, cancellationToken);
		}
	}
}