Skip to content

Commit b065af3

Browse files
authored
Merge pull request #72 from Virtual-Finland-Development/fix/response-signatures-as-in-ds-definition
Error message signature update according to the dataspace specs
2 parents 234ae08 + d7a3fe8 commit b065af3

1 file changed

Lines changed: 24 additions & 29 deletions

File tree

VirtualFinland.UserAPI/src/VirtualFinland.UsersAPI/Middleware/ErrorHandlerMiddleware.cs

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ public class ErrorHandlerMiddleware
1010
private readonly ILogger<ErrorHandlerMiddleware> _logger;
1111

1212
/// <summary>
13-
/// RFC7807 Problem Details
13+
/// Dataspace error response details
1414
/// </summary>
15-
private class ErrorResponseDetails : Microsoft.AspNetCore.Mvc.ProblemDetails
15+
private class ErrorResponseDetails
1616
{
17-
17+
public string Type { get; set; } = string.Empty;
18+
public string Message { get; set; } = string.Empty;
1819
}
1920

2021
public ErrorHandlerMiddleware(RequestDelegate next, ILogger<ErrorHandlerMiddleware> logger)
@@ -33,53 +34,47 @@ public async Task Invoke(HttpContext context)
3334
_logger.LogError(error, "Request processing failure!");
3435
var response = context.Response;
3536
response.ContentType = "application/json";
36-
Dictionary<string, List<string>> validationErrorDetails = new Dictionary<string, List<string>>();
3737

38-
switch(error)
38+
ErrorResponseDetails errorResponseDetails = new()
39+
{
40+
Type = "",
41+
Message = ""
42+
};
43+
44+
switch (error)
3945
{
4046
case NotAuthorizedException:
4147
// custom application error
4248
response.StatusCode = (int)HttpStatusCode.Unauthorized;
49+
errorResponseDetails.Type = "Unauthorized";
50+
errorResponseDetails.Message = error.Message ?? "Not authorized";
4351
break;
4452
case NotFoundException:
4553
// not found error
4654
response.StatusCode = (int)HttpStatusCode.NotFound;
55+
errorResponseDetails.Type = "NotFound";
56+
errorResponseDetails.Message = error.Message ?? "Not found";
4757
break;
48-
case BadRequestException e:
58+
case BadRequestException:
4959
// bad request error
5060
response.StatusCode = (int)HttpStatusCode.BadRequest;
51-
52-
e.ValidationErrors?.ForEach( o => validationErrorDetails.Add(o.Field, new List<string>() { o.Message }));
61+
errorResponseDetails.Type = "BadRequest";
62+
errorResponseDetails.Message = error.Message ?? "Bad request";
5363
break;
5464
default:
5565
// unhandled error
5666
response.StatusCode = (int)HttpStatusCode.InternalServerError;
67+
errorResponseDetails.Type = "InternalServerError";
68+
errorResponseDetails.Message = error.Message ?? "Internal Server Error";
5769
break;
5870
}
5971

60-
ErrorResponseDetails errorResponseDetails = validationErrorDetails?.Count == 0 ? new ErrorResponseDetails()
61-
{
62-
Type = "https://tools.ietf.org/html/rfc7231",
63-
Title = error.Message,
64-
Detail = error.Message,
65-
Status = response.StatusCode,
66-
Instance = response.HttpContext.Request.Path
67-
} : new ErrorResponseDetails()
68-
{
69-
Type = "https://tools.ietf.org/html/rfc7231",
70-
Title = error.Message,
71-
Detail = error.Message,
72-
Status = response.StatusCode,
73-
Instance = response.HttpContext.Request.Path,
74-
Extensions = { new KeyValuePair<string, object?>( "errors", validationErrorDetails) }
75-
};
76-
7772
var result = JsonSerializer.Serialize(errorResponseDetails,
7873
new JsonSerializerOptions
79-
{
80-
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
81-
WriteIndented = true
82-
});
74+
{
75+
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
76+
WriteIndented = true
77+
});
8378
await response.WriteAsync(result);
8479
}
8580
}

0 commit comments

Comments
 (0)