/// <summary> /// Converts the validation result to an error result which will be serialized by ServiceStack in a clean and human-readable way. /// </summary> /// <param name="result">The validation result</param> /// <returns></returns> public static ValidationErrorResult ToErrorResult(this ValidationResult result) { Dictionary <string, string> ToStringDictionary(ValidationFailure error) { try { return(error.FormattedMessagePlaceholderValues.ToStringDictionary()); } catch (Exception e) { return(null); } } var validationResult = new ValidationErrorResult(); foreach (var error in result.Errors) { validationResult.Errors.Add(new ValidationErrorField(error.ErrorCode, error.PropertyName, error.ErrorMessage, error.AttemptedValue) { Meta = error.CustomState as Dictionary <string, string> ?? ToStringDictionary(error) }); } return(validationResult); }
/// <summary> /// Converts the validation result to an error result which will be serialized by ServiceStack in a clean and human-readable way. /// </summary> /// <param name="result">The validation result</param> /// <returns></returns> public static ValidationErrorResult ToErrorResult(this ValidationResult result) { var validationResult = new ValidationErrorResult(); foreach (var error in result.Errors) { validationResult.Errors.Add(new ValidationErrorField(error.ErrorCode, error.PropertyName, error.ErrorMessage, error.AttemptedValue)); } return(validationResult); }
public static object CreateErrorResponse <TRequest>(TRequest request, ValidationErrorResult validationError) { var responseStatus = ResponseStatusTranslator.Instance.Parse(validationError); var errorResponse = CreateErrorResponse( request, new ValidationError(validationError), responseStatus); return(errorResponse); }
public static object CreateErrorResponse(object request, ValidationErrorResult validationError) { var responseStatus = validationError.ToResponseStatus(); var errorResponse = CreateErrorResponse( request, new ValidationError(validationError), responseStatus); return(errorResponse); }
public IActionResult CalculateRoversPosition([FromBody] MarsRoversModel model) { var validationResult = _marsRoversValidator.ValidateModelIfNotNull(model); if (!validationResult.IsValid) { return(ValidationErrorResult.Create(validationResult, nameof(MarsRoversModel))); } var resource = _marsRoversManager.CalculateRoversPosition(model); return(Created("", resource)); }
/// <summary> /// Converts the validation result to an error result which will be serialized by ServiceStack in a clean and human-readable way. /// </summary> /// <param name="result">The validation result</param> /// <returns></returns> public static ValidationErrorResult ToErrorResult(this ValidationResult result) { var validationResult = new ValidationErrorResult(); foreach (var error in result.Errors) { validationResult.Errors.Add(new ValidationErrorField(error.ErrorCode, error.PropertyName, error.ErrorMessage, error.AttemptedValue) { Meta = error.CustomState as Dictionary <string, string> ?? error.PlaceholderValues }); } return(validationResult); }
public ActionResult <IEnumerable <InstalledPackage> > RunMigrations([FromQuery] string packageName) { try { _packageMigrationRunner.RunPackageMigrationsIfPending(packageName); return(_packagingService.GetAllInstalledPackages().ToList()); } catch (Exception ex) { _logger.LogError(ex, "Package migration failed on package {Package}", packageName); return(ValidationErrorResult.CreateNotificationValidationErrorResult( $"Package migration failed on package {packageName} with error: {ex.Message}. Check log for full details.")); } }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { // AngelR ConnectionString: "SQLCONNSTR_BUSINESSESDIRECTORY": "Server=IVDL0122;Initial Catalog=BusinessesDirectory;User ID=sa;Password=12345678;" // JoséR ConnectionString: "SQLCONNSTR_BUSINESSESDIRECTORY": "Server=[computername];Initial Catalog=BusinessesDirectory;User ID=sa;Password=[password];" // GabrielA ConnectionString: "SQLCONNSTR_BUSINESSESDIRECTORY": "Server=[computername];Initial Catalog=BusinessesDirectory;User ID=sa;Password=[password];" // Azure ConnectionString: "SQLCONNSTR_BUSINESSESDIRECTORY": [Azure Connection String] var sqlServerConnectionString = Configuration.GetConnectionString("SQLCONNSTR_BUSINESSESDIRECTORY"); if (sqlServerConnectionString == null) { throw new Exception("BUSINESSESDIRECTORY connection string is not set."); } services.AddDbContext <BusinessesDirectoryContext>(options => options.UseSqlServer(sqlServerConnectionString)); services.AddCors(options => { options.AddPolicy(allowedDevelopmentOrigin, policy => policy.WithOrigins("http://localhost:4200") .AllowAnyHeader() .AllowAnyMethod() ); options.AddPolicy(allowedProductionOrigin, policy => policy.WithOrigins("https://www.lockdowndirectory.com") .AllowAnyHeader() .AllowAnyMethod() ); } ); services.AddTransient <Seed>(); services.AddAutoMapper(); services.AddScoped <IBusinessService, BusinessService>(); services.AddScoped <IBusinessTypeService, BusinessTypeService>(); services.AddScoped <IBusinessRepository, BusinessRepository>(); services.AddScoped <IBusinessTypeRepository, BusinessTypeRepository>(); services.AddScoped <ILocationService, LocationService>(); services.AddScoped <ILocationRepository, LocationRepository>(); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); //services.AddSpaStaticFiles(configuration => //{ // configuration.RootPath = "BD_DistFiles"; //}); // Configure Custom Validation Error Handling services.Configure <ApiBehaviorOptions>(options => { options.InvalidModelStateResponseFactory = ctx => ValidationErrorResult.GetValidationErrors(ctx); }); }
public async Task <IActionResult> PutAsync(int id, [FromBody] SaveProductResource resource) { var validator = new ProductValidator(); var validationResult = validator.ValidateIfNullModel(resource); if (!validationResult.IsValid) { return(ValidationErrorResult.Create(validationResult, typeof(SaveProductResource).Name)); } var product = _mapper.Map <SaveProductResource, Product>(resource); var result = await _productService.UpdateAsync(id, product); if (!result.Success) { return(BadRequest(new ErrorResource(result.Message))); } var productResource = _mapper.Map <Product, ProductResource>(result.Product); return(Ok(productResource)); }
public async Task <ActionResult <UserDisplay> > PostVerifyInvite([FromQuery] int id, [FromQuery] string token) { if (string.IsNullOrWhiteSpace(token)) { return(NotFound()); } var decoded = token.FromUrlBase64(); if (decoded.IsNullOrWhiteSpace()) { return(NotFound()); } var identityUser = await _userManager.FindByIdAsync(id.ToString()); if (identityUser == null) { return(NotFound()); } var result = await _userManager.ConfirmEmailAsync(identityUser, decoded); if (result.Succeeded == false) { return(ValidationErrorResult.CreateNotificationValidationErrorResult(result.Errors.ToErrorMessage())); } await _signInManager.SignOutAsync(); await _signInManager.SignInAsync(identityUser, false); var user = _userService.GetUserById(id); return(_umbracoMapper.Map <UserDisplay>(user)); }
public void OnException(ExceptionContext context) { var message = RequestInfo.GetRequestErrorMessage(context.HttpContext.Request); if (context.Exception is BaseException) { var ex = context.Exception as BaseException; var errorResult = new ValidationErrorResult(message, ex.Errors); this.BuildResponse(context, ex.GetObjectResult(errorResult)); } else { LogInternalServerError(context); var errorResult = new ValidationErrorResult(message, GetInternalServerError()); if (_env.IsDevelopment()) { errorResult.AddDeveloperMessage(context.Exception.Message); } var objectResult = new InternalServerErrorObjectResult(errorResult); this.BuildResponse(context, objectResult); } }
public static ResponseStatus ToResponseStatus(this ValidationErrorResult validationResult) { return(validationResult.IsValid ? CreateSuccessResponse(validationResult.SuccessMessage) : ResponseStatusUtils.CreateResponseStatus(validationResult.ErrorCode, validationResult.ErrorMessage, validationResult.Errors)); }
public override ObjectResult GetObjectResult(ValidationErrorResult errorResult) { return(new BadRequestObjectResult(errorResult));; }
public abstract ObjectResult GetObjectResult(ValidationErrorResult errorResult);
public override ObjectResult GetObjectResult(ValidationErrorResult errorResult) { return(new NotFoundObjectResult(errorResult));; }