Skip to content

Commit 38f15c3

Browse files
committed
feat(controllers): add errors response
1 parent 556a534 commit 38f15c3

File tree

5 files changed

+349
-0
lines changed

5 files changed

+349
-0
lines changed

JsonApiDotnetCore.sln

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NoEntityFrameworkExample",
2222
EndProject
2323
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NoEntityFrameworkTests", "test\NoEntityFrameworkTests\NoEntityFrameworkTests.csproj", "{73DA578D-A63F-4956-83ED-6D7102E09140}"
2424
EndProject
25+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTests", "test\UnitTests\UnitTests.csproj", "{6D4BD85A-A262-44C6-8572-FE3A30410BF3}"
26+
EndProject
2527
Global
2628
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2729
Debug|Any CPU = Debug|Any CPU
@@ -80,6 +82,18 @@ Global
8082
{73DA578D-A63F-4956-83ED-6D7102E09140}.Release|x64.Build.0 = Release|Any CPU
8183
{73DA578D-A63F-4956-83ED-6D7102E09140}.Release|x86.ActiveCfg = Release|Any CPU
8284
{73DA578D-A63F-4956-83ED-6D7102E09140}.Release|x86.Build.0 = Release|Any CPU
85+
{6D4BD85A-A262-44C6-8572-FE3A30410BF3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
86+
{6D4BD85A-A262-44C6-8572-FE3A30410BF3}.Debug|Any CPU.Build.0 = Debug|Any CPU
87+
{6D4BD85A-A262-44C6-8572-FE3A30410BF3}.Debug|x64.ActiveCfg = Debug|x64
88+
{6D4BD85A-A262-44C6-8572-FE3A30410BF3}.Debug|x64.Build.0 = Debug|x64
89+
{6D4BD85A-A262-44C6-8572-FE3A30410BF3}.Debug|x86.ActiveCfg = Debug|x86
90+
{6D4BD85A-A262-44C6-8572-FE3A30410BF3}.Debug|x86.Build.0 = Debug|x86
91+
{6D4BD85A-A262-44C6-8572-FE3A30410BF3}.Release|Any CPU.ActiveCfg = Release|Any CPU
92+
{6D4BD85A-A262-44C6-8572-FE3A30410BF3}.Release|Any CPU.Build.0 = Release|Any CPU
93+
{6D4BD85A-A262-44C6-8572-FE3A30410BF3}.Release|x64.ActiveCfg = Release|x64
94+
{6D4BD85A-A262-44C6-8572-FE3A30410BF3}.Release|x64.Build.0 = Release|x64
95+
{6D4BD85A-A262-44C6-8572-FE3A30410BF3}.Release|x86.ActiveCfg = Release|x86
96+
{6D4BD85A-A262-44C6-8572-FE3A30410BF3}.Release|x86.Build.0 = Release|x86
8397
EndGlobalSection
8498
GlobalSection(SolutionProperties) = preSolution
8599
HideSolutionNode = FALSE
@@ -90,5 +104,6 @@ Global
90104
{0B959765-40D2-43B5-87EE-FE2FEF9DBED5} = {24B15015-62E5-42E1-9BA0-ECE6BE7AA15F}
91105
{570165EC-62B5-4684-A139-8D2A30DD4475} = {7A2B7ADD-ECB5-4D00-AA6A-D45BD11C97CF}
92106
{73DA578D-A63F-4956-83ED-6D7102E09140} = {24B15015-62E5-42E1-9BA0-ECE6BE7AA15F}
107+
{6D4BD85A-A262-44C6-8572-FE3A30410BF3} = {24B15015-62E5-42E1-9BA0-ECE6BE7AA15F}
93108
EndGlobalSection
94109
EndGlobal

src/JsonApiDotNetCore/Controllers/JsonApiControllerMixin.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Linq;
2+
using JsonApiDotNetCore.Internal;
13
using Microsoft.AspNetCore.Mvc;
24

35
namespace JsonApiDotNetCore.Controllers
@@ -13,5 +15,26 @@ protected IActionResult Forbidden()
1315
{
1416
return new StatusCodeResult(403);
1517
}
18+
19+
protected IActionResult Errors(ErrorCollection errors)
20+
{
21+
var result = new ObjectResult(errors);
22+
result.StatusCode = GetErrorStatusCode(errors);
23+
24+
return result;
25+
}
26+
27+
private int GetErrorStatusCode(ErrorCollection errors)
28+
{
29+
var statusCodes = errors.Errors
30+
.Select(e => (int)e.StatusCode)
31+
.Distinct()
32+
.ToList();
33+
34+
if(statusCodes.Count == 1)
35+
return statusCodes[0];
36+
37+
return int.Parse(statusCodes.Max().ToString()[0] + "00");
38+
}
1639
}
1740
}

