Skip to content

Commit

Permalink
Added new ServiceExceptionHandler to IAppHost, use that to handle exc…
Browse files Browse the repository at this point in the history
…eptions if it exists.

More Reqstars tests - with Typed + Rest client API examples
  • Loading branch information
mythz committed Sep 21, 2012
1 parent 727b76a commit 220ad68
Show file tree
Hide file tree
Showing 15 changed files with 233 additions and 53 deletions.
8 changes: 7 additions & 1 deletion src/ServiceStack.ServiceInterface/ServiceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,14 @@ public object Execute(TRequest request)
/// <returns></returns>
protected virtual object HandleException(TRequest request, Exception ex)
{
var errorResponse = ErrorHandler.Instance.HandleException(GetAppHost(), request, ex);
var useAppHost = GetAppHost();

var errorResponse = useAppHost != null && useAppHost.ServiceExceptionHandler != null
? useAppHost.ServiceExceptionHandler(request, ex)
: ErrorHandler.Instance.HandleException(useAppHost, request, ex);

AfterEachRequest(request, errorResponse ?? ex);

return errorResponse;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@
<Compile Include="Cors\CorsFeature.cs" />
<Compile Include="Cors\CorsSupportAttribute.cs" />
<Compile Include="ErrorHandler.cs" />
<Compile Include="Express.cs" />
<Compile Include="Providers\InMemoryRollingRequestLogger.cs" />
<Compile Include="Admin\RequestLogsService.cs" />
<Compile Include="RequiredRoleAttribute.cs" />
Expand Down
4 changes: 3 additions & 1 deletion src/ServiceStack.ServiceInterface/Testing/BasicAppHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ public T TryResolve<T>()

public List<IViewEngine> ViewEngines { get; set; }

public Action<IHttpRequest, IHttpResponse, string, Exception> ExceptionHandler { get; set; }
public HandleUncaughtExceptionDelegate ExceptionHandler { get; set; }

public HandleServiceExceptionDelegate ServiceExceptionHandler { get; set; }

public List<HttpHandlerResolverDelegate> CatchAllHandlers { get; set; }

Expand Down
4 changes: 3 additions & 1 deletion src/ServiceStack.ServiceInterface/Testing/TestAppHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ public T TryResolve<T>()

public List<IViewEngine> ViewEngines { get; private set; }

public Action<IHttpRequest, IHttpResponse, string, Exception> ExceptionHandler { get; set; }
public HandleUncaughtExceptionDelegate ExceptionHandler { get; set; }

public HandleServiceExceptionDelegate ServiceExceptionHandler { get; set; }

public List<HttpHandlerResolverDelegate> CatchAllHandlers { get; private set; }

Expand Down
3 changes: 2 additions & 1 deletion src/ServiceStack/ServiceHost/ServiceController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using ServiceStack.Common.Web;
using ServiceStack.Configuration;
using ServiceStack.Logging;
using ServiceStack.ServiceClient.Web;
using ServiceStack.ServiceModel.Serialization;
using ServiceStack.Text;
using ServiceStack.WebHost.Endpoints;
Expand Down Expand Up @@ -125,7 +126,7 @@ public void RegisterIService(ITypeFactory serviceFactoryFn, Type serviceType)
continue;

var actionName = mi.Name.ToUpper();
if (!HttpHeaders.AllVerbs.Contains(actionName) && actionName != ActionContext.AnyAction)
if (!HttpMethod.AllVerbs.Contains(actionName) && actionName != ActionContext.AnyAction)
continue;

var requestType = mi.GetParameters()[0].ParameterType;
Expand Down
6 changes: 3 additions & 3 deletions src/ServiceStack/ServiceHost/ServiceExec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq.Expressions;
using System.Reflection;
using ServiceStack.Common.Web;
using ServiceStack.ServiceClient.Web;
using ServiceStack.Text;
using ServiceStack.WebHost.Endpoints;

Expand Down Expand Up @@ -139,7 +140,7 @@ static IServiceExec()
var args = mi.GetParameters();
if (args.Length != 1) continue;
var actionName = mi.Name.ToUpper();
if (!HttpHeaders.AllVerbs.Contains(actionName) && actionName != ActionContext.AnyAction)
if (!HttpMethod.AllVerbs.Contains(actionName) && actionName != ActionContext.AnyAction)
continue;

var requestType = args[0].ParameterType;
Expand Down Expand Up @@ -251,7 +252,7 @@ public static object Execute(IRequestContext requestContext,
}

var expectedMethodName = actionName.Substring(0, 1) + actionName.Substring(1).ToLower();
throw new MissingMethodException(
throw new NotImplementedException(
"Could not find method named {1}({0}) or Any({0}) on Service {2}"
.Fmt(request.GetType().Name, expectedMethodName, typeof(TService).Name));
}
Expand All @@ -261,7 +262,6 @@ public class IServiceRequestExec<TService, TRequest> : ICanServiceExec
{
static IServiceRequestExec()
{
"Creating cache for {0},{1}".Print(typeof(TService).Name, typeof(TRequest).Name);
IServiceExec<TService>.CreateServiceRunnersFor<TRequest>();
}

Expand Down
8 changes: 7 additions & 1 deletion src/ServiceStack/ServiceHost/ServiceRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,15 @@ public virtual object Execute(IRequestContext requestContext, object instance, I

public virtual object HandleException(IRequestContext requestContext, TRequest request, Exception ex)
{
var useAppHost = GetAppHost();

//TODO workout validation errors
var errorResponse = DtoUtils.HandleException(GetAppHost(), request, ex);
var errorResponse = useAppHost != null && useAppHost.ServiceExceptionHandler != null
? useAppHost.ServiceExceptionHandler(request, ex)
: DtoUtils.HandleException(GetAppHost(), request, ex);

AfterEachRequest(requestContext, request, errorResponse ?? ex);

return errorResponse;
}

Expand Down
7 changes: 6 additions & 1 deletion src/ServiceStack/WebHost.Endpoints/AppDelegates.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.IO;
using System;
using System.Web;
using ServiceStack.ServiceHost;

Expand All @@ -7,4 +7,9 @@ namespace ServiceStack.WebHost.Endpoints
public delegate IHttpHandler HttpHandlerResolverDelegate(string httpMethod, string pathInfo, string filePath);

public delegate bool StreamSerializerResolverDelegate(IRequestContext requestContext, object dto, IHttpResponse httpRes);

public delegate void HandleUncaughtExceptionDelegate(
IHttpRequest httpReq, IHttpResponse httpRes, string operationName, Exception ex);

public delegate object HandleServiceExceptionDelegate(object request, Exception ex);
}
14 changes: 10 additions & 4 deletions src/ServiceStack/WebHost.Endpoints/AppHostBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,16 @@ public List<IViewEngine> ViewEngines
}
}

public Action<IHttpRequest, IHttpResponse, string, Exception> ExceptionHandler
{
get { return EndpointHost.ExceptionHandler; }
set { EndpointHost.ExceptionHandler = value; }
public HandleUncaughtExceptionDelegate ExceptionHandler
{
get { return EndpointHost.ExceptionHandler; }
set { EndpointHost.ExceptionHandler = value; }
}

public HandleServiceExceptionDelegate ServiceExceptionHandler
{
get { return EndpointHost.ServiceExceptionHandler; }
set { EndpointHost.ServiceExceptionHandler = value; }
}

public List<HttpHandlerResolverDelegate> CatchAllHandlers
Expand Down
5 changes: 3 additions & 2 deletions src/ServiceStack/WebHost.Endpoints/EndpointHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using ServiceStack.Common;
using ServiceStack.Common.Web;
using ServiceStack.Html;
using ServiceStack.Logging;
using ServiceStack.MiniProfiler;
using ServiceStack.ServiceHost;
using ServiceStack.VirtualPath;
Expand Down Expand Up @@ -35,7 +34,9 @@ public class EndpointHost

public static List<IViewEngine> ViewEngines { get; set; }

public static Action<IHttpRequest, IHttpResponse, string, Exception> ExceptionHandler { get; set; }
public static HandleUncaughtExceptionDelegate ExceptionHandler { get; set; }

public static HandleServiceExceptionDelegate ServiceExceptionHandler { get; set; }

public static List<HttpHandlerResolverDelegate> CatchAllHandlers { get; set; }

Expand Down
13 changes: 9 additions & 4 deletions src/ServiceStack/WebHost.Endpoints/IAppHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,15 @@ public interface IAppHost : IResolver
/// </summary>
List<IViewEngine> ViewEngines { get; }

/// <summary>
/// Provide an exception handler
/// </summary>
Action<IHttpRequest, IHttpResponse, string, Exception> ExceptionHandler { get; set; }
/// <summary>
/// Provide an exception handler for un-caught exceptions
/// </summary>
HandleUncaughtExceptionDelegate ExceptionHandler { get; set; }

/// <summary>
/// Provide an exception handler for unhandled exceptions
/// </summary>
HandleServiceExceptionDelegate ServiceExceptionHandler { get; set; }

/// <summary>
/// Provide a catch-all handler that doesn't match any routes
Expand Down
11 changes: 3 additions & 8 deletions src/ServiceStack/WebHost.Endpoints/Support/HttpListenerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -389,14 +389,9 @@ public List<IViewEngine> ViewEngines
}
}

/// <summary>
/// Provide an exception handler
/// </summary>
public Action<IHttpRequest, IHttpResponse, string, Exception> ExceptionHandler
{
get { return EndpointHost.ExceptionHandler; }
set { EndpointHost.ExceptionHandler = value; }
}
public HandleUncaughtExceptionDelegate ExceptionHandler { get; set; }

public HandleServiceExceptionDelegate ServiceExceptionHandler { get; set; }

public List<HttpHandlerResolverDelegate> CatchAllHandlers
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
<Compile Include="DynamicJsonTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RazorRockstars_FilesTests.cs" />
<Compile Include="ReqstarsController.cs" />
<Compile Include="ReqStarsService.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
Loading

0 comments on commit 220ad68

Please sign in to comment.