Interface MandatoryExceptionTranslator<T extends Exception>

Type Parameters:
T - Type of exception it handles. E.g. SqlException

public interface MandatoryExceptionTranslator<T extends Exception>
Common interface for writing mappers of (framework) exceptions. Many times we encounter common framework messages that needs to be parsed to extract information from them. Frameworks are used repeatedly in multiple projects and handling those exceptions is most of the time the same. This interface should unify handling of such cases and reuse once written implementation. Example of such implementation could be SqlException of particular database driver or common network-related exceptions.
  • Method Details

    • translate

      @NotNull @NotNull ProcessingException translate(@Nullable T exception)
      Parse framework or other repeated exception into a ProcessingException. Please extract as much information as needed to properly understand error state but no more than that. Beware of security concerns and do not provide any sensitive information. Please do not return directly Throwable.getMessage() or Throwable.getLocalizedMessage() without parsing its content and selecting only information you trust. Never trust messages from third party libraries, you do not have a control over them. One version may return correct message, later version may return sensitive information. When you implement this interface, try to write as many test cases as you can to validate particular exception mapping. When library version for which you write exception translator changes, you may need to validate that messages has not changed and mapping has still the same behaviour.
      Parameters:
      exception - An exception to parse.
      Returns:
      Instance of ProcessingException in the case this mapper is able to properly parse it, defaultException(Exception) otherwise.
    • translateAndThrow

      default void translateAndThrow(@Nullable T exception)
      Translate the exception with the translator. In the case the translator is capable to handle such exception, translated exception will be thrown, otherwise no action is performed.
      Parameters:
      exception - Exception to translate.
    • defaultException

      @NotNull @NotNull ProcessingException defaultException(@Nullable T exception)
      The default exception to return when translator is not capable to handle provided exception.
      Parameters:
      exception - Exception to translate.