Skip to content

Commit d92b3b9

Browse files
author
Bart Koelman
committed
Include request body in logged exception when available
1 parent 6fae6f8 commit d92b3b9

File tree

4 files changed

+25
-22
lines changed

4 files changed

+25
-22
lines changed
Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Net;
3+
using System.Text;
34
using JsonApiDotNetCore.Models.JsonApiDocuments;
45

56
namespace JsonApiDotNetCore.Exceptions
@@ -9,30 +10,32 @@ namespace JsonApiDotNetCore.Exceptions
910
/// </summary>
1011
public sealed class InvalidRequestBodyException : JsonApiException
1112
{
12-
public InvalidRequestBodyException(string reason)
13-
: this(reason, null, null)
14-
{
15-
}
16-
17-
public InvalidRequestBodyException(string reason, string details)
18-
: this(reason, details, null)
19-
{
20-
}
21-
22-
public InvalidRequestBodyException(Exception innerException)
23-
: this(null, null, innerException)
24-
{
25-
}
26-
27-
private InvalidRequestBodyException(string reason, string details = null, Exception innerException = null)
13+
public InvalidRequestBodyException(string reason, string details, string requestBody, Exception innerException = null)
2814
: base(new Error(HttpStatusCode.UnprocessableEntity)
2915
{
3016
Title = reason != null
3117
? "Failed to deserialize request body: " + reason
3218
: "Failed to deserialize request body.",
33-
Detail = details ?? innerException?.Message
19+
Detail = FormatDetails(details, requestBody, innerException)
3420
}, innerException)
3521
{
3622
}
23+
24+
private static string FormatDetails(string details, string requestBody, Exception innerException)
25+
{
26+
string text = details ?? innerException?.Message;
27+
28+
if (requestBody != null)
29+
{
30+
if (text != null)
31+
{
32+
text += Environment.NewLine;
33+
}
34+
35+
text += "Request body: <<" + requestBody + ">>";
36+
}
37+
38+
return text;
39+
}
3740
}
3841
}

src/JsonApiDotNetCore/Formatters/JsonApiReader.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ public async Task<InputFormatterResult> ReadAsync(InputFormatterContext context)
5050
}
5151
catch (Exception exception)
5252
{
53-
throw new InvalidRequestBodyException(exception);
53+
throw new InvalidRequestBodyException(null, null, body, exception);
5454
}
5555

5656
if (context.HttpContext.Request.Method == "PATCH")
5757
{
5858
var hasMissingId = model is IList list ? CheckForId(list) : CheckForId(model);
5959
if (hasMissingId)
6060
{
61-
throw new InvalidRequestBodyException("Payload must include id attribute.");
61+
throw new InvalidRequestBodyException("Payload must include id attribute.", null, body);
6262
}
6363
}
6464

src/JsonApiDotNetCore/Serialization/Common/BaseDocumentParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ private IIdentifiable ParseResourceObject(ResourceObject data)
138138
throw new InvalidRequestBodyException("Payload includes unknown resource type.",
139139
$"The resource '{data.Type}' is not registered on the resource graph. " +
140140
"If you are using Entity Framework, make sure the DbSet matches the expected resource name. " +
141-
"If you have manually registered the resource, check that the call to AddResource correctly sets the public name.");
141+
"If you have manually registered the resource, check that the call to AddResource correctly sets the public name.", null);
142142
}
143143

144144
var entity = resourceContext.ResourceType.New<IIdentifiable>();

test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/UpdatingDataTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public async Task Response422IfUpdatingNotSettableAttribute()
8888
var error = document.Errors.Single();
8989
Assert.Equal(HttpStatusCode.UnprocessableEntity, error.StatusCode);
9090
Assert.Equal("Failed to deserialize request body.", error.Title);
91-
Assert.Equal("Property set method not found.", error.Detail);
91+
Assert.StartsWith("Property set method not found." + Environment.NewLine + "Request body: <<", error.Detail);
9292
}
9393

9494
[Fact]
@@ -145,7 +145,7 @@ public async Task Respond_422_If_IdNotInAttributeList()
145145
var error = document.Errors.Single();
146146
Assert.Equal(HttpStatusCode.UnprocessableEntity, error.StatusCode);
147147
Assert.Equal("Failed to deserialize request body: Payload must include id attribute.", error.Title);
148-
Assert.Null(error.Detail);
148+
Assert.StartsWith("Request body: <<", error.Detail);
149149
}
150150

151151
[Fact]

0 commit comments

Comments
 (0)