最佳实践 - 您自己的项目/应用程序的 NSError 域和代码
有一个之前的SO帖子关于设置错误域您自己的框架,但是为您自己的项目/应用程序设置错误域和自定义错误代码的最佳实践是什么?
例如,假设您正在开发一个具有大量验证的核心数据密集型应用程序,您是否应该坚持使用“现成的”核心数据错误代码(例如来自 的
)或者您应该创建自己的 NSManagedObjectValidationError
CoreDataErrors.hMyAppErrors.h
并定义更具体的错误(即 MyAppValidationErrorInvalidCombinationOfLimbs
)?
创建自定义错误域和一组错误代码可以显着消除代码的歧义,但是维护开销是否太大,是否需要担心错误代码编号冲突?或者这里还有其他问题吗?
There is a previous SO post regarding setting up error domains for your own frameworks, but what is the best practice regarding setting up error domains and custom error codes for your own project/app?
For example, supposing you're working on a Core Data-intensive app with lots of validations, should you just stick with the "off the shelf" Core Data error codes (such as NSManagedObjectValidationError
from CoreDataErrors.h
) or should you create your own MyAppErrors.h
and define errors with more specificity (i.e., MyAppValidationErrorInvalidCombinationOfLimbs
?
Creating a custom error domain and set of error codes could significantly disambiguate your code, but is it too much overhead to maintain and does one have to worry about error code numbering conflicts? Or are there other concerns here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我个人使用反向 DNS 风格的域。例如:
域的第三部分(
@"myproject"
)仅用于区分此项目("My Project"
)中的错误与另一个项目中的错误(“我的其他项目”
=>com.davedelong.myotherproject
)。这是一种确保我不会与其他人的错误域发生冲突的简单方法(如果我使用的是第 3 方代码),除非该开发人员故意试图干扰我(这我相信这是极不可能的......)。
至于代码编号冲突,不用担心。只要代码在域内是唯一的,就应该没问题。
至于翻译错误,那就取决于你了。无论您做什么,请确保妥善记录。 就我个人而言,我通常只是传递框架生成的错误,因为我永远不确定我是否会处理所有代码并将所有 userInfo 转换为更具体的内容我的项目。框架可以更改和添加更多代码,或者更改现有代码的含义等。它还可以帮助我更具体地识别错误来自何处。例如,如果我的 StackKit 框架在
com.stackkit
域中生成错误,我知道这是一个框架问题。但是,如果它在NSURLErrorDomain
中生成错误,那么我就知道它具体来自 URL 加载机制。您可以做的是捕获框架生成的错误并将其包装在一个新的错误对象中,该对象具有您的域和通用代码,例如
kFrameworkErrorCodeUnknown
之类的东西,然后放置NSUnderlyingErrorKey
下的userInfo
中捕获的错误。 CoreData 经常这样做(例如,如果您尝试save:
一个NSManagedObjectContext
,但存在关系完整性错误,您将收到一个错误,但NSUnderlyingErrorKey
将包含更多信息,例如具体哪些关系是错误的,等等)。I personally use a reverse-DNS style domain. For example:
The third part of the domain (
@"myproject"
) is just used to differentiate the errors from this project ("My Project"
) from errors in another project ("My Other Project"
=>com.davedelong.myotherproject
).It's a simple way to ensure that I'm not going to conflict with anyone else's error domains (if I'm using 3rd party code), unless that developer is purposefully trying to mess with just me (which I believe would be highly unlikely...).
As for code numbering conflicts, don't worry about that. Just as long as codes are unique within a domain, you should be OK.
As for translating errors, that's up to you. Whatever you do, make sure you document it well. Personally, I usually just pass on framework-generated errors as they came to me, since I'm never quite sure that I'll handle all the codes and translate all of the userInfo into something more specific to my project. The frameworks could change and add more codes, or change the meaning of existing codes, etc. It also helps me more specifically identify where the error came from. For example, if my StackKit framework generates an error in the
com.stackkit
domain, I know that it's a framework problem. However, if it generates an error in theNSURLErrorDomain
, then I know that it specifically came from the URL loading mechanism.What you could do is capture the framework generated error and wrap it in a new error object that has your domain and a generic code, something like
kFrameworkErrorCodeUnknown
or something, and then place the captured error in theuserInfo
under theNSUnderlyingErrorKey
. CoreData does this a lot (for example, if you try tosave:
anNSManagedObjectContext
, but you have relationship integrity errors, you'll get a single error back, but theNSUnderlyingErrorKey
will contain much more information, like specifically which relationships are wrong, etc).我没有足够的代表来发表评论,但对于 Dave DeLong 接受的答案,使用
[[NSBundle mainBundle] bundleIdentifier]
而不是@"com.myName 可能会稍微好一些.myProject”
。这样,如果您更改您的姓名或项目名称,它将准确反映。I don't have enough rep to comment, but for the accepted answer by Dave DeLong, it might be slightly better to use
[[NSBundle mainBundle] bundleIdentifier]
instead of@"com.myName.myProject"
. This way, if you change your name or project's name, it will be reflected accurately.如何创建自定义 NSError:
首先创建错误消息的字典
,然后将 userInfo 分配给 NSDictionary 并完成。
How to create a custom NSError:
First create a Dictionary of the error message
Then assign the userInfo to the NSDictionary and your done.