-
Notifications
You must be signed in to change notification settings - Fork 0
Error Handling
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
}
}
In the end all Web Service Exceptions are serialized into a ResponseStatus DTO which by default is serialized by convention.
In addition to the above options, you can override the serialization of ad-hoc exceptions by having it implement the IResponseStatusConvertible.ToResponseStatus()
method and have it return your own populated ResponseStatus instance instead.
Use Config.MapExceptionToStatusCode
to change what error codes are returned for different exceptions, e.g:
SetConfig(new EndpointHostConfig {
MapExceptionToStatusCode = {
{ typeof(CustomInvalidRoleException), 403 },
{ typeof(CustomerNotFoundException), 404 },
}
});
Use Config.CustomHttpHandlers
for specifying custom HttpHandlers to use with specific error status codes, e.g:
SetConfig(new EndpointHostConfig {
CustomHttpHandlers = {
{ HttpStatusCode.NotFound, new RazorHandler("/notfound") },
{ HttpStatusCode.Unauthorized, new RazorHandler("/login") },
}
});
Use Config.GlobalHtmlErrorHttpHandler
for specifying a fallback HttpHandler for all error status codes, e.g:
SetConfig(new EndpointHostConfig {
GlobalHtmlErrorHttpHandler = new RazorHandler("/oops"),
});
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