Symbolic vs. numeric error codes in web API

不羁岁月 提交于 2019-12-23 12:13:26

问题


I'm currently working on new project, which should provide a web-based API. Of course, there are many cases, when requested operation fails and API should report about reasons of this fail.

Previously I used traditional way, when response contains numeric error code and more human-friendly error message, but maintaining error code list and mapping between my codes and internal exceptions was a bit frustrating.

I thought that it might be more convenient to use symbolic error codes, for example generated from exception names.

I see following advantages of this idea:

  • No need for mapping from exceptions to numbers.
  • Impossible to assign same error codes to different error types (in my case - exceptions).
  • Error code becomes more informative for developer who will use API by itself.

And disadvantages:

  • Exposing exception names might be not so good idea in terms of encapsulation.
  • Since error codes are mostly used by API client applications to determine error type, comparing string is more expensive.
  • No need for central exception->errcode registry increases chances to miss some of error codes in documentation.

So, what is better to use: symbolic or numeric error codes?

来源:https://stackoverflow.com/questions/11775812/symbolic-vs-numeric-error-codes-in-web-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!