简体   繁体   English

我应该如何处理服务层中的异常?

[英]How should I handle exceptions from my service layer?

I use DataAnnotations in my GUI layer to show error messages in forms, but I have some questions about how to handle exceptions from my service layer and what to show the user if they occur. 我在GUI层中使用DataAnnotations来显示表单中的错误消息,但是我对如何处理服务层中的异常以及如果发生异常如何显示用户有一些疑问。

To communicate with my service layer I use a request and response class. 为了与服务层通信,我使用了请求和响应类。 For example: 例如:

public class RegisterUserRequest
{
    public string Username { get; set; }
    public string Password { get; set; }
    public string Email { get; set; }
}

Should I check for nulls in my request class inside the setter methods? 我应该在setter方法内的请求类中检查空值吗? Or should I do this in my service? 还是我应该在服务中这样做? I think it makes sense to do this on both the request and response setter methods and throw an ArgumentNullException if a parameter is null. 我认为在请求和响应设置器方法上执行此操作并在参数为null时抛出ArgumentNullException是有意义的。

In my service class I throw an InvalidOperationException when for example the username or password is invalid. 在我的服务类中,例如当用户名或密码无效时,我抛出InvalidOperationException Is this the right exception to throw? 这是抛出异常的正确选择吗?

Another question I have is if I should catch all exceptions, and if so, what do I tell the user about the exception? 我还有一个问题是,是否应该捕获所有异常,如果可以,我该如何告知用户该异常? If for example some property is null, it should throw an ArgumentNullException . 例如,如果某些属性为null,则应抛出ArgumentNullException But should I let the user know about this? 但是我应该让用户知道吗?

When an username is invalid I throw an InvalidOperationException . 当用户名无效时,我抛出InvalidOperationException This one I do want to show to the user because it tells the user that it should use at least 3 characters or something. 我确实想向用户显示此字符,因为它告诉用户应至少使用3个字符或其他字符。

I think I should use the error message from the InvalidOperationException to show to users and redirect to a standard error view when other exceptions occur like: "Oops, something went wrong". 我认为我应该使用InvalidOperationException的错误消息显示给用户,并在发生其他异常(例如:“糟糕,出现问题”)时重定向到标准错误视图。

我将再次使用DataAnnotations,因为如果没有人为用户名或密码输入错误的格式,则没人希望将其重定向到错误页面。

I think a more relevant exception to throw when the username or password is invalid is an ArgumentException. 我认为,当用户名或密码无效时引发的更相关的异常是ArgumentException。 In the description of that exception type it specifically covers the case where an argument is invalid. 在对该异常类型的描述中,它专门涵盖了参数无效的情况。

As for passing the exception to the user, you should try to inform the user of the error without exposing any of the inner workings of your service, so having a response message containing the error "Invalid username - must be at least 3 characters" will give them useful feedback. 至于将异常传递给用户,您应该尝试将错误通知用户,而不暴露服务的任何内部工作,因此,如果有一条包含错误消息“无效的用户名-必须至少为3个字符”,则将显示响应消息。给他们有用的反馈。

For errors that you don't want to pass on in detail I would suggest logging an error message yourself and then passing the error ID to the user. 对于您不想详细传递的错误,我建议您自己记录一条错误消息,然后将错误ID传递给用户。 eg "An unhandled error has occured. Please contact support, quoting error ID xxx". 例如:“发生了未处理的错误。请联系支持,并提供错误ID xxx”。 This should only be used as a last resort however - it is better to inform the user how to fix it but this would be a suitable way to catch all errors without passing too much information to the client. 但是,这仅应作为最后的手段-最好通知用户如何解决它,但这将是捕获所有错误而不将过多信息传递给客户端的一种合适方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该如何处理 Dispose() 方法中的异常? - How should I handle exceptions in my Dispose() method? 我应该如何从服务层方法中公开总记录数和IEnumable分页记录集合? - How should I expose the total record count and IEnumable collection of paged records from my service layer method? 我应该处理哪些异常 - What exceptions should I handle 我应该处理/捕获这些异常吗? - Should I handle/catch these exceptions? 如何根据配置设置设计业务层以处理Sql Server和Mongo DB - How should I design my business layer to handle Sql Server and Mongo DB based on configuration setting 我应该如何处理从WCF回调接口对象集合之一引发的异常? - How should I handle exceptions thrown from one of a collection of WCF callback interface objects? 我应该在部分类中组织我的实体框架服务层吗? - Should I organize my Entity Framework Service Layer in partial classes? 我应该如何在业务/服务/应用程序层之间使用异步等待 - How should i use async await between my business/service/application layer 我该如何处理这个C​​#函数中的异常? - How should I handle exceptions in this C# function? 我应该如何处理无效泛型类型参数的异常? - How should I handle exceptions for invalid generic type arguments?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM