forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Error Handling
cordis-dev edited this page Jan 21, 2013
·
23 revisions
ServiceStack and it's new API provides flexible way to intercept exceptions. If you need a single entry point for all service exceptions, you can subscribe to AppHostBase.ServiceExceptionHandler
events in Configure
method like shown below:
public override void Configure(Container container)
{
ServiceExceptionHandler += (request, exception) => {
//log your exceptions here
...
//call default exception handler or prepare your own custom response
return DtoUtils.HandleException(this, request, exception);
};
}
If you want to provide different error handlers for different actions and services you can just tell ServiceStack to run your services in your own custom IServiceRunner and implement the HandleExcepion event hook in your AppHost:
public override IServiceRunner<TRequest> CreateServiceRunner<TRequest>(
ActionContext actionContext)
{
return new MyServiceRunner<TRequest>(this, actionContext);
}
Where MyServiceRunner is just a custom class implementing the custom hooks you're interested in, e.g:
public class MyServiceRunner<T> : ServiceRunner<T> {
public override object HandleException(IRequestContext requestContext,
TRequest request, Exception ex) {
// Called whenever an exception is thrown in your Services Action
}
}
If you have old services you still can handle all exceptions in one single place. Create base class for your services and override HandleException
method like shown below.
public class MyServiceBase<TRequest> : RestService<TRequest>
{
public override object HandleException(TRequest request, Exception exception)
{
//log your exceptions here
...
//call default exception handler or prepare your own custom response with
return base.HandleException(request, exception);
// or prepare new customer response with new HttpError(...)
}
}
- Why ServiceStack?
- What is a message based web service?
- Advantages of message based web services
- Why remote services should use separate DTOs
- Getting Started
- Reference
- Clients
- Formats
- View Engines 4. Razor & Markdown Razor
- Hosts
- Advanced
- Configuration options
- Access HTTP specific features in services
- Logging
- Serialization/deserialization
- Request/response filters
- Filter attributes
- Concurrency Model
- Built-in caching options
- Built-in profiling
- Messaging and Redis
- Form Hijacking Prevention
- Auto-Mapping
- HTTP Utils
- Virtual File System
- Config API
- Physical Project Structure
- Modularizing Services
- Plugins
- Tests
- Other Languages
- Use Cases
- Performance
- How To
- Future