捕获数据库异常 [英] Catching Database Exceptions

查看:365
本文介绍了捕获数据库异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在设计一个 Web服务,当然;我需要此 Service 自动写入 Database 。这很简单;但显然 Sql 倾向于玩不好。

So I'm designing a Web Service and of course; I need this Service to automatically write to a Database. Which is quite straight forward; but obviously Sql has a tendency to not play nice.

在进行故障排除时,这简直是一场噩梦,更不用说通过了。 Web服务将使调试变得更像一场噩梦。

Which pretty much creates a nightmare when troubleshooting, not to mention through a Web Service would make it even more of a nightmare to debug.

因此,我检查了Internet,偶然发现了Stack Overflow上一篇有关 SqlHelper类实质上具有大量此类:

So I checked the internet and stumbled across an article on Stack Overflow that talked about a SqlHelper Class that essentially had a massive list of these:

public static bool IsDuplicateId(SqlException sex)
{
    return (sex.Number == 2601);
}

这样的实现将是乏味的,因为您必须调用所有这些方法。但是,有人回答:

So that implementation would be tedious, as you'd have to call all of these Methods. However, someone answered with:

switch (e.Number)
    case 2601:
         // Do Something
         break;
    default:
        throw;

所以我想为什么不创建一个 Class 来处理其中的大多数可能的错误。牢记此特定实现:

So I thought why not create a Class to handle a majority of these possible errors. With this particular implementation in mind:

public class SqlExceptionHelper
{
    public SqlExceptionHelper(SqlException sqlException)
    {
        // Do Nothing.
    }

    public static string GetSqlDescription(SqlException sqlException)
    {
        switch (sqlException.Number)
        {
             case 21:
                 return "Fatal Error Occurred: Error Code 21.";
             case 53:
                 return "Error in Establishing a Database Connection: 53.";
             default
                 return ("Unexpected Error: " + sqlException.Message.ToString());
         }
     }
}

所以我的思考过程是有一个 Class ,可以重复使用以检测一些常见错误;在其他类中,我只是使用SomeNamespace.ExceptionHelpers 我可以实现类似这样的东西:

So my thought process is I have a Class that can be reused to detect some common errors; and in other classes I simply using SomeNamespace.ExceptionHelpers; I could implement something like so:

public class SiteHandler : ISiteHandler
{
     public string InsertDataToDatabase(Handler siteInfo)
     {
          try
          {
              // Open Database Connection, Run Commands, Some additional Checks.
          }
          catch(SqlException exception)
          {
             SqlExceptionHelper errorCompare = new SqlExceptionHelper(exception);
             return errorCompare.ToString();
          }
     }
}

因此从本质上讲,它应该处理所有那些可爱的例外;但我开始考虑哪一个不好。 是否正在将此类例外作为退货商品?这本身会不好吗?还是这真的是通过服务处理此类异常捕获的最佳方法?

So essentially it should handle all those lovely exceptions; but I started thinking which isn't good. Is returning such Exceptions as a return good? Can that in itself be bad? Or is this really the best way to handle such Exception catching through a Service?

所以我的问题可以归结为:

So my question boils down to:


       Is this the best way to handle error catching through a Service?


感谢您的帮助。

推荐答案

理想情况下,Web服务应返回相关的HTTP状态代码,而不是异常。这些通常是200(代表OK),400(代表用户可以自行修复的错误)或500(代表服务器错误-这些也可以自动重试)。

Ideally web services should return relevant HTTP status codes, not exceptions. These are usually in the 200's (for OK), 400's (for errors that the user can fix themselves), or 500's (for server errors - these can be auto-retried too).

根据您收到的数据库错误,可以将其转换为相应的HTTP状态代码。如果您认为错误代码的描述会对用户有所帮助,则可以将其设置为异常消息。

Based on your database error you get back, you can translate this to the appropriate HTTP status code. The description of the error code could be set to the exception message if you think it will help the user.

这篇关于捕获数据库异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