Skip to content

Commit a880f44

Browse files
author
IharYakimush
authored
Merge pull request #2 from IharYakimush/develop
2.0.2
2 parents 1ea347b + 5a0d175 commit a880f44

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+552
-152
lines changed

Community.AspNetCore.ExceptionHandling.Integration/Controllers/ValuesController.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Data;
4-
using System.Linq;
5-
using System.Threading.Tasks;
64
using Microsoft.AspNetCore.Mvc;
75

8-
namespace Commmunity.AspNetCore.ExceptionHandling.Integration.Controllers
6+
namespace Community.AspNetCore.ExceptionHandling.Integration.Controllers
97
{
108
[Route("api/[controller]")]
119
public class ValuesController : Controller

Community.AspNetCore.ExceptionHandling.Integration/Program.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.IO;
4-
using System.Linq;
5-
using System.Threading.Tasks;
6-
using Microsoft.AspNetCore;
1+
using Microsoft.AspNetCore;
72
using Microsoft.AspNetCore.Hosting;
8-
using Microsoft.Extensions.Configuration;
9-
using Microsoft.Extensions.Logging;
103

11-
namespace Commmunity.AspNetCore.ExceptionHandling.Integration
4+
namespace Community.AspNetCore.ExceptionHandling.Integration
125
{
136
public class Program
147
{

Community.AspNetCore.ExceptionHandling.Integration/Startup.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Data;
4-
using System.IO;
5-
using System.Linq;
63
using System.Text;
7-
using System.Threading.Tasks;
8-
using Commmunity.AspNetCore.ExceptionHandling.Mvc;
4+
using Community.AspNetCore.ExceptionHandling.Mvc;
95
using Microsoft.AspNetCore.Builder;
106
using Microsoft.AspNetCore.Hosting;
117
using Microsoft.Extensions.Configuration;
128
using Microsoft.Extensions.DependencyInjection;
13-
using Microsoft.Extensions.Logging;
14-
using Microsoft.Extensions.Options;
159

16-
namespace Commmunity.AspNetCore.ExceptionHandling.Integration
10+
namespace Community.AspNetCore.ExceptionHandling.Integration
1711
{
1812
public class Startup
1913
{
@@ -40,7 +34,11 @@ public void ConfigureServices(IServiceCollection services)
4034
options.For<InvalidCastException>()
4135
.Response(e => 400)
4236
.Headers((h, e) => h["X-qwe"] = e.Message)
43-
.WithBody((req,sw, exception) => sw.WriteAsync(exception.ToString()))
37+
.WithBody((req,sw, exception) =>
38+
{
39+
byte[] array = Encoding.UTF8.GetBytes(exception.ToString());
40+
return sw.WriteAsync(array, 0, array.Length);
41+
})
4442
.NextPolicy();
4543

4644
options.For<Exception>()
@@ -56,7 +54,8 @@ public void ConfigureServices(IServiceCollection services)
5654
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
5755
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
5856
{
59-
app.UseResponseBuffering().UseDeveloperExceptionPage().UseExceptionHandlingPolicies();
57+
//app.UseResponseBuffering();
58+
app.UseDeveloperExceptionPage().UseExceptionHandlingPolicies();
6059
app.UseMvc();
6160
}
6261
}

Community.AspNetCore.ExceptionHandling.Mvc/Community.AspNetCore.ExceptionHandling.Mvc.csproj

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
<PropertyGroup>
44
<TargetFramework>netcoreapp2.1</TargetFramework>
55
<Description>Extension methods to configure exception handler which write MVC action result to responce body. Userfull for writing objects</Description>
6-
<PackageProjectUrl>https://github.com/IharYakimush/AspNetCore</PackageProjectUrl>
7-
<PackageLicenseUrl>https://github.com/IharYakimush/AspNetCore/blob/develop/LICENSE</PackageLicenseUrl>
6+
<PackageProjectUrl>https://github.com/IharYakimush/asp-net-core-exception-handling</PackageProjectUrl>
7+
<PackageLicenseUrl>https://github.com/IharYakimush/asp-net-core-exception-handling/blob/develop/LICENSE</PackageLicenseUrl>
88
<Copyright>IharYakimush</Copyright>
9-
<PackageTags>AspNetCore exception handling policy mvc action result</PackageTags>
10-
<AssemblyVersion>2.0.0.0</AssemblyVersion>
9+
<PackageTags>aspnetcore exception handling policy mvc action result</PackageTags>
10+
<AssemblyVersion>2.0.2.0</AssemblyVersion>
1111
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
1212
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
13-
<FileVersion>2.0.0.0</FileVersion>
13+
<FileVersion>2.0.2.0</FileVersion>
1414
<Company />
1515
<Authors>IharYakimush</Authors>
16-
<Version>2.0.0</Version>
16+
<Version>2.0.2</Version>
1717
<SignAssembly>true</SignAssembly>
1818
<AssemblyOriginatorKeyFile>..\sgn.snk</AssemblyOriginatorKeyFile>
1919
<ApplicationIcon />

Community.AspNetCore.ExceptionHandling.Mvc/PolicyBuilderExtensions.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
using System;
2-
using Commmunity.AspNetCore.ExceptionHandling.Builder;
2+
using Community.AspNetCore.ExceptionHandling.Builder;
33
using Microsoft.AspNetCore.Http;
44
using Microsoft.AspNetCore.Mvc;
55
using Microsoft.AspNetCore.Mvc.Abstractions;
66
using Microsoft.AspNetCore.Mvc.Infrastructure;
77
using Microsoft.AspNetCore.Routing;
88
using Microsoft.Extensions.DependencyInjection;
99

10-
namespace Commmunity.AspNetCore.ExceptionHandling.Mvc
10+
namespace Community.AspNetCore.ExceptionHandling.Mvc
1111
{
1212
public static class PolicyBuilderExtensions
1313
{
@@ -85,7 +85,7 @@ public static IResponseHandlers<TException> WithActionResult<TException, TResult
8585
where TException : Exception
8686
where TResult : IActionResult
8787
{
88-
return builder.WithActionResult((request, exception) => result);
88+
return WithActionResult(builder, (request, exception) => result);
8989
}
9090

9191
/// <summary>
@@ -113,7 +113,7 @@ public static IResponseHandlers<TException> WithObjectResult<TException, TObject
113113
this IResponseHandlers<TException> builder, TObject value, int index = -1)
114114
where TException : Exception
115115
{
116-
return builder.WithActionResult(new ObjectResult(value), index);
116+
return WithActionResult(builder, new ObjectResult(value), index);
117117
}
118118

119119
/// <summary>
@@ -141,8 +141,7 @@ public static IResponseHandlers<TException> WithObjectResult<TException, TObject
141141
this IResponseHandlers<TException> builder, Func<HttpRequest, TException, TObject> valueFactory, int index = -1)
142142
where TException : Exception
143143
{
144-
return builder.WithActionResult(
145-
(request, exception) => new ObjectResult(valueFactory(request, exception)), index);
144+
return WithActionResult(builder, (request, exception) => new ObjectResult(valueFactory(request, exception)), index);
146145
}
147146
}
148147
}

Community.AspNetCore.ExceptionHandling.NewtonsoftJson/Community.AspNetCore.ExceptionHandling.NewtonsoftJson.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
<Description>Extension methods to configure exception handler which write object serialized using Newtonsoft.Json serializer to responce body. In case of using netcore2.1+ use Commmunity.AspNetCore.ExceptionHandling.Mvc package instead</Description>
66
<Authors>IharYakimush</Authors>
77
<Copyright>IharYakimush</Copyright>
8-
<PackageLicenseUrl>https://github.com/IharYakimush/AspNetCore/blob/develop/LICENSE</PackageLicenseUrl>
9-
<PackageProjectUrl>https://github.com/IharYakimush/AspNetCore</PackageProjectUrl>
10-
<PackageTags>AspNetCore exception handling policy json response</PackageTags>
11-
<AssemblyVersion>2.0.0.0</AssemblyVersion>
12-
<Version>2.0.0</Version>
8+
<PackageLicenseUrl>https://github.com/IharYakimush/asp-net-core-exception-handling/blob/develop/LICENSE</PackageLicenseUrl>
9+
<PackageProjectUrl>https://github.com/IharYakimush/asp-net-core-exception-handling</PackageProjectUrl>
10+
<PackageTags>aspnetcore exception handling policy json response</PackageTags>
11+
<AssemblyVersion>2.0.2.0</AssemblyVersion>
12+
<Version>2.0.2</Version>
1313
<Company />
1414
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
1515
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>

Community.AspNetCore.ExceptionHandling.NewtonsoftJson/PolicyBuilderExtensions.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
using System;
22
using System.IO;
3+
using System.Text;
34
using System.Threading.Tasks;
4-
using Commmunity.AspNetCore.ExceptionHandling.Builder;
5+
using Community.AspNetCore.ExceptionHandling.Builder;
56
using Microsoft.AspNetCore.Http;
67
using Microsoft.Extensions.DependencyInjection;
78
using Microsoft.Net.Http.Headers;
89
using Newtonsoft.Json;
910

10-
namespace Commmunity.AspNetCore.ExceptionHandling.NewtonsoftJson
11+
namespace Community.AspNetCore.ExceptionHandling.NewtonsoftJson
1112
{
1213
public static class PolicyBuilderExtensions
1314
{
@@ -35,12 +36,12 @@ public static class PolicyBuilderExtensions
3536
/// <returns>
3637
/// Policy builder
3738
/// </returns>
38-
[Obsolete("In case of using netcore2.1+ use Commmunity.AspNetCore.ExceptionHandling.Mvc instead")]
39+
[Obsolete("In case of using netcore2.1+ use Community.AspNetCore.ExceptionHandling.Mvc instead")]
3940
public static IResponseHandlers<TException> WithBodyJson<TException, TObject>(
4041
this IResponseHandlers<TException> builder, Func<HttpRequest, TException, TObject> valueFactory, JsonSerializerSettings settings = null, int index = -1)
4142
where TException : Exception
4243
{
43-
return builder.WithBody((request, streamWriter, exception) =>
44+
return builder.WithBody((request, stream, exception) =>
4445
{
4546
if (settings == null)
4647
{
@@ -65,15 +66,14 @@ public static IResponseHandlers<TException> WithBodyJson<TException, TObject>(
6566
//return Task.CompletedTask;
6667
using (MemoryStream ms = new MemoryStream())
6768
{
68-
using (StreamWriter sw = new StreamWriter(ms, streamWriter.Encoding, 1024, true))
69+
using (StreamWriter sw = new StreamWriter(ms, Encoding.UTF8, 1024, true))
6970
{
7071
jsonSerializer.Serialize(sw, val);
7172
}
7273

7374
ms.Seek(0, SeekOrigin.Begin);
7475
byte[] array = ms.ToArray();
75-
BinaryWriter binaryWriter = new BinaryWriter(streamWriter.BaseStream, streamWriter.Encoding, true);
76-
binaryWriter.Write(array);
76+
stream.WriteAsync(array, 0, array.Length);
7777

7878
return Task.CompletedTask;
7979
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.1</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.2" />
11+
<PackageReference Include="Microsoft.AspNetCore.Buffering" Version="0.2.2" />
12+
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="2.1.1" />
13+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.0" />
14+
<PackageReference Include="Moq" Version="4.9.0" />
15+
<PackageReference Include="xunit" Version="2.3.1" />
16+
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
17+
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
18+
</ItemGroup>
19+
20+
<ItemGroup>
21+
<ProjectReference Include="..\Community.AspNetCore.ExceptionHandling.Mvc\Community.AspNetCore.ExceptionHandling.Mvc.csproj" />
22+
<ProjectReference Include="..\Community.AspNetCore.ExceptionHandling.NewtonsoftJson\Community.AspNetCore.ExceptionHandling.NewtonsoftJson.csproj" />
23+
<ProjectReference Include="..\Community.AspNetCore.ExceptionHandling\Community.AspNetCore.ExceptionHandling.csproj" />
24+
</ItemGroup>
25+
26+
</Project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Threading.Tasks;
2+
using Community.AspNetCore.ExceptionHandling.Exc;
3+
using Community.AspNetCore.ExceptionHandling.Handlers;
4+
using Xunit;
5+
6+
namespace Community.AspNetCore.ExceptionHandling.Tests.Exc
7+
{
8+
9+
public class ReThrowExceptionHandlerTests
10+
{
11+
[Fact]
12+
public async Task AlwaysReThrowResult()
13+
{
14+
ReThrowExceptionHandler handler = new ReThrowExceptionHandler();
15+
HandlerResult result = await handler.Handle(null, null);
16+
17+
Assert.Equal(HandlerResult.ReThrow, result);
18+
}
19+
}
20+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.Threading.Tasks;
2+
using Community.AspNetCore.ExceptionHandling.Handlers;
3+
using Xunit;
4+
5+
namespace Community.AspNetCore.ExceptionHandling.Tests.Handlers
6+
{
7+
public class MarkHandledHandlerTests
8+
{
9+
[Fact]
10+
public async Task AlwaysHandledResult()
11+
{
12+
MarkHandledHandler handler = new MarkHandledHandler();
13+
HandlerResult result = await handler.Handle(null, null);
14+
15+
Assert.Equal(HandlerResult.Handled, result);
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)