Пример #1
0
 public void Build(CarterModuleBase parentModule)
 {
     foreach (var route in _routes)
     {
         parentModule.Routes.Add((route.Method, route.Path), r => route.Handler(parentModule, r));
     }
 }
Пример #2
0
        private static async Task <IResponse> CallHandlerAsync(MethodInfo handler,
                                                               IEnumerable <ParameterMapper> parameterMappers,
                                                               ReturnValueMapper returnValueMapper,
                                                               CarterModuleBase self,
                                                               HttpContext ctx)
        {
            try
            {
                var mappedParameters = parameterMappers.Select(m => m(self, ctx)).ToArray();

                var actualReturnValue = handler.Invoke(self, mappedParameters);
                var mappedReturnValue = await returnValueMapper(actualReturnValue);

                return(mappedReturnValue);
            }
            catch (ValidationException e)
            {
                if (!e.Errors?.Any() ?? true)
                {
                    return(ResponseBuilder.BadRequest.WithErrorBody(e.Message).ToResponse());
                }
                return(ResponseBuilder.BadRequest.WithErrorBody(e.Errors).ToResponse());
            }
        }