发生DBupdateException时为用户显示用户友好错误消息的最佳方法

发生DBupdateException时为用户显示用户友好错误消息的最佳方法

问题描述:

我的操作方法中包含以下代码:

I have the following code inside my action method :

Try {
    repository.Save();
    DetailBox b = new DetailBox() { 
        Tag = repository.getTechnologyTagByIT360ID(resourceid) 
    };

    return PartialView("_detailBox", b);
}
catch (DbUpdateException e)
{
    ModelState.AddModelError(string.Empty, "Error occurred:" +
        e.InnerException.InnerException.Message);
}

当前,如果发生DbUpdatException,它将向用户显示以下错误:

Currently, if the DbUpdatException occurs it will show the following error to the user:


•发生错误:违反UNIQUE KEY约束'IX_Technology_2'。
无法在对象dbo.Technology中插入重复的密钥。
的重复键值为(18605)。该语句已终止。

• Error occurred: Violation of UNIQUE KEY constraint 'IX_Technology_2'. Cannot insert duplicate key in object 'dbo.Technology'. The duplicate key value is (18605). The statement has been terminated.

虽然不是很糟糕,但是同时它也不是很友好。现在,有很多原因可能导致DbUpateException发生,因此我无法定义自己的message。那么,我应该采取哪种方法呢?例如,我是否应该编写一种服务方法来检查 repository.Save(); 之前数据库中是否存在重复的键值(18605)?

Which is not very bad, but at the same time, it is not very user friendly. Now there are many reasons why the DbUpateException might occur, so I can not define my own message . So, which approach should I take? Should I, for example, write a service method that check if the duplicate key value (18605) already exists in the database just before the repository.Save();?

捕获异常是您处理意外情况的方法,通常是在与就像文件系统,服务和数据库一样,是您无法控制的。

Catching exceptions is something you do to deal with unexpected situations, typically when interacting with systems that are beyond your own control, like file systems, services and yes, databases.

但是例外应该是例外。也就是说,只要可以预见到它们,就应该尝试防止它们被抛出。绝对不要将它们用于程序流控制。因此,如果用户可以输入重复的数据库条目,请先检查并在违反约束时返回一个体面的消息。这是对用户输入进行常规验证的一部分。就像您不允许在数据库字段的字符串值太长时发生异常一样。

But exceptions should be that - exceptional. That is, whenever you can anticipate them you should try to prevent them from being thrown. You should never use them for program flow control. So if a user can enter duplicate database entries, check first and return a decent message when the constraint is violated. It's part of normal validation of user input. Same as you don't allow an exception to occur when a string value is too long for a database field.

最后的捕获仍然应该存在,但是现在确实如此变为 exception ,即在检查和提交之间的极短时间内,并发用户碰巧输入了相同的值。

The final catch should still be there, but now it really becomes exceptional, that is, when the same value happens to get entered by a concurrent user in the tiny time lapse between the check and the commit.