Skip to content

Commit

Permalink
fixed some catch() extensions, and added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
icanhasjonas committed Sep 22, 2017
1 parent 5af7a49 commit e0ffd22
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 25 deletions.
119 changes: 119 additions & 0 deletions Then.Tests/CatchTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
using System;
using System.Threading.Tasks;
using Xunit;

namespace Then.Tests {
public class CatchTests {
public class CustomException : Exception {
public CustomException() : this( "Hello World" ) { }
public CustomException( string message ) : base( message ) { }
}

public class OtherException : Exception {
public OtherException() : this( "Hello World" ) { }
public OtherException( string message ) : base( message ) { }
}

[Fact]
public async Task CatchTestSimple()
{
await Task
.FromException( new CustomException() )
.Catch();
}

[Fact]
public async Task CatchTestSimple2()
{
var result = await Task
.FromException<string>( new CustomException() )
.Catch();

Assert.Null( result );
}


[Fact]
public async Task SimpleUntypedExceptionHandler()
{
var result = await Task
.FromException<string>( new CustomException() )
.Catch( e => "hello" );

Assert.Equal( "hello", result );
}

[Fact]
public async Task SimpleTypedExceptionHandler()
{
var result = await Task
.FromException<string>( new CustomException() )
.Catch<string, CustomException>( e => "hello" );

Assert.Equal( "hello", result );
}

[Fact]
public async Task SimpleUntypedAsyncExceptionHandler()
{
var result = await Task
.FromException<string>( new CustomException() )
.Catch( async e => "hello" );

Assert.Equal( "hello", result );
}

[Fact]
public async Task SimpleTypedAsyncExceptionHandler()
{
var result = await Task
.FromException<string>( new CustomException() )
.Catch<string, CustomException>( async e => "hello" );

Assert.Equal( "hello", result );
}

[Fact]
public async Task SimpleTypedExceptionHandlerWithNoResult()
{
await Task
.FromException( new CustomException() )
.Catch<CustomException>( e => { } );
}

[Fact]
public async Task SimpleUntypedExceptionHandlerWithNoResult()
{
await Task
.FromException( new CustomException() )
.Catch( e => { } );
}

[Fact]
public async Task SimpleTypedAsyncExceptionHandlerWithNoResult()
{
await Task
.FromException( new CustomException() )
.Catch<CustomException>( async e => { } );
}

[Fact]
public async Task SimpleUntypedAsyncExceptionHandlerWithNoResult()
{
await Task
.FromException( new CustomException() )
.Catch( async e => { } );
}


[Fact]
public async Task FilteredExceptionHandlerShouldNotCatchOtherExceptions()
{
await Assert.ThrowsAsync<OtherException>( async () => {
await Task
.FromException<string>( new OtherException() )
.Catch<string, CustomException>( e => "hello" );
} );
}
}
}
19 changes: 19 additions & 0 deletions Then.Tests/Then.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0-preview-20170628-02" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Then\Then.csproj" />
</ItemGroup>

</Project>
47 changes: 24 additions & 23 deletions Then/CatchTaskExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,66 @@

namespace System.Threading.Tasks {
public static class CatchTaskExtensions {
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Task<T> Catch<T>(this Task<T> task) where T : class => task.Catch(e => default(T));

private static readonly Action<Exception> s_emptyHandler = e => { };

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Task Catch(this Task task) => task.Catch(s_emptyHandler);

[MethodImpl( MethodImplOptions.AggressiveInlining )]
public static Task<T> Catch<T>( this Task<T> task, Func<Exception, Task<T>> exceptionHandler ) => task.Catch<T, Exception>( exceptionHandler );

[MethodImpl( MethodImplOptions.AggressiveInlining )]
public static async Task<T> Catch<T>( this Task<T> task, Func<Exception, Task<T>> exceptionHandler )
public static async Task<T> Catch<T, TException>( this Task<T> task, Func<TException, Task<T>> exceptionHandler ) where TException : Exception
{
try {
return await task.ConfigureAwait( false );
}
catch( Exception e ) {
catch( TException e ) {
return await exceptionHandler( e ).ConfigureAwait( false );
}
}

[MethodImpl( MethodImplOptions.AggressiveInlining )]
public static async Task<T> Catch<T>( this Task<T> task, Func<Exception, T> exceptionHandler )
public static Task<T> Catch<T>( this Task<T> task, Func<Exception, T> exceptionHandler ) => task.Catch<T, Exception>( exceptionHandler );

[MethodImpl( MethodImplOptions.AggressiveInlining )]
public static async Task<T> Catch<T, TException>( this Task<T> task, Func<TException, T> exceptionHandler ) where TException : Exception
{
try {
return await task.ConfigureAwait( false );
}
catch( Exception e ) {
catch( TException e ) {
return exceptionHandler( e );
}
}

[MethodImpl( MethodImplOptions.AggressiveInlining )]
public static Task<T> Catch<T>( this Task<T> task ) where T : class => task.Catch( e => default(T) );

private static readonly Action<Exception> s_emptyHandler = e => { };

[MethodImpl( MethodImplOptions.AggressiveInlining )]
public static Task Catch( this Task task ) => task.Catch( s_emptyHandler );
public static Task Catch( this Task task, Action<Exception> exceptionHandler ) => task.Catch<Exception>( exceptionHandler );

[MethodImpl( MethodImplOptions.AggressiveInlining )]
public static async Task Catch( this Task task, Action<Exception> exceptionHandler )
public static async Task Catch<TException>( this Task task, Action<TException> exceptionHandler ) where TException : Exception
{
try {
await task.ConfigureAwait( false );
}
catch( Exception e ) {
catch( TException e ) {
exceptionHandler( e );
}
}

[MethodImpl( MethodImplOptions.AggressiveInlining )]
public static async Task Catch<TException>( this Task task, Action<TException> exceptionHandler ) where TException : Exception
{
try {
await task.ConfigureAwait( false );
}
catch( Exception e ) when( e is TException ) {
exceptionHandler( (TException)e );
}
}
public static Task Catch( this Task task, Func<Exception, Task> exceptionHandler ) => task.Catch<Exception>( exceptionHandler );

[MethodImpl( MethodImplOptions.AggressiveInlining )]
public static async Task Catch( this Task task, Func<Exception, Task> exceptionHandler )
public static async Task Catch<TException>( this Task task, Func<TException, Task> exceptionHandler ) where TException : Exception
{
try {
await task.ConfigureAwait( false );
}
catch( Exception e ) {
catch( TException e ) {
await exceptionHandler( e ).ConfigureAwait( false );
}
}
Expand Down
2 changes: 1 addition & 1 deletion Then/Then.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<RepositoryType>git</RepositoryType>
<PackageProjectUrl>https://github.com/bmbsqd/then</PackageProjectUrl>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.0.1</Version>
<Version>1.0.3</Version>
</PropertyGroup>

</Project>
8 changes: 7 additions & 1 deletion task-extensions.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26730.12
VisualStudioVersion = 15.0.26730.16
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Then", "Then\Then.csproj", "{3E7D3193-4CD9-4107-A33D-61530E97FFFE}"
EndProject
Expand All @@ -10,6 +10,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
README.md = README.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Then.Tests", "Then.Tests\Then.Tests.csproj", "{CC4B0756-B317-45DE-87EE-C329FDBCC577}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -20,6 +22,10 @@ Global
{3E7D3193-4CD9-4107-A33D-61530E97FFFE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3E7D3193-4CD9-4107-A33D-61530E97FFFE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3E7D3193-4CD9-4107-A33D-61530E97FFFE}.Release|Any CPU.Build.0 = Release|Any CPU
{CC4B0756-B317-45DE-87EE-C329FDBCC577}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CC4B0756-B317-45DE-87EE-C329FDBCC577}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CC4B0756-B317-45DE-87EE-C329FDBCC577}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CC4B0756-B317-45DE-87EE-C329FDBCC577}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit e0ffd22

Please sign in to comment.