I'm designing an application which returns JSON string to clients in response, and an error code in case some exception occurs. I'm planning to design a set of error codes which can help me determine what type of error occurred just by looking at the code.

Is there any convention which can be followed for grouping same type of error codes?

EDIT

Thanks for the replies.

Here's how I grouped the error codes (somewhat similar to HTTP status codes) :

  • error codes 51xx => Informational
  • error codes 52xx => success
    • error codes [5200-5220) => success on verification
    • error codes [5220-5240) => success on sent data
    • error codes [5240-5260) => success on creation
    • error codes [5260-5280) => success on modification
    • error codes [5280-5300) => misc.
  • error codes 53xx => To inform that some action needs to be taken
  • error codes 54xx => client related error codes
  • error codes 5500-5549 => server related error code with problem in service layer
  • error codes 5550-5600 => server related error codes with problem in database layer
有帮助吗?

解决方案

Group your errors together in logical units, and determine the convention you want to use. Some people use namespacing, or numbered grouping to make errors easily identifiable, but it largely depends on how many error codes you need to support, and how you want to group them. There isn't a 'standard' convention that I know of.

其他提示

In my experience, that's really up to you. Error codes, however defined, will always have to be explained by you, so you can assign/group them however you please. Users don't care.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top