Esempio n. 1
0
        /// <summary>
        /// Set response body, close response builder and pass control to next handler.
        /// </summary>
        /// <typeparam name="TException">
        /// The exception type
        /// </typeparam>
        /// <param name="builder">
        /// The policy builder
        /// </param>
        /// <param name="settings">
        /// Delegate to write to response stream.
        /// </param>
        /// <param name="index" optional="true">
        /// Handler index in the chain. Optional. By default handler added to the end of chain.
        /// </param>
        /// <returns>
        /// Policy builder
        /// </returns>
        public static IResponseHandlers <TException> WithBody <TException>(
            this IResponseHandlers <TException> builder, Func <HttpRequest, Stream, TException, Task> settings, int index = -1)
            where TException : Exception
        {
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            builder.Services.Configure <RawResponseHandlerOptions <TException> >(responceOptions =>
            {
                responceOptions.SetResponse.Add((context, exception) =>
                                                RawResponseExceptionHandler <TException> .SetBody(context, exception, settings));
            });

            return(builder);
        }
Esempio n. 2
0
        // Set status code
        /// <summary>
        /// Configure response and pass control to next handler. It is recommended to finish chain using <see cref="Handled{TException}"/> when response builder will be configured.
        /// </summary>
        /// <typeparam name="TException">
        /// The exception type
        /// </typeparam>
        /// <param name="builder">
        /// The policy builder
        /// </param>
        /// <param name="responseAlreadyStartedBehaviour">
        /// The begaviour <see cref="ResponseAlreadyStartedBehaviour"/> in case response already started.
        /// </param>
        /// <param name="index" optional="true">
        /// Handler index in the chain. Optional. By default handler added to the end of chain.
        /// </param>
        /// <param name="statusCodeFactory">
        /// The factory for response status code. By default 500 will be used, unless code was already set by another handler for current request.
        /// </param>
        /// <returns>
        /// Response builder.
        /// </returns>
        public static IResponseHandlers <TException> Response <TException>(
            this IExceptionMapping <TException> builder,
            Func <TException, int> statusCodeFactory = null,
            ResponseAlreadyStartedBehaviour responseAlreadyStartedBehaviour = ResponseAlreadyStartedBehaviour.ReThrow,
            int index = -1)
            where TException : Exception
        {
            builder.Services.Configure <RawResponseHandlerOptions <TException> >(responceOptions =>
            {
                responceOptions.ResponseAlreadyStartedBehaviour = responseAlreadyStartedBehaviour;

                responceOptions.SetResponse.Add((context, exception) =>
                {
                    return(RawResponseExceptionHandler <TException> .SetStatusCode(context, exception, statusCodeFactory));
                });
            });

            builder.EnsureHandler <TException, RawResponseExceptionHandler <TException> >(index);

            return(builder as IResponseHandlers <TException>);
        }