Skip to content

Commit 6a1e63f

Browse files
committed
Add Response Types
1 parent a13a1b8 commit 6a1e63f

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

Poc.TextProcessor.Presentation.RestApi/Controllers/TextController.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,27 @@ public TextController(ITextService textService,
2727

2828
[HttpGet("Options")]
2929
[Produces("application/json", "application/xml")]
30-
public SortCollection GetOptions()
30+
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(SortCollection))]
31+
public IActionResult GetOptions()
3132
{
32-
return _textSortService.List();
33+
var sortOptions = _textSortService.List();
34+
return Ok(sortOptions);
3335
}
3436

3537
[HttpGet("Statistics")]
3638
[Produces("application/json", "application/xml")]
37-
public Statistics GetStatistics([FromQuery] string textToAnalyze)
39+
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(Statistics))]
40+
public IActionResult GetStatistics([FromQuery] string textToAnalyze)
3841
{
39-
return _textService.GetStatistics(textToAnalyze);
42+
var textStatics = _textService.GetStatistics(textToAnalyze);
43+
return Ok(textStatics);
4044
}
4145

4246
[HttpGet("OrderedText")]
4347
[Produces("application/json", "application/xml")]
4448
[ResponseOnException(HttpStatusCode.InternalServerError, typeof(SortingException))]
49+
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(string))]
50+
[ProducesResponseType(StatusCodes.Status400BadRequest)]
4551
public IActionResult GetOrderedText([FromQuery] string textToOrder, string orderOption)
4652
{
4753
if (Enum.TryParse(orderOption, true, out SortOption orderOptionEnum))

Poc.TextProcessor.Presentation.RestApi/Infrastructure/FilterAttributes/ExceptionHandlingFilter.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.AspNetCore.Mvc.Controllers;
33
using Microsoft.AspNetCore.Mvc.Filters;
44
using Poc.TextProcessor.CrossCutting.Globalization;
5+
using System.Text;
56

67
namespace Poc.TextProcessor.Presentation.RestApi.Infrastructure.FilterAttributes
78
{
@@ -15,12 +16,22 @@ public void OnException(ExceptionContext context)
1516

1617
if (responseOnExceptionAttribute != null && responseOnExceptionAttribute.ExceptionTypes.Contains(exceptionType))
1718
{
18-
context.Result = new ObjectResult(Messages.UnexpectedError)
19+
var responseContent = HandleExceptionResponse(context.Exception.Message);
20+
context.Result = new ObjectResult(responseContent)
1921
{
2022
StatusCode = (int)responseOnExceptionAttribute.ResponseCode
2123
};
2224
context.ExceptionHandled = true;
2325
}
2426
}
27+
28+
private static string HandleExceptionResponse(string exceptionMessage)
29+
{
30+
var messageBuilder = new StringBuilder();
31+
messageBuilder.AppendLine(Messages.UnexpectedError);
32+
messageBuilder.AppendLine(exceptionMessage);
33+
34+
return messageBuilder.ToString();
35+
}
2536
}
2637
}

0 commit comments

Comments
 (0)