Skip to content

Commit 2d314ea

Browse files
authored
Merge pull request #52 from MakingSense/DSL-625-quoted-profile-on-error-response
Added quoted profile on API's error responses.
2 parents 86e46ab + b9666fc commit 2d314ea

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/MakingSense.AspNetCore.HypermediaApi/ExceptionHandling/ApiErrorHandlerMiddleware.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,15 @@ private static void WriteResponseHeaders(Problem problem, HttpRequest request, H
161161
// otherwise return application/json + schema
162162
var acceptsProblemType = request.Headers[HeaderNames.Accept].Contains(PROBLEM_MEDIATYPE);
163163

164+
var keepUnquoted = request.Query.ContainsKey("keep-unquoted-profile");
165+
164166
response.OnStarting((o) =>
165167
{
166168
response.ContentType = acceptsProblemType ? PROBLEM_MEDIATYPE
167-
: response.ContentType + $"; profile={SchemaAttribute.Path}problem.json";
169+
: response.ContentType + CreateProfileContentType(keepUnquoted);
168170
return Task.FromResult(0);
169171
}, null);
170172

171-
172173
foreach (var pair in problem.GetCustomHeaders())
173174
{
174175
response.Headers[pair.Key] = pair.Value;
@@ -177,5 +178,12 @@ private static void WriteResponseHeaders(Problem problem, HttpRequest request, H
177178
// Set problem status code
178179
response.StatusCode = problem.status;
179180
}
181+
182+
private static String CreateProfileContentType(bool keepUnquoted = false)
183+
{
184+
var profile = keepUnquoted ? SchemaAttribute.Path + "problem.json"
185+
: $"\"{SchemaAttribute.Path?.Replace("\"", "\\\"")}" + "problem.json\"";
186+
return $"; profile={profile}";
187+
}
180188
}
181189
}

0 commit comments

Comments
 (0)