在Web API中返回自定义错误对象

时间:2013-04-26 18:32:29

标签: asp.net-mvc-4 exception-handling error-handling asp.net-web-api

我有一个Web API,我正在使用MVC 4 Web API框架。如果有异常,我现在正在抛出一个新的HttpResponseException。即:

if (!Int32.TryParse(id, out userId))
    throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid id")); 

这会将一个对象返回给客户端,该对象只是{"message":"Invalid id"}

我希望通过返回更详细的对象来进一步控制对异常的响应。像

这样的东西
{
 "status":-1,
 "substatus":3,
 "message":"Could not find user"
 }

我该怎么做呢?是序列化我的错误对象并在响应消息中设置它的最佳方法吗?

我也调查了ModelStateDictionary并且已经提出了这个“黑客”,但它仍然不是一个干净的输出:

var msd = new ModelStateDictionary();
msd.AddModelError("status", "-1");
msd.AddModelError("substatus", "3");
msd.AddModelError("message", "invalid stuff");
throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, msd));

修改
看起来像自定义HttpError是我需要的。这似乎可以解决问题,现在可以从我的业务层扩展它......

var error = new HttpError("invalid stuff") {{"status", -1}, {"substatus", 3}};
throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, error));

3 个答案:

答案 0 :(得分:40)

这些答案比他们需要的更复杂。

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Filters.Add(new HandleApiExceptionAttribute());
        // ...
    }
}

public class HandleApiExceptionAttribute : ExceptionFilterAttribute
{
    public override void OnException(HttpActionExecutedContext context)
    {
        var request = context.ActionContext.Request;

        var response = new
        {
             //Properties go here...
        };

        context.Response = request.CreateResponse(HttpStatusCode.BadRequest, response);
    }
}

这就是你所需要的一切。这也很好,也很容易进行单元测试:

[Test]
public async void OnException_ShouldBuildProperErrorResponse()
{
    var expected = new 
    {
         //Properties go here...
    };

    //Setup
    var target = new HandleApiExceptionAttribute()

    var contextMock = BuildContextMock();

    //Act
    target.OnException(contextMock);

    dynamic actual = await contextMock.Response.Content.ReadAsAsync<ExpandoObject>();

    Assert.AreEqual(expected.Aproperty, actual.Aproperty);
}

private HttpActionExecutedContext BuildContextMock()
{
    var requestMock = new HttpRequestMessage();
    requestMock.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration());

    return new HttpActionExecutedContext()
    {
        ActionContext = new HttpActionContext
        {
            ControllerContext = new HttpControllerContext
            {
                Request = requestMock
            }

        },
        Exception = new Exception()
    };
}

答案 1 :(得分:9)

我认为这会解决问题:

为业务层创建自定义异常类:

 public class MyException: Exception
 {
    public ResponseStatus Status { get; private set; }
    public ResponseSubStatus SubStatus { get; private set; }
    public new string Message { get; private set; }

    public MyException()
    {}

    public MyException(ResponseStatus status, ResponseSubStatus subStatus, string message)
    {
        Status = status;
        SubStatus = subStatus;
        Message = message;
    }
 }

创建一个静态方法,从HttpError的实例生成MyException。我在这里使用了反射,所以我可以向MyException添加属性,并且总是让它们返回而不更新Create

    public static HttpError Create<T>(MyException exception) where T:Exception
    {
        var properties = exception.GetType().GetProperties(BindingFlags.Instance 
                                                         | BindingFlags.Public 
                                                         | BindingFlags.DeclaredOnly);
        var error = new HttpError();
        foreach (var propertyInfo in properties)
        {
            error.Add(propertyInfo.Name, propertyInfo.GetValue(exception, null));
        }
        return error;
    }

我目前有一个常规异常处理程序的自定义属性。类型MyException的所有例外都将在此处理:

public class ExceptionHandlingAttribute : ExceptionFilterAttribute
{
    public override void OnException(HttpActionExecutedContext context)
    {
        var statusCode = HttpStatusCode.InternalServerError;

        if (context.Exception is MyException)
        {
            statusCode = HttpStatusCode.BadRequest;
            throw new HttpResponseException(context.Request.CreateErrorResponse(statusCode, HttpErrorHelper.Create(context.Exception)));
        }

        if (context.Exception is AuthenticationException)
            statusCode = HttpStatusCode.Forbidden;

        throw new HttpResponseException(context.Request.CreateErrorResponse(statusCode, context.Exception.Message));
    }
}

我会更多地玩这个并更新,因为我在这个计划中找到了漏洞。

答案 2 :(得分:2)

看看下面的文章。它将帮助您控制Web api异常和错误消息:Web Api, HttpError, and the Behavior of Exceptions