How to escalate error messages

It is a matter of best practice for extracting error messages.

I am working on a project where we have errors with codes, short descriptions and seriousness. I was wondering what is the best way to externalize such descriptions. It occurs to me that they have bad code to store them in a database, have them in property files, or possibly have a static class loaded with descriptions. I think I will go with the properties, but maybe there is a better way to do this.

thanks

+6
source share
3 answers

Use the ResourceBundle to store these messages (and other user interface messages, such as button text and tags, accelerators, shortcuts, etc. on). Here is a brief example, assuming you have a package named ErrorMessages with an error named error.NameOfErrorMessage and a JFrame in a variable frame.

ResourceBundle errorMsg = ResourceBundle.getBundle("ErrorMessages"); String currError = errorMsg.getString("error.NameOfErrorMessage"); JOptionPane.showMessageDialog(frame, currError); 

For more information, you can see About the Bundle Resource Class in the Internationalization Tutorial.

+8
source

We faced a similar problem. You are right that having messages in your code is a bad choice. We found that several factors influenced the best alternative. In our case, we needed to have the same error codes in Java on the client side, as well as in SQL and Perl code on the server side. We found it helpful to have a central definition, so we used a database.

If you need to handle error codes in Java, a property file or resource bundle is probably the most flexible as it allows localization and / or internationalization. I would stay away from the static class; although this is better than describing errors in a string, it is still relatively inflexible.

+3
source

I believe there are several ways to do this correctly. The most common way that I see is to use external files that link internal error codes in your actual code and description, something like

 error.123="Error with data X" warning.1="You must use ..." 

To change the text error in your application, you only need to change this text file. This is how internationalization works.

 error.123="Error con el dato X" warning.1="Deberías usar ..." 
0
source

Source: https://habr.com/ru/post/885518/


All Articles