Initial stage of getting rid of HttpResponseException and replacing it with the usage of ValidationErrorResult

This commit is contained in:
Elitsa Marinovska
2020-12-22 16:36:07 +01:00
parent 2c54b5c28c
commit 358a8ec2af
15 changed files with 180 additions and 192 deletions

View File

@@ -1,24 +1,21 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Umbraco.Core;
using Umbraco.Core.Configuration;
using Umbraco.Core.Mapping;
using Umbraco.Core.Models;
using Umbraco.Core.Security;
using Umbraco.Core.Services;
using Umbraco.Web.BackOffice.Filters;
using Umbraco.Web.Common.Attributes;
using Umbraco.Web.Common.Exceptions;
using Umbraco.Web.Models.ContentEditing;
using Umbraco.Web.Security;
using Constants = Umbraco.Core.Constants;
using Umbraco.Core.Configuration.Models;
using Microsoft.Extensions.Options;
using Microsoft.AspNetCore.Authorization;
using Umbraco.Web.Common.ActionsResults;
using Umbraco.Web.Common.Authorization;
namespace Umbraco.Web.BackOffice.Controllers
@@ -101,7 +98,7 @@ namespace Umbraco.Web.BackOffice.Controllers
public ActionResult<int> Create(int parentId, string key)
{
if (string.IsNullOrEmpty(key))
throw HttpResponseException.CreateNotificationValidationErrorResponse("Key can not be empty."); // TODO: translate
return ValidationErrorResult.CreateNotificationValidationErrorResult("Key can not be empty."); // TODO: translate
if (_localizationService.DictionaryItemExists(key))
{
@@ -109,7 +106,7 @@ namespace Umbraco.Web.BackOffice.Controllers
"dictionaryItem/changeKeyError",
_backofficeSecurityAccessor.BackOfficeSecurity.CurrentUser.GetUserCulture(_localizedTextService, _globalSettings),
new Dictionary<string, string> { { "0", key } });
throw HttpResponseException.CreateNotificationValidationErrorResponse(message);
return ValidationErrorResult.CreateNotificationValidationErrorResult(message);
}
try
@@ -130,7 +127,7 @@ namespace Umbraco.Web.BackOffice.Controllers
catch (Exception ex)
{
_logger.LogError(ex, "Error creating dictionary with {Name} under {ParentId}", key, parentId);
throw HttpResponseException.CreateNotificationValidationErrorResponse("Error creating dictionary item");
return ValidationErrorResult.CreateNotificationValidationErrorResult("Error creating dictionary item");
}
}
@@ -141,11 +138,8 @@ namespace Umbraco.Web.BackOffice.Controllers
/// The id.
/// </param>
/// <returns>
/// The <see cref="DictionaryDisplay"/>.
/// The <see cref="DictionaryDisplay"/>. Returns a not found response when dictionary item does not exist
/// </returns>
/// <exception cref="HttpResponseException">
/// Returns a not found response when dictionary item does not exist
/// </exception>
[DetermineAmbiguousActionByPassingParameters]
public ActionResult<DictionaryDisplay> GetById(int id)
{
@@ -163,11 +157,8 @@ namespace Umbraco.Web.BackOffice.Controllers
/// The id.
/// </param>
/// <returns>
/// The <see cref="DictionaryDisplay"/>.
/// The <see cref="DictionaryDisplay"/>. Returns a not found response when dictionary item does not exist
/// </returns>
/// <exception cref="HttpResponseException">
/// Returns a not found response when dictionary item does not exist
/// </exception>
[DetermineAmbiguousActionByPassingParameters]
public ActionResult<DictionaryDisplay> GetById(Guid id)
{
@@ -185,11 +176,8 @@ namespace Umbraco.Web.BackOffice.Controllers
/// The id.
/// </param>
/// <returns>
/// The <see cref="DictionaryDisplay"/>.
/// The <see cref="DictionaryDisplay"/>. Returns a not found response when dictionary item does not exist
/// </returns>
/// <exception cref="HttpResponseException">
/// Returns a not found response when dictionary item does not exist
/// </exception>
[DetermineAmbiguousActionByPassingParameters]
public ActionResult<DictionaryDisplay> GetById(Udi id)
{
@@ -213,13 +201,13 @@ namespace Umbraco.Web.BackOffice.Controllers
/// <returns>
/// The <see cref="DictionaryDisplay"/>.
/// </returns>
public DictionaryDisplay PostSave(DictionarySave dictionary)
public ActionResult<DictionaryDisplay> PostSave(DictionarySave dictionary)
{
var dictionaryItem =
_localizationService.GetDictionaryItemById(int.Parse(dictionary.Id.ToString()));
if (dictionaryItem == null)
throw HttpResponseException.CreateNotificationValidationErrorResponse("Dictionary item does not exist");
return ValidationErrorResult.CreateNotificationValidationErrorResult("Dictionary item does not exist");
var userCulture = _backofficeSecurityAccessor.BackOfficeSecurity.CurrentUser.GetUserCulture(_localizedTextService, _globalSettings);
@@ -236,7 +224,7 @@ namespace Umbraco.Web.BackOffice.Controllers
userCulture,
new Dictionary<string, string> { { "0", dictionary.Name } });
ModelState.AddModelError("Name", message);
throw HttpResponseException.CreateValidationErrorResponse(ModelState);
return new ValidationErrorResult(ModelState);
}
dictionaryItem.ItemKey = dictionary.Name;
@@ -263,7 +251,7 @@ namespace Umbraco.Web.BackOffice.Controllers
catch (Exception ex)
{
_logger.LogError(ex, "Error saving dictionary with {Name} under {ParentId}", dictionary.Name, dictionary.ParentId);
throw HttpResponseException.CreateNotificationValidationErrorResponse("Something went wrong saving dictionary");
return ValidationErrorResult.CreateNotificationValidationErrorResult("Something went wrong saving dictionary");
}
}