Skip to content

Instantly share code, notes, and snippets.

@antonydenyer
Created September 20, 2012 13:30
Show Gist options
  • Save antonydenyer/3755929 to your computer and use it in GitHub Desktop.
Save antonydenyer/3755929 to your computer and use it in GitHub Desktop.
ServiceStack ErrorResponseFactory
public interface IErrorResponseFactory
{
object CreateErrorResponse<TRequest>(TRequest request, Exception ex, ResponseStatus responseStatus);
object CreateErrorResponse<TRequest>(TRequest request, ValidationErrorResult validationError);
}
public class ErrorResponseFactory : IErrorResponseFactory
{
public object CreateErrorResponse<TRequest>(TRequest request, ValidationErrorResult validationError)
{
var responseStatus = ResponseStatusTranslator.Instance.Parse(validationError);
var errorResponse = CreateErrorResponse(request, new ValidationError(validationError), responseStatus);
return errorResponse;
}
public object CreateErrorResponse<TRequest>(TRequest request, Exception ex, ResponseStatus responseStatus)
{
var responseDto = CreateResponseDto(responseStatus);
var httpError = ex as IHttpError;
if (httpError != null)
{
if (responseDto != null)
httpError.Response = responseDto;
return httpError;
}
var errorCode = ex.GetType().Name;
var errorMsg = ex.Message;
if (responseStatus != null)
{
errorCode = responseStatus.ErrorCode ?? errorCode;
errorMsg = responseStatus.Message ?? errorMsg;
}
return new HttpError(responseDto, ex.ToStatusCode(), errorCode, errorMsg);
}
private static object CreateResponseDto(ResponseStatus responseStatus)
{
return new ErrorResponse { ResponseStatus = responseStatus };
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment