Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MyBatisExceptionTranslator loose PersistenceException message #53

Open
pavla opened this issue Nov 21, 2014 · 0 comments
Open

MyBatisExceptionTranslator loose PersistenceException message #53

pavla opened this issue Nov 21, 2014 · 0 comments

Comments

@pavla
Copy link

pavla commented Nov 21, 2014

When MyBatis PersistenceException is translated to DataAccessException by MyBatisExceptionTranslator the original message from PersistenceException (ErrorContext message) gets lost. I've implemented my own ExceptionTranslator that preserves the original message - feel free to use it to update your MyBatisExceptionTranslator (my changes are marked with comments).

 public DataAccessException translateExceptionIfPossible(RuntimeException e) {
        // customized extension: store errorMessage from original exception 
        StringBuilder errorMessage = new StringBuilder(e.getMessage()).append("\n");
        if (e instanceof PersistenceException) {
          if (e.getCause() instanceof PersistenceException) {
            e = (PersistenceException) e.getCause();
            // customized extension: add error message from underlying exception
            errorMessage.append(e.getMessage()).append("\n");
          }
          if (e.getCause() instanceof SQLException) {
            this.initExceptionTranslator();
            // customized extension: pass errorMessage 
            return this.exceptionTranslator.translate(errorMessage.toString(), null, (SQLException) e.getCause());
          }
          return new MyBatisSystemException(e);
        } 
        return null;
      }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant