I am working on a class based web application. I have places where objects interact, and I have certain situations where I use error codes to communicate with the end user - usually when form values are missing or invalid. These are situations where exceptions are unfounded (and I'm not sure that I could avoid situations with exceptions anyway).
In one object, I have 20 code numbers, each of which corresponds to a message addressed to the user and a message addressed to the administrator / developer, so both sides know what is happening. Now, when I worked on the code several times, I found it difficult to quickly figure out which code numbers in the series that I already used, so I accidentally create conflicting codes. For example, I just did it today from 12, 13, 14, and 15.
How can I better organize this so as not to create conflicting error codes? Should I create one single-element class errorCodes, which has a main list of all error codes for all classes, organizing them throughout the web application? Or should each object have its own set of error codes, when necessary, and I just keep the list in the comments for the object in order to use and update it when I go?
Edit: Therefore, I like the suggestions to use constants or named constants inside a class. This gives me one place where I programmatically identify and track error codes and their messages.The next question: what interface do I provide to the outside world for error codes and messages of this class? Am I doing something like triggerError(20)in a class and then providing a public method to return an error code, a string constant, and a message related to the user and the administrator?
source
share