Skip to content

Commit a894f69

Browse files
committed
feat(#138): improved exception handling
1 parent ee0025f commit a894f69

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

src/JsonApiDotNetCore/Controllers/BaseJsonApiController.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ protected BaseJsonApiController(
7777

7878
public virtual async Task<IActionResult> GetAsync()
7979
{
80-
if (_getAll == null) throw new JsonApiException(405, "Query requests are not supported");
80+
if (_getAll == null) throw new JsonApiException(405, "Get requests are not supported");
8181

8282
var entities = await _getAll.GetAsync();
8383

@@ -86,7 +86,7 @@ public virtual async Task<IActionResult> GetAsync()
8686

8787
public virtual async Task<IActionResult> GetAsync(TId id)
8888
{
89-
if (_getById == null) throw new JsonApiException(405, "Query requests are not supported");
89+
if (_getById == null) throw new JsonApiException(405, "Get by Id requests are not supported");
9090

9191
var entity = await _getById.GetAsync(id);
9292

@@ -98,7 +98,7 @@ public virtual async Task<IActionResult> GetAsync(TId id)
9898

9999
public virtual async Task<IActionResult> GetRelationshipsAsync(TId id, string relationshipName)
100100
{
101-
if (_getRelationships == null) throw new JsonApiException(405, "Query requests are not supported");
101+
if (_getRelationships == null) throw new JsonApiException(405, "Get Relationships requests are not supported");
102102

103103
var relationship = await _getRelationships.GetRelationshipsAsync(id, relationshipName);
104104
if (relationship == null)
@@ -109,7 +109,7 @@ public virtual async Task<IActionResult> GetRelationshipsAsync(TId id, string re
109109

110110
public virtual async Task<IActionResult> GetRelationshipAsync(TId id, string relationshipName)
111111
{
112-
if (_getRelationship == null) throw new JsonApiException(405, "Query requests are not supported");
112+
if (_getRelationship == null) throw new JsonApiException(405, "Get Relationship requests are not supported");
113113

114114
var relationship = await _getRelationship.GetRelationshipAsync(id, relationshipName);
115115

@@ -118,7 +118,7 @@ public virtual async Task<IActionResult> GetRelationshipAsync(TId id, string rel
118118

119119
public virtual async Task<IActionResult> PostAsync([FromBody] T entity)
120120
{
121-
if (_create == null) throw new JsonApiException(405, "Command requests are not supported");
121+
if (_create == null) throw new JsonApiException(405, "Post requests are not supported");
122122

123123
if (entity == null)
124124
return UnprocessableEntity();
@@ -133,7 +133,7 @@ public virtual async Task<IActionResult> PostAsync([FromBody] T entity)
133133

134134
public virtual async Task<IActionResult> PatchAsync(TId id, [FromBody] T entity)
135135
{
136-
if (_update == null) throw new JsonApiException(405, "Command requests are not supported");
136+
if (_update == null) throw new JsonApiException(405, "Patch requests are not supported");
137137

138138
if (entity == null)
139139
return UnprocessableEntity();
@@ -148,7 +148,7 @@ public virtual async Task<IActionResult> PatchAsync(TId id, [FromBody] T entity)
148148

149149
public virtual async Task<IActionResult> PatchRelationshipsAsync(TId id, string relationshipName, [FromBody] List<DocumentData> relationships)
150150
{
151-
if (_updateRelationships == null) throw new JsonApiException(405, "Command requests are not supported");
151+
if (_updateRelationships == null) throw new JsonApiException(405, "Relationship Patch requests are not supported");
152152

153153
await _updateRelationships.UpdateRelationshipsAsync(id, relationshipName, relationships);
154154

@@ -157,7 +157,7 @@ public virtual async Task<IActionResult> PatchRelationshipsAsync(TId id, string
157157

158158
public virtual async Task<IActionResult> DeleteAsync(TId id)
159159
{
160-
if (_delete == null) throw new JsonApiException(405, "Command requests are not supported");
160+
if (_delete == null) throw new JsonApiException(405, "Delete requests are not supported");
161161

162162
var wasDeleted = await _delete.DeleteAsync(id);
163163

src/JsonApiDotNetCore/Internal/JsonApiException.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public JsonApiException(int statusCode, string message, string detail)
3434
: base(message)
3535
=> _errors.Add(new Error(statusCode, message, detail));
3636

37+
public JsonApiException(int statusCode, string message, Exception innerException)
38+
: base(message, innerException)
39+
=> _errors.Add(new Error(statusCode, message, innerException.Message));
40+
3741
public ErrorCollection GetError() => _errors;
3842

3943
public int GetStatusCode()

src/JsonApiDotNetCore/Serialization/JsonApiDeSerializer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public object Deserialize(string requestBody)
3434
}
3535
catch (Exception e)
3636
{
37-
throw new JsonApiException(400, "Failed to deserialize request body", e.Message);
37+
throw new JsonApiException(400, "Failed to deserialize request body", e);
3838
}
3939
}
4040

@@ -54,7 +54,7 @@ public object DeserializeRelationship(string requestBody)
5454
}
5555
catch (Exception e)
5656
{
57-
throw new JsonApiException(400, "Failed to deserialize request body", e.Message);
57+
throw new JsonApiException(400, "Failed to deserialize request body", e);
5858
}
5959
}
6060

@@ -75,7 +75,7 @@ public List<TEntity> DeserializeList<TEntity>(string requestBody)
7575
}
7676
catch (Exception e)
7777
{
78-
throw new JsonApiException(400, "Failed to deserialize request body", e.Message);
78+
throw new JsonApiException(400, "Failed to deserialize request body", e);
7979
}
8080
}
8181

0 commit comments

Comments
 (0)