mybatis / mybatis/spring

MyBatisExceptionTranslator loose PersistenceException message

Open
#53 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
2.9k
Forks
2.6k
Avg merge
3d 4m
Merged PRs (30d)
8

Description

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;
      }

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at MyBatisExceptionTranslator and its translateExceptionIfPossible method, then trace how PersistenceException messages reach DataAccessException. The issue is done when the original ErrorContext message is preserved during translation, including when an underlying SQLException is handled.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.