如何处理WebAPI 2中的异常。方法吗?

时间:2022-09-11 14:45:07

In my WebAPI 2.1 application I am processing inserts like this:

在WebAPI 2.1应用程序中,我处理的插入如下:

    [Route("Post")]
    public async Task<IHttpActionResult> Post([FromBody]City city)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }
        try
        {
            db.Exams.Add(city);
            await db.SaveChangesAsync(User, DateTime.UtcNow);
        }
        catch (Exception ex)
        {
            var e = ex;
            return BadRequest("Error: City not created");
        }
        return Ok(city);
    }

I use something similar for updates and deletes. I think I should do something more meaningfull with the exceptions but I am not sure where to start with how to handle these. I looked at elmah but it said it deals with unhandled exceptions. Does this mean I should not be catching exceptions like this?

我对更新和删除使用类似的东西。我认为我应该做一些更有意义的事情,除了例外,但我不确定从哪里开始处理这些问题。我查看了elmah,但它说它处理的是未处理的异常。这是否意味着我不应该捕捉这样的异常?

Can someone give me some pointers as to if what I have is okay and also should I be logging exceptions and how?

有人能给我一些指示,如果我所拥有的是好的,我应该记录异常吗?

1 个解决方案

#1


3  

What you are doing is not "bad", it's just a bit verbose and won't scale well if you have many try/catch blocks all over your code. When an exception is raised, you decide what to do so, returning a bad request response is fine if it's really a bad request. There are many things you can return, depending on what went wrong. But you have to handle what to do when exceptions are thrown all over your code, so maintaining this logic scattered all over your code can quickly become a problem.

您正在做的事情并不“糟糕”,它只是有点啰嗦,如果您的代码中有很多try/catch块,那么它就不能很好地扩展。当一个异常被提出时,您决定要做什么,如果一个错误的请求响应确实是一个错误的请求,那么返回一个错误的请求响应是可以的。你可以返回很多东西,这取决于哪里出错了。但是,当异常在代码中到处抛出时,您必须处理要做的事情,因此将这个逻辑分散在代码中很快就会成为一个问题。

It's better to utilise asp.net web api's exception handling framework. For example, take a look at these articles:

最好使用asp.net web api的异常处理框架。例如,看看这些文章:

The idea is to centralise your logic in a global exception handler and to use that as the only place in your code where you have to worry about this. The rest of your code will be throwing exceptions and everything will be coming through your exception handler/filter/etc., depending on what you decide.

其思想是将逻辑集中到全局异常处理程序中,并将其用作代码中惟一需要担心的地方。代码的其余部分将抛出异常,所有的代码都将通过异常处理程序/筛选器/etc来处理。取决于你的决定。

For example, I have created my own exception types (e.g. CustomExceptionA, CustomExceptionB, etc.) and when I throw an exception of a type I know exactly how to handle it in one place and perform a certain bit of logic. If I want to change the way I handle a particular exception type, then there's only one place I have to make a change and the rest of the code will be unaffected.

例如,我创建了自己的异常类型(例如CustomExceptionA、CustomExceptionB等),当我抛出一个类型的异常时,我确切地知道如何在一个地方处理它并执行一些逻辑。如果我想更改处理特定异常类型的方式,那么只有一个地方需要进行更改,其余代码将不受影响。

The second article link above also includes a global exception logger to log such exceptions.

上面的第二篇文章链接还包括一个全局异常日志记录器来记录这些异常。

#1


3  

What you are doing is not "bad", it's just a bit verbose and won't scale well if you have many try/catch blocks all over your code. When an exception is raised, you decide what to do so, returning a bad request response is fine if it's really a bad request. There are many things you can return, depending on what went wrong. But you have to handle what to do when exceptions are thrown all over your code, so maintaining this logic scattered all over your code can quickly become a problem.

您正在做的事情并不“糟糕”,它只是有点啰嗦,如果您的代码中有很多try/catch块,那么它就不能很好地扩展。当一个异常被提出时,您决定要做什么,如果一个错误的请求响应确实是一个错误的请求,那么返回一个错误的请求响应是可以的。你可以返回很多东西,这取决于哪里出错了。但是,当异常在代码中到处抛出时,您必须处理要做的事情,因此将这个逻辑分散在代码中很快就会成为一个问题。

It's better to utilise asp.net web api's exception handling framework. For example, take a look at these articles:

最好使用asp.net web api的异常处理框架。例如,看看这些文章:

The idea is to centralise your logic in a global exception handler and to use that as the only place in your code where you have to worry about this. The rest of your code will be throwing exceptions and everything will be coming through your exception handler/filter/etc., depending on what you decide.

其思想是将逻辑集中到全局异常处理程序中,并将其用作代码中惟一需要担心的地方。代码的其余部分将抛出异常,所有的代码都将通过异常处理程序/筛选器/etc来处理。取决于你的决定。

For example, I have created my own exception types (e.g. CustomExceptionA, CustomExceptionB, etc.) and when I throw an exception of a type I know exactly how to handle it in one place and perform a certain bit of logic. If I want to change the way I handle a particular exception type, then there's only one place I have to make a change and the rest of the code will be unaffected.

例如,我创建了自己的异常类型(例如CustomExceptionA、CustomExceptionB等),当我抛出一个类型的异常时,我确切地知道如何在一个地方处理它并执行一些逻辑。如果我想更改处理特定异常类型的方式,那么只有一个地方需要进行更改,其余代码将不受影响。

The second article link above also includes a global exception logger to log such exceptions.

上面的第二篇文章链接还包括一个全局异常日志记录器来记录这些异常。