Results 1 to 4 of 4
- 03-18-2009, 06:15 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 9
- Rep Power
- 0
Re-usable, dynamic pattern for error code-message mapping
Primarily the problem relates to a JSP project, but the question is not jsp specific.
I want to create kind of a re-usable and extendable repository of error codes and related error messages.
These errors could be form validation, sql, or any custom error really.
There also needs to be a re-usable, modifiable, extendable system for these functions that return error messages, as well as
Currently I have a singleton class ErrorCodeMessage mapping codes to the error message string. These codes are public static enum so that I can grab them from anywhere quickly with smart IDE.
I have various utility classes for example FormValidator with functions for checking email address format, zip/postal code, phone number etc...
These functions return codes from the ErrorCodeMessage class.
The problems:
1. In ErrorCodeMessage I have to duplicate all the error code typing
public static enum ErrorCode {
ERROR1;
}
hashmap.put(ERROR1, "some error message");
2. There is no way to ensure that all codes have a corresponding message
3. It is difficult to extend the errors due to it being enum, yet I need the enum for ease of programmer use in the IDE
4. The utility functions should be interchangeable (Strategy design pattern), but sicne they're not tied in directly with a error code its messy.
EX. password validating function changes to reuire 8 instead of 6 characters, the error message then needs to be changed....
If anyone knows a good overall solution, please feel free to share.
Thanks :)Last edited by Algatron; 03-18-2009 at 06:19 PM. Reason: Forgot one problem
- 03-18-2009, 08:32 PM #2
Member
- Join Date
- Mar 2009
- Posts
- 1
- Rep Power
- 0
The best approach to implementing such a error code-description mapping would be helpful to me too. Currently I have a file that simply has all the codes and descriptions thrown in, e.g.
public static final String ERROR_CODE_1 = "404";
public static final String ERROR_DESC_1 = "Not Found";
public static final String ERROR_CODE_2 = "500";
public static final String ERROR_DESC_2 = "Internal Server Error";
and then I can use them as:
foo(Constants.ERROR_CODE_1, Constants.ERROR_DESC_1);
Clearly, this approach is much more rudimentary than yours. What I would like to do is have a properties file that maybe looks something like:
404=Not Found
500=Internal Server Error
But that just seems to complicate things further since I would need to have either a variable corresponding to each property, or specify the code directly in the call to my printError, i.e.
either
public static final String ERROR_CODE_1 = "404";
foo(Constants.ERROR_CODE_1, Properties.getProperty(Constants.ERROR_CODE_1));
or
foo(404, Properties.getProperty(404));
neither of which are something I feel comfortable doing.
I'm sure people have implemented a more standard way of handling this situation. Anybody who has the knowledge and is able to share would be doing quite a few people a favor.
Thanks.
- 03-18-2009, 11:15 PM #3
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
Well, an approach that would give you the internationalization of properties files with strong type checking of an enum, would be to create a code builder. The code builder would create an enum from a properties file. Then, use the enum's valueof() so the error code strings can be directly converted to the appropriate enum element and used in switch statements or whatever.
To obtain the text for a given element, override 'toString' to read from the properties file.
This approach would ensure compile time safety and internationalization capability, although it has the slightly messy feature of involving a code builder. The code builder would be dirt simple though...
- 03-19-2009, 04:01 AM #4
Member
- Join Date
- Mar 2009
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
xml mapping error with JPA in RAD, RSA
By ishakteyran in forum Other IDEsReplies: 0Last Post: 02-05-2009, 08:00 PM -
xml mapping error with JPA
By ishakteyran in forum XMLReplies: 0Last Post: 02-05-2009, 07:59 PM -
Hibernate mapping error Provided id of the wrong type with composite id
By zigzag in forum JDBCReplies: 1Last Post: 11-11-2008, 08:18 PM -
Dynamic code instrumentation with AspectJ...is it possible ?
By Barronrad in forum New To JavaReplies: 0Last Post: 06-26-2008, 03:27 AM -
How to Print out adiamond pattern I try this code but
By masaka in forum New To JavaReplies: 11Last Post: 04-16-2008, 01:52 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks