Use error codes or not use error codes

So, I am working on a new project at work, and today a colleague had the idea that my exceptions and even returned error messages should be completely localized. I thought it was a good idea, but he said that I only need to return error codes erroneously. I personally don't like the idea of ​​an error code, as it tends to make other programmers either

  • Reusing error codes where they do not fit because they do not want to add another
  • They tend to use the wrong error codes, as they can be defined so that there are enough of them.

So my question is, what do everyone else do to deal with this situation? I am open to all suggestions, including those that think error codes are the way to go.

+4
source share
3 answers

Could there be cultural differences according to your coding language?

In Java, for example, numeric error codes are not used much ...


Regarding exceptions, I think this is just a technical tool. The important thing is that your message is intended for the user or developer. It is important for the user to localize messages if several languages ​​appear or the ability to change messages without recompiling (to configure between clients, to adapt to changing user needs ..).


In my projects, our culture is to use (java) enumerations to process all sets of fixed values. Errors are no different. Enumerations for errors can provide:

  • strong typing (you cannot pass anything else to a method expecting an error code)
  • simple localization (a simple utility method can automatically find a message corresponding to each of them, using, for example, "SimpleClassName". The template "INSTANCE_NAME", you can also expose the getMessage () method for each enumeration that delegates the implementation of your utility method)
  • checking your localized files (your unit tests can loop in each language of the code and files and find all unsurpassed values)
  • error level function (we use the same levels as for logging: fatal, errors, warnings, therefore logical decisions are very easily implemented!).
  • To facilitate the search for the corresponding error by other developers, we use several enumerations (possibly in the same package), classifying errors according to their technical or functional domain.

To ask your two problems:

  • To add, you only need to add the instance to the enumeration and the message in the localization file (but the tests can catch later if you forget).
  • When classifying in several enumerations, and possibly in javadoc, they are guided by the correct error.
+3
source

I would not use error codes for localization. There may be good reasons to use error codes (for example, to check which type of error has occurred), but localization is not one of these causes. Instead, use the same structure that you use for the rest of message localization as well for exceptions. For instance. if you use gettext everywhere, also use it in exceptions. This will make life easier for the translator.

+3
source

You can include an error code in the exception, thereby obtaining the best of both.

One of the common causes of errors with old function return code errors is the failure to check the error code before continuing with the subsequent code. An exception cannot be implicitly ignored. Eliminating the source of the error is good.

The error code allows you to:

  • Calling code to distinguish between different types of errors.
  • Messages that should be constructed by user interface components when errors occur in non-UI, non-localized code.
  • A list of errors in the code that can be useful when writing user documentation or troubleshooting guides.

A few recommendations that I found useful:

  • Only the architectural layers of the user interface create and localize messages to the user.
  • If non-UI layers exchange errors through exceptions, they may optionally contain error codes and additional fields that are useful to the user interface when building the message.
  • In Java, when error codes are defined in the UI layer, error message format strings can be obtained through the counters themselves. They may contain characters for additional data carried on error.
  • In Java, include the argument index in the format specifiers in the source language strings so that translators can move dynamic data in localized messages.
+2
source

All Articles