问题
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