|
| 1 | +using System; |
| 2 | +using Commmunity.AspNetCore.ExceptionHandling.Builder; |
| 3 | +using Microsoft.AspNetCore.Http; |
| 4 | +using Microsoft.AspNetCore.Mvc; |
| 5 | +using Microsoft.AspNetCore.Mvc.Abstractions; |
| 6 | +using Microsoft.AspNetCore.Mvc.Infrastructure; |
| 7 | +using Microsoft.AspNetCore.Routing; |
| 8 | +using Microsoft.Extensions.DependencyInjection; |
| 9 | + |
| 10 | +namespace Commmunity.AspNetCore.ExceptionHandling.Mvc |
| 11 | +{ |
| 12 | + public static class PolicyBuilderExtensions |
| 13 | + { |
| 14 | + private static readonly RouteData EmptyRouteData = new RouteData(); |
| 15 | + |
| 16 | + private static readonly ActionDescriptor EmptyActionDescriptor = new ActionDescriptor(); |
| 17 | + |
| 18 | + /// <summary> |
| 19 | + /// Set <see cref="IActionResult"/> to response and pass control to next handler. |
| 20 | + /// </summary> |
| 21 | + /// <typeparam name="TException"> |
| 22 | + /// The exception type |
| 23 | + /// </typeparam> |
| 24 | + /// <typeparam name="TResult"> |
| 25 | + /// The action result type. Should implement <see cref="IActionResult"/>. |
| 26 | + /// </typeparam> |
| 27 | + /// <param name="builder"> |
| 28 | + /// The policy builder |
| 29 | + /// </param> |
| 30 | + /// <param name="resultFactory"> |
| 31 | + /// The <see cref="IActionResult"/> factory. |
| 32 | + /// </param> |
| 33 | + /// <param name="index" optional="true"> |
| 34 | + /// Handler index in the chain. Optional. By default handler added to the end of chain. |
| 35 | + /// </param> |
| 36 | + /// <returns> |
| 37 | + /// Policy builder |
| 38 | + /// </returns> |
| 39 | + public static IResponseHandlers<TException> WithActionResult<TException, TResult>( |
| 40 | + this IResponseHandlers<TException> builder, Func<HttpRequest, TException, TResult> resultFactory, int index = -1) |
| 41 | + where TException : Exception |
| 42 | + where TResult : IActionResult |
| 43 | + { |
| 44 | + return builder.WithBody((request, streamWriter, exception) => |
| 45 | + { |
| 46 | + var context = request.HttpContext; |
| 47 | + var executor = context.RequestServices.GetService<IActionResultExecutor<TResult>>(); |
| 48 | + |
| 49 | + if (executor == null) |
| 50 | + { |
| 51 | + throw new InvalidOperationException($"No result executor for '{typeof(TResult).FullName}' has been registered."); |
| 52 | + } |
| 53 | + |
| 54 | + var routeData = context.GetRouteData() ?? EmptyRouteData; |
| 55 | + |
| 56 | + var actionContext = new ActionContext(context, routeData, EmptyActionDescriptor); |
| 57 | + |
| 58 | + return executor.ExecuteAsync(actionContext, resultFactory(request, exception)); |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Set <see cref="IActionResult"/> to response and pass control to next handler. |
| 64 | + /// </summary> |
| 65 | + /// <typeparam name="TException"> |
| 66 | + /// The exception type |
| 67 | + /// </typeparam> |
| 68 | + /// <typeparam name="TResult"> |
| 69 | + /// The action result type. Should implement <see cref="IActionResult"/>. |
| 70 | + /// </typeparam> |
| 71 | + /// <param name="builder"> |
| 72 | + /// The policy builder |
| 73 | + /// </param> |
| 74 | + /// <param name="result"> |
| 75 | + /// The <see cref="IActionResult"/> action result. |
| 76 | + /// </param> |
| 77 | + /// <param name="index" optional="true"> |
| 78 | + /// Handler index in the chain. Optional. By default handler added to the end of chain. |
| 79 | + /// </param> |
| 80 | + /// <returns> |
| 81 | + /// Policy builder |
| 82 | + /// </returns> |
| 83 | + public static IResponseHandlers<TException> WithActionResult<TException, TResult>( |
| 84 | + this IResponseHandlers<TException> builder, TResult result, int index = -1) |
| 85 | + where TException : Exception |
| 86 | + where TResult : IActionResult |
| 87 | + { |
| 88 | + return builder.WithActionResult((request, exception) => result); |
| 89 | + } |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// Set <see cref="ObjectResult"/> to response and pass control to next handler. |
| 93 | + /// </summary> |
| 94 | + /// <typeparam name="TException"> |
| 95 | + /// The exception type |
| 96 | + /// </typeparam> |
| 97 | + /// <typeparam name="TObject"> |
| 98 | + /// The result object type. |
| 99 | + /// </typeparam> |
| 100 | + /// <param name="builder"> |
| 101 | + /// The policy builder |
| 102 | + /// </param> |
| 103 | + /// <param name="value"> |
| 104 | + /// The result object. |
| 105 | + /// </param> |
| 106 | + /// <param name="index" optional="true"> |
| 107 | + /// Handler index in the chain. Optional. By default handler added to the end of chain. |
| 108 | + /// </param> |
| 109 | + /// <returns> |
| 110 | + /// Policy builder |
| 111 | + /// </returns> |
| 112 | + public static IResponseHandlers<TException> WithObjectResult<TException, TObject>( |
| 113 | + this IResponseHandlers<TException> builder, TObject value, int index = -1) |
| 114 | + where TException : Exception |
| 115 | + { |
| 116 | + return builder.WithActionResult(new ObjectResult(value), index); |
| 117 | + } |
| 118 | + |
| 119 | + /// <summary> |
| 120 | + /// Set <see cref="ObjectResult"/> to response and pass control to next handler. |
| 121 | + /// </summary> |
| 122 | + /// <typeparam name="TException"> |
| 123 | + /// The exception type |
| 124 | + /// </typeparam> |
| 125 | + /// <typeparam name="TObject"> |
| 126 | + /// The result object type. |
| 127 | + /// </typeparam> |
| 128 | + /// <param name="builder"> |
| 129 | + /// The policy builder |
| 130 | + /// </param> |
| 131 | + /// <param name="valueFactory"> |
| 132 | + /// The result object factory. |
| 133 | + /// </param> |
| 134 | + /// <param name="index" optional="true"> |
| 135 | + /// Handler index in the chain. Optional. By default handler added to the end of chain. |
| 136 | + /// </param> |
| 137 | + /// <returns> |
| 138 | + /// Policy builder |
| 139 | + /// </returns> |
| 140 | + public static IResponseHandlers<TException> WithObjectResult<TException, TObject>( |
| 141 | + this IResponseHandlers<TException> builder, Func<HttpRequest, TException, TObject> valueFactory, int index = -1) |
| 142 | + where TException : Exception |
| 143 | + { |
| 144 | + return builder.WithActionResult( |
| 145 | + (request, exception) => new ObjectResult(valueFactory(request, exception)), index); |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments