Results 1 to 4 of 4
  1. #1
    Algatron is offline Member
    Join Date
    Mar 2009
    Rep Power

    Default 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 {
    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 07:19 PM. Reason: Forgot one problem

  2. #2
    temp_checker is offline Member
    Join Date
    Mar 2009
    Rep Power


    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.

    public static final String ERROR_CODE_1 = "404";
    foo(Constants.ERROR_CODE_1, Properties.getProperty(Constants.ERROR_CODE_1));

    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.


  3. #3
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Rep Power


    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...

  4. #4
    Algatron is offline Member
    Join Date
    Mar 2009
    Rep Power


    Ok, I see what you're saying, im not sure if this is more or less messy then my current system :)

    I'll experiment a bit with it.

    Thanks for the ideas !

Similar Threads

  1. xml mapping error with JPA in RAD, RSA
    By ishakteyran in forum Other IDEs
    Replies: 0
    Last Post: 02-05-2009, 09:00 PM
  2. xml mapping error with JPA
    By ishakteyran in forum XML
    Replies: 0
    Last Post: 02-05-2009, 08:59 PM
  3. Replies: 1
    Last Post: 11-11-2008, 09:18 PM
  4. Replies: 0
    Last Post: 06-26-2008, 04:27 AM
  5. Replies: 11
    Last Post: 04-16-2008, 02:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts