Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added error filter extensions #973

Merged
merged 2 commits into from
Aug 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
using System;
using HotChocolate.Execution;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace HotChocolate
{
public class SchemaServiceCollectionExtensionsTests
{
[Fact]
public void AddErrorFilter_1_Services_Is_Null()
{
// arrange
// act
Action action = () =>
SchemaServiceCollectionExtensions.AddErrorFilter<MyErrorFilter>(null);

// assert
Assert.Throws<ArgumentNullException>(action);
}

[Fact]
public void AddErrorFilter_1()
{
// arrange
var services = new ServiceCollection();

// act
SchemaServiceCollectionExtensions.AddErrorFilter<MyErrorFilter>(services);

// assert
Assert.Collection(services,
t =>
{
Assert.Equal(typeof(IErrorFilter), t.ServiceType);
Assert.Equal(typeof(MyErrorFilter), t.ImplementationType);
});
}

[Fact]
public void AddErrorFilter_2_Services_Is_Null()
{
// arrange
// act
Action action = () =>
SchemaServiceCollectionExtensions.AddErrorFilter(
null, error => error);

// assert
Assert.Throws<ArgumentNullException>(action);
}

[Fact]
public void AddErrorFilter_2_Filter_Is_Null()
{
// arrange
var services = new ServiceCollection();

// act
Action action = () =>
SchemaServiceCollectionExtensions.AddErrorFilter(
services, default(Func<IError, IError>));

// assert
Assert.Throws<ArgumentNullException>(action);
}

[Fact]
public void AddErrorFilter_2()
{
// arrange
var services = new ServiceCollection();
Func<IError, IError> filter = error => error;

// act
SchemaServiceCollectionExtensions.AddErrorFilter(services, filter);

// assert
Assert.Collection(services,
t =>
{
Assert.Equal(typeof(IErrorFilter), t.ServiceType);
Assert.IsType<FuncErrorFilterWrapper>(t.ImplementationInstance);
});
}

[Fact]
public void AddErrorFilter_3_Services_Is_Null()
{
// arrange
// act
Action action = () =>
SchemaServiceCollectionExtensions.AddErrorFilter(
null, sp => new MyErrorFilter());

// assert
Assert.Throws<ArgumentNullException>(action);
}

[Fact]
public void AddErrorFilter_3_Filter_Is_Null()
{
// arrange
var services = new ServiceCollection();

// act
Action action = () =>
SchemaServiceCollectionExtensions.AddErrorFilter(
services, default(Func<IServiceProvider, IErrorFilter>));

// assert
Assert.Throws<ArgumentNullException>(action);
}

[Fact]
public void AddErrorFilter_3()
{
// arrange
var services = new ServiceCollection();
Func<IServiceProvider, IErrorFilter> factory =
sp => sp.GetRequiredService<IErrorFilter>();

// act
SchemaServiceCollectionExtensions.AddErrorFilter(services, factory);

// assert
Assert.Collection(services,
t =>
{
Assert.Equal(typeof(IErrorFilter), t.ServiceType);
Assert.Equal(factory, t.ImplementationFactory);
});
}

public class MyErrorFilter
: IErrorFilter
{
public IError OnError(IError error)
{
throw new System.NotImplementedException();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,53 @@ public static IServiceCollection AddResponseStreamSerializer<T>(
.AddSingleton<IResponseStreamSerializer>(sp => factory(sp));
}

public static IServiceCollection AddErrorFilter(
this IServiceCollection services,
Func<IError, IError> errorFilter)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}

if (errorFilter == null)
{
throw new ArgumentNullException(nameof(errorFilter));
}

return services.AddSingleton<IErrorFilter>(
new FuncErrorFilterWrapper(errorFilter));
}

public static IServiceCollection AddErrorFilter(
this IServiceCollection services,
Func<IServiceProvider, IErrorFilter> factory)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}

if (factory == null)
{
throw new ArgumentNullException(nameof(factory));
}

return services.AddSingleton(factory);
}

public static IServiceCollection AddErrorFilter<T>(
this IServiceCollection services)
where T : class, IErrorFilter
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}

return services.AddSingleton<IErrorFilter, T>();
}

private static IServiceCollection RemoveService<TService>(
this IServiceCollection services)
{
Expand Down