diff options
8 files changed, 38 insertions, 17 deletions
diff --git a/BackEnd/Timeline/Controllers/V2/TimelinePostV2Controller.cs b/BackEnd/Timeline/Controllers/V2/TimelinePostV2Controller.cs index aa839abf..8a4fa7ed 100644 --- a/BackEnd/Timeline/Controllers/V2/TimelinePostV2Controller.cs +++ b/BackEnd/Timeline/Controllers/V2/TimelinePostV2Controller.cs @@ -144,7 +144,7 @@ namespace Timeline.Controllers.V2 var data = body.DataList[i]; if (data is null) - return UnprocessableEntity(new CommonResponse(ErrorCodes.Common.InvalidModel, $"Data at index {i} is null.")); + return UnprocessableEntity(new ErrorResponse(ErrorResponse.InvalidRequest, $"Data at index {i} is null.")); try { @@ -153,7 +153,7 @@ namespace Timeline.Controllers.V2 } catch (FormatException) { - return UnprocessableEntity(new CommonResponse(ErrorCodes.Common.InvalidModel, $"Data at index {i} is not a valid base64 string.")); + return UnprocessableEntity(new ErrorResponse(ErrorResponse.InvalidRequest, $"Data at index {i} is not a valid base64 string.")); } } @@ -169,7 +169,7 @@ namespace Timeline.Controllers.V2 } catch (TimelinePostCreateDataException e) { - return UnprocessableEntity(new CommonResponse(ErrorCodes.Common.InvalidModel, $"Data at index {e.Index} is invalid. {e.Message}")); + return UnprocessableEntity(new ErrorResponse(ErrorResponse.InvalidRequest, $"Data at index {e.Index} is invalid. {e.Message}")); } } diff --git a/BackEnd/Timeline/Controllers/V2/TimelineV2Controller.cs b/BackEnd/Timeline/Controllers/V2/TimelineV2Controller.cs index 393446f7..7f620928 100644 --- a/BackEnd/Timeline/Controllers/V2/TimelineV2Controller.cs +++ b/BackEnd/Timeline/Controllers/V2/TimelineV2Controller.cs @@ -99,7 +99,7 @@ namespace Timeline.Controllers.V2 } catch (EntityNotExistException e) when (e.EntityType.Equals(EntityTypes.User)) { - return UnprocessableEntity(new CommonResponse(ErrorCodes.Common.InvalidModel, "Member username does not exist.")); + return UnprocessableEntity(new ErrorResponse(ErrorResponse.InvalidRequest, "Member username does not exist.")); } await _timelineService.AddMemberAsync(timelineId, userId); return NoContent(); @@ -127,7 +127,7 @@ namespace Timeline.Controllers.V2 } catch (EntityNotExistException e) when (e.EntityType.Equals(EntityTypes.User)) { - return UnprocessableEntity(new CommonResponse(ErrorCodes.Common.InvalidModel, "Member username does not exist.")); + return UnprocessableEntity(new ErrorResponse(ErrorResponse.InvalidRequest, "Member username does not exist.")); } await _timelineService.RemoveMemberAsync(timelineId, userId); return NoContent(); diff --git a/BackEnd/Timeline/Controllers/V2/UserV2Controller.cs b/BackEnd/Timeline/Controllers/V2/UserV2Controller.cs index e556bf8e..2eb67d72 100644 --- a/BackEnd/Timeline/Controllers/V2/UserV2Controller.cs +++ b/BackEnd/Timeline/Controllers/V2/UserV2Controller.cs @@ -90,7 +90,7 @@ namespace Timeline.Controllers.V2 [ProducesResponseType(StatusCodes.Status403Forbidden)] [ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status422UnprocessableEntity)] - public async Task<ActionResult<HttpUser>> Patch([FromBody] HttpUserPatchRequest body, [FromRoute][Username] string username) + public async Task<ActionResult<HttpUser>> PatchAsync([FromBody] HttpUserPatchRequest body, [FromRoute][Username] string username) { var userId = await _userService.GetUserIdByUsernameAsync(username); if (UserHasPermission(UserPermission.UserManagement)) @@ -114,6 +114,8 @@ namespace Timeline.Controllers.V2 } } + private const string RootUserInvalidOperationMessage = "Can't do this operation on root user."; + /// <summary> /// Delete a user and all his related data. You have to be administrator. /// </summary> @@ -125,7 +127,7 @@ namespace Timeline.Controllers.V2 [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status403Forbidden)] [ProducesResponseType(StatusCodes.Status422UnprocessableEntity)] - public async Task<ActionResult<CommonDeleteResponse>> Delete([FromRoute][Username] string username) + public async Task<ActionResult> DeleteAsync([FromRoute][Username] string username) { try { @@ -134,7 +136,7 @@ namespace Timeline.Controllers.V2 } catch (InvalidOperationOnRootUserException) { - return UnprocessableEntity(); + return UnprocessableEntity(new ErrorResponse(ErrorResponse.InvalidOperation, RootUserInvalidOperationMessage)); } } @@ -144,7 +146,7 @@ namespace Timeline.Controllers.V2 [ProducesResponseType(StatusCodes.Status403Forbidden)] [ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status422UnprocessableEntity)] - public async Task<ActionResult<CommonResponse>> PutUserPermission([FromRoute][Username] string username, [FromRoute] UserPermission permission) + public async Task<ActionResult> PutUserPermissionAsync([FromRoute][Username] string username, [FromRoute] UserPermission permission) { try { @@ -154,17 +156,17 @@ namespace Timeline.Controllers.V2 } catch (InvalidOperationOnRootUserException) { - return UnprocessableEntity(); + return UnprocessableEntity(new ErrorResponse(ErrorResponse.InvalidOperation, RootUserInvalidOperationMessage)); } } [HttpDelete("{username}/permissions/{permission}"), PermissionAuthorize(UserPermission.UserManagement)] [ProducesResponseType(StatusCodes.Status204NoContent)] - [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status403Forbidden)] + [ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status422UnprocessableEntity)] - public async Task<ActionResult<CommonResponse>> DeleteUserPermission([FromRoute][Username] string username, [FromRoute] UserPermission permission) + public async Task<ActionResult> DeleteUserPermissionAsync([FromRoute][Username] string username, [FromRoute] UserPermission permission) { try { @@ -174,7 +176,7 @@ namespace Timeline.Controllers.V2 } catch (InvalidOperationOnRootUserException) { - return UnprocessableEntity(); + return UnprocessableEntity(new ErrorResponse(ErrorResponse.InvalidOperation, RootUserInvalidOperationMessage)); } } } diff --git a/BackEnd/Timeline/Filters/CatchEntityAlreadyExistExceptionFilter.cs b/BackEnd/Timeline/Filters/CatchEntityAlreadyExistExceptionFilter.cs index a1d1c0e6..469f2de5 100644 --- a/BackEnd/Timeline/Filters/CatchEntityAlreadyExistExceptionFilter.cs +++ b/BackEnd/Timeline/Filters/CatchEntityAlreadyExistExceptionFilter.cs @@ -23,7 +23,7 @@ namespace Timeline.Filters {
if (context.HttpContext.Request.Path.StartsWithSegments("/api/v2")) { - context.Result = new UnprocessableEntityObjectResult(new CommonResponse(ErrorCodes.Conflict.Default, "An entity with given key already exists.")); + context.Result = new UnprocessableEntityObjectResult(new ErrorResponse(ErrorResponse.EntityExist, "An entity with given key already exists.")); } else { diff --git a/BackEnd/Timeline/Filters/CatchEntityDeletedExceptionFilter.cs b/BackEnd/Timeline/Filters/CatchEntityDeletedExceptionFilter.cs index 41f4894d..5a6c75ec 100644 --- a/BackEnd/Timeline/Filters/CatchEntityDeletedExceptionFilter.cs +++ b/BackEnd/Timeline/Filters/CatchEntityDeletedExceptionFilter.cs @@ -9,7 +9,7 @@ namespace Timeline.Filters {
public void OnException(ExceptionContext context)
{
- if (context.Exception is EntityDeletedException e)
+ if (context.Exception is EntityDeletedException)
{
context.Result = new StatusCodeResult(StatusCodes.Status410Gone);
}
diff --git a/BackEnd/Timeline/Filters/CatchEntityNotExistExceptionFilter.cs b/BackEnd/Timeline/Filters/CatchEntityNotExistExceptionFilter.cs index 876f792a..01a3d52d 100644 --- a/BackEnd/Timeline/Filters/CatchEntityNotExistExceptionFilter.cs +++ b/BackEnd/Timeline/Filters/CatchEntityNotExistExceptionFilter.cs @@ -25,7 +25,7 @@ namespace Timeline.Filters {
if (context.HttpContext.Request.Path.StartsWithSegments("/api/v2")) { - context.Result = new NotFoundObjectResult(new CommonResponse(ErrorCodes.NotExist.Default, "The entity does not exist.")); + context.Result = new NotFoundResult(); } else { diff --git a/BackEnd/Timeline/Helpers/InvalidModelResponseFactory.cs b/BackEnd/Timeline/Helpers/InvalidModelResponseFactory.cs index 906ac566..d7dca9d1 100644 --- a/BackEnd/Timeline/Helpers/InvalidModelResponseFactory.cs +++ b/BackEnd/Timeline/Helpers/InvalidModelResponseFactory.cs @@ -10,7 +10,7 @@ namespace Timeline.Helpers {
if (context.HttpContext.Request.Path.StartsWithSegments("/api/v2")) { - return new UnprocessableEntityObjectResult(new CommonResponse(ErrorCodes.Common.InvalidModel, "Request is of bad format.")); + return new UnprocessableEntityObjectResult(new ErrorResponse(ErrorResponse.InvalidRequest, "Request is of bad format.")); }
var modelState = context.ModelState;
diff --git a/BackEnd/Timeline/Models/Http/ErrorResponse.cs b/BackEnd/Timeline/Models/Http/ErrorResponse.cs new file mode 100644 index 00000000..119e3977 --- /dev/null +++ b/BackEnd/Timeline/Models/Http/ErrorResponse.cs @@ -0,0 +1,19 @@ +namespace Timeline.Models.Http +{ + public class ErrorResponse + { + public const string InvalidRequest = "INVALID_REQUEST"; + public const string EntityExist = "ENTITY_EXIST"; + public const string InvalidOperation = "INVALID_OPERATION"; + + public ErrorResponse(string error, string message) + { + Error = error; + Message = message; + } + + public string Error { get; set; } + + public string Message { get; set; } + } +} |