Best way to define error codes/strings in Java?

前端 未结 13 760
别那么骄傲
别那么骄傲 2020-12-12 08:37

I am writing a web service in Java, and I am trying to figure out the best way to define error codes and their associated error strings. I need to have a nu

相关标签:
13条回答
  • 2020-12-12 09:18

    As far as I am concerned, I prefer to externalize the error messages in a properties files. This will be really helpful in case of internationalization of your application (one properties file per language). It is also easier to modify an error message, and it won't need any re-compilation of the Java sources.

    On my projects, generally I have an interface that contains errors codes (String or integer, it doesn't care much), which contains the key in the properties files for this error:

    public interface ErrorCodes {
        String DATABASE_ERROR = "DATABASE_ERROR";
        String DUPLICATE_USER = "DUPLICATE_USER";
        ...
    }
    

    in the properties file:

    DATABASE_ERROR=An error occurred in the database.
    DUPLICATE_USER=The user already exists.
    ...
    

    Another problem with your solution is the maintenability: you have only 2 errors, and already 12 lines of code. So imagine your Enumeration file when you will have hundreds of errors to manage!

    0 讨论(0)
  • 2020-12-12 09:22

    I'd recommend that you take a look at java.util.ResourceBundle. You should care about I18N, but it's worth it even if you don't. Externalizing the messages is a very good idea. I've found that it was useful to be able to give a spreadsheet to business folks that allowed them to put in the exact language they wanted to see. We wrote an Ant task to generate the .properties files at compile time. It makes I18N trivial.

    If you're also using Spring, so much the better. Their MessageSource class is useful for these sorts of things.

    0 讨论(0)
  • 2020-12-12 09:22

    I use PropertyResourceBundle to define the error codes in an enterprise application to manage locale error code resources. This is the best way to handle error codes instead of writing code (may be hold good for few error codes) when the number of error codes are huge and structured.

    Look at java doc for more information on PropertyResourceBundle

    0 讨论(0)
  • 2020-12-12 09:23

    Overloading toString() seems a bit icky -- that seems a bit of a stretch of toString()'s normal use.

    What about:

    public enum Errors {
      DATABASE(1, "A database error has occured."),
      DUPLICATE_USER(5007, "This user already exists.");
      //... add more cases here ...
    
      private final int id;
      private final String message;
    
      Errors(int id, String message) {
         this.id = id;
         this.message = message;
      }
    
      public int getId() { return id; }
      public String getMessage() { return message; }
    }
    

    seems a lot cleaner to me... and less verbose.

    0 讨论(0)
  • 2020-12-12 09:24

    At my last job I went a little deeper in the enum version:

    public enum Messages {
        @Error
        @Text("You can''t put a {0} in a {1}")
        XYZ00001_CONTAINMENT_NOT_ALLOWED,
        ...
    }
    

    @Error, @Info, @Warning are retained in the class file and are available at runtime. (We had a couple of other annotations to help describe message delivery as well)

    @Text is a compile-time annotation.

    I wrote an annotation processor for this that did the following:

    • Verify that there are no duplicate message numbers (the part before the first underscore)
    • Syntax-check the message text
    • Generate a messages.properties file that contains the text, keyed by the enum value.

    I wrote a few utility routines that helped log errors, wrap them as exceptions (if desired) and so forth.

    I'm trying to get them to let me open-source it... -- Scott

    0 讨论(0)
  • 2020-12-12 09:25

    I (and the rest of our team in my company) prefer to raise exceptions instead of returning error codes. Error codes have to be checked everywhere, passed around, and tend to make the code unreadable when the amount of code becomes bigger.

    The error class would then define the message.

    PS: and actually also care for internationalization !
    PPS: you could also redefine the raise-method and add logging, filtering etc. if required (at leastin environments, where the Exception classes and friends are extendable/changeable)

    0 讨论(0)
提交回复
热议问题