Skip to content

Commit 7d7add2

Browse files
committed
add static exception UnSupportedRequestMethod
1 parent 265dc0e commit 7d7add2

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

src/JsonApiDotNetCore/Controllers/BaseJsonApiController.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using JsonApiDotNetCore.Models;
55
using JsonApiDotNetCore.Services;
66
using Microsoft.AspNetCore.Mvc;
7-
using Microsoft.Extensions.Logging;
87

98
namespace JsonApiDotNetCore.Controllers
109
{
@@ -105,7 +104,7 @@ public BaseJsonApiController(
105104

106105
public virtual async Task<IActionResult> GetAsync()
107106
{
108-
if (_getAll == null) throw new JsonApiException(405, "Get requests are not supported");
107+
if (_getAll == null) throw Exceptions.UnSupportedRequestMethod;
109108

110109
var entities = await _getAll.GetAsync();
111110

@@ -114,7 +113,7 @@ public virtual async Task<IActionResult> GetAsync()
114113

115114
public virtual async Task<IActionResult> GetAsync(TId id)
116115
{
117-
if (_getById == null) throw new JsonApiException(405, "Get by Id requests are not supported");
116+
if (_getById == null) throw Exceptions.UnSupportedRequestMethod;
118117

119118
var entity = await _getById.GetAsync(id);
120119

@@ -126,7 +125,7 @@ public virtual async Task<IActionResult> GetAsync(TId id)
126125

127126
public virtual async Task<IActionResult> GetRelationshipsAsync(TId id, string relationshipName)
128127
{
129-
if (_getRelationships == null) throw new JsonApiException(405, "Get Relationships requests are not supported");
128+
if (_getRelationships == null) throw Exceptions.UnSupportedRequestMethod;
130129

131130
var relationship = await _getRelationships.GetRelationshipsAsync(id, relationshipName);
132131
if (relationship == null)
@@ -137,7 +136,7 @@ public virtual async Task<IActionResult> GetRelationshipsAsync(TId id, string re
137136

138137
public virtual async Task<IActionResult> GetRelationshipAsync(TId id, string relationshipName)
139138
{
140-
if (_getRelationship == null) throw new JsonApiException(405, "Get Relationship requests are not supported");
139+
if (_getRelationship == null) throw Exceptions.UnSupportedRequestMethod;
141140

142141
var relationship = await _getRelationship.GetRelationshipAsync(id, relationshipName);
143142

@@ -146,7 +145,7 @@ public virtual async Task<IActionResult> GetRelationshipAsync(TId id, string rel
146145

147146
public virtual async Task<IActionResult> PostAsync([FromBody] T entity)
148147
{
149-
if (_create == null) throw new JsonApiException(405, "Post requests are not supported");
148+
if (_create == null) throw Exceptions.UnSupportedRequestMethod;
150149

151150
if (entity == null)
152151
return UnprocessableEntity();
@@ -161,7 +160,7 @@ public virtual async Task<IActionResult> PostAsync([FromBody] T entity)
161160

162161
public virtual async Task<IActionResult> PatchAsync(TId id, [FromBody] T entity)
163162
{
164-
if (_update == null) throw new JsonApiException(405, "Patch requests are not supported");
163+
if (_update == null) throw Exceptions.UnSupportedRequestMethod;
165164

166165
if (entity == null)
167166
return UnprocessableEntity();
@@ -176,7 +175,7 @@ public virtual async Task<IActionResult> PatchAsync(TId id, [FromBody] T entity)
176175

177176
public virtual async Task<IActionResult> PatchRelationshipsAsync(TId id, string relationshipName, [FromBody] List<DocumentData> relationships)
178177
{
179-
if (_updateRelationships == null) throw new JsonApiException(405, "Relationship Patch requests are not supported");
178+
if (_updateRelationships == null) throw Exceptions.UnSupportedRequestMethod;
180179

181180
await _updateRelationships.UpdateRelationshipsAsync(id, relationshipName, relationships);
182181

@@ -185,7 +184,7 @@ public virtual async Task<IActionResult> PatchRelationshipsAsync(TId id, string
185184

186185
public virtual async Task<IActionResult> DeleteAsync(TId id)
187186
{
188-
if (_delete == null) throw new JsonApiException(405, "Delete requests are not supported");
187+
if (_delete == null) throw Exceptions.UnSupportedRequestMethod;
189188

190189
var wasDeleted = await _delete.DeleteAsync(id);
191190

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace JsonApiDotNetCore.Internal
2+
{
3+
internal static class Exceptions
4+
{
5+
private const string DOCUMENTATION_URL = "https://json-api-dotnet.github.io/#/errors/";
6+
private static string BuildUrl(string title) => DOCUMENTATION_URL + title;
7+
8+
public static JsonApiException UnSupportedRequestMethod { get; }
9+
= new JsonApiException(405, "Request method is not supported.", BuildUrl(nameof(UnSupportedRequestMethod)));
10+
}
11+
}

0 commit comments

Comments
 (0)