test/UnitTests/.gitignore

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
## Ignore Visual Studio temporary files, build results, and
2+
## files generated by popular Visual Studio add-ons.
3+
4+
# User-specific files
5+
*.suo
6+
*.user
7+
*.userosscache
8+
*.sln.docstates
9+
10+
# User-specific files (MonoDevelop/Xamarin Studio)
11+
*.userprefs
12+
13+
# Build results
14+
[Dd]ebug/
15+
[Dd]ebugPublic/
16+
[Rr]elease/
17+
[Rr]eleases/
18+
x64/
19+
x86/
20+
build/
21+
bld/
22+
[Bb]in/
23+
[Oo]bj/
24+
25+
# Visual Studio 2015 cache/options directory
26+
.vs/
27+
# Uncomment if you have tasks that create the project's static files in wwwroot
28+
#wwwroot/
29+
30+
# MSTest test Results
31+
[Tt]est[Rr]esult*/
32+
[Bb]uild[Ll]og.*
33+
34+
# NUNIT
35+
*.VisualState.xml
36+
TestResult.xml
37+
38+
# Build Results of an ATL Project
39+
[Dd]ebugPS/
40+
[Rr]eleasePS/
41+
dlldata.c
42+
43+
# DNX
44+
project.lock.json
45+
artifacts/
46+
47+
*_i.c
48+
*_p.c
49+
*_i.h
50+
*.ilk
51+
*.meta
52+
*.obj
53+
*.pch
54+
*.pdb
55+
*.pgc
56+
*.pgd
57+
*.rsp
58+
*.sbr
59+
*.tlb
60+
*.tli
61+
*.tlh
62+
*.tmp
63+
*.tmp_proj
64+
*.log
65+
*.vspscc
66+
*.vssscc
67+
.builds
68+
*.pidb
69+
*.svclog
70+
*.scc
71+
72+
# Chutzpah Test files
73+
_Chutzpah*
74+
75+
# Visual C++ cache files
76+
ipch/
77+
*.aps
78+
*.ncb
79+
*.opendb
80+
*.opensdf
81+
*.sdf
82+
*.cachefile
83+
84+
# Visual Studio profiler
85+
*.psess
86+
*.vsp
87+
*.vspx
88+
*.sap
89+
90+
# TFS 2012 Local Workspace
91+
$tf/
92+
93+
# Guidance Automation Toolkit
94+
*.gpState
95+
96+
# ReSharper is a .NET coding add-in
97+
_ReSharper*/
98+
*.[Rr]e[Ss]harper
99+
*.DotSettings.user
100+
101+
# JustCode is a .NET coding add-in
102+
.JustCode
103+
104+
# TeamCity is a build add-in
105+
_TeamCity*
106+
107+
# DotCover is a Code Coverage Tool
108+
*.dotCover
109+
110+
# NCrunch
111+
_NCrunch_*
112+
.*crunch*.local.xml
113+
nCrunchTemp_*
114+
115+
# MightyMoose
116+
*.mm.*
117+
AutoTest.Net/
118+
119+
# Web workbench (sass)
120+
.sass-cache/
121+
122+
# Installshield output folder
123+
[Ee]xpress/
124+
125+
# DocProject is a documentation generator add-in
126+
DocProject/buildhelp/
127+
DocProject/Help/*.HxT
128+
DocProject/Help/*.HxC
129+
DocProject/Help/*.hhc
130+
DocProject/Help/*.hhk
131+
DocProject/Help/*.hhp
132+
DocProject/Help/Html2
133+
DocProject/Help/html
134+
135+
# Click-Once directory
136+
publish/
137+
138+
# Publish Web Output
139+
*.[Pp]ublish.xml
140+
*.azurePubxml
141+
# TODO: Comment the next line if you want to checkin your web deploy settings
142+
# but database connection strings (with potential passwords) will be unencrypted
143+
*.pubxml
144+
*.publishproj
145+
146+
# NuGet Packages
147+
*.nupkg
148+
# The packages folder can be ignored because of Package Restore
149+
**/packages/*
150+
# except build/, which is used as an MSBuild target.
151+
!**/packages/build/
152+
# Uncomment if necessary however generally it will be regenerated when needed
153+
#!**/packages/repositories.config
154+
155+
# Microsoft Azure Build Output
156+
csx/
157+
*.build.csdef
158+
159+
# Microsoft Azure Emulator
160+
ecf/
161+
rcf/
162+
163+
# Microsoft Azure ApplicationInsights config file
164+
ApplicationInsights.config
165+
166+
# Windows Store app package directory
167+
AppPackages/
168+
BundleArtifacts/
169+
170+
# Visual Studio cache files
171+
# files ending in .cache can be ignored
172+
*.[Cc]ache
173+
# but keep track of directories ending in .cache
174+
!*.[Cc]ache/
175+
176+
# Others
177+
ClientBin/
178+
~$*
179+
*~
180+
*.dbmdl
181+
*.dbproj.schemaview
182+
*.pfx
183+
*.publishsettings
184+
node_modules/
185+
orleans.codegen.cs
186+
187+
# RIA/Silverlight projects
188+
Generated_Code/
189+
190+
# Backup & report files from converting an old project file
191+
# to a newer Visual Studio version. Backup files are not needed,
192+
# because we have git ;-)
193+
_UpgradeReport_Files/
194+
Backup*/
195+
UpgradeLog*.XML
196+
UpgradeLog*.htm
197+
198+
# SQL Server files
199+
*.mdf
200+
*.ldf
201+
202+
# Business Intelligence projects
203+
*.rdl.data
204+
*.bim.layout
205+
*.bim_*.settings
206+
207+
# Microsoft Fakes
208+
FakesAssemblies/
209+
210+
# GhostDoc plugin setting file
211+
*.GhostDoc.xml
212+
213+
# Node.js Tools for Visual Studio
214+
.ntvs_analysis.dat
215+
216+
# Visual Studio 6 build log
217+
*.plg
218+
219+
# Visual Studio 6 workspace options file
220+
*.opt
221+
222+
# Visual Studio LightSwitch build output
223+
**/*.HTMLClient/GeneratedArtifacts
224+
**/*.DesktopClient/GeneratedArtifacts
225+
**/*.DesktopClient/ModelManifest.xml
226+
**/*.Server/GeneratedArtifacts
227+
**/*.Server/ModelManifest.xml
228+
_Pvt_Extensions
229+
230+
# Paket dependency manager
231+
.paket/paket.exe
232+
233+
# FAKE - F# Make
234+
.fake/
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using JsonApiDotNetCore.Controllers;
4+
using JsonApiDotNetCore.Internal;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Xunit;
7+
8+
namespace UnitTests
9+
{
10+
public class JsonApiControllerMixin_Tests : JsonApiControllerMixin
11+
{
12+
13+
[Fact]
14+
public void Errors_Correctly_Infers_Status_Code()
15+
{
16+
// arrange
17+
var errors422 = new ErrorCollection {
18+
Errors = new List<Error> {
19+
new Error("422", "bad specific"),
20+
new Error("422", "bad other specific"),
21+
}
22+
};
23+
24+
var errors400 = new ErrorCollection {
25+
Errors = new List<Error> {
26+
new Error("200", "weird"),
27+
new Error("400", "bad"),
28+
new Error("422", "bad specific"),
29+
}
30+
};
31+
32+
var errors500 = new ErrorCollection {
33+
Errors = new List<Error> {
34+
new Error("200", "weird"),
35+
new Error("400", "bad"),
36+
new Error("422", "bad specific"),
37+
new Error("500", "really bad"),
38+
new Error("502", "really bad specific"),
39+
}
40+
};
41+
42+
43+
// act
44+
var result422 = this.Errors(errors422);
45+
var result400 = this.Errors(errors400);
46+
var result500 = this.Errors(errors500);
47+
48+
// assert
49+
var response422 = Assert.IsType<ObjectResult>(result422);
50+
var response400 = Assert.IsType<ObjectResult>(result400);
51+
var response500 = Assert.IsType<ObjectResult>(result500);
52+
53+
Assert.Equal(422, response422.StatusCode);
54+
Assert.Equal(400, response400.StatusCode);
55+
Assert.Equal(500, response500.StatusCode);
56+
}
57+
}
58+
}

test/UnitTests/UnitTests.csproj

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.0</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0-preview-20170427-09" />
11+
<PackageReference Include="xunit" Version="2.2.0" />
12+
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<ProjectReference Include="..\..\src\JsonApiDotNetCore\JsonApiDotNetCore.csproj" />
17+
</ItemGroup>
18+
19+
</Project>

0 commit comments

Comments
 (0)