Created
September 20, 2012 13:30
-
-
Save antonydenyer/3755929 to your computer and use it in GitHub Desktop.
ServiceStack ErrorResponseFactory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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