View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.jdbc;
8   
9   import org.mule.api.config.ExceptionReader;
10  import org.mule.util.StringUtils;
11  
12  import java.sql.SQLException;
13  import java.util.HashMap;
14  import java.util.Map;
15  
16  /**
17   * Surfaces information about SQLExceptions such as the code and sql state. Also uses
18   * the NextException to find the cause
19   */
20  public class SQLExceptionReader implements ExceptionReader
21  {
22      public String getMessage(Throwable t)
23      {
24          SQLException e = (SQLException) t;
25          return e.getMessage() + "(SQL Code: " + e.getErrorCode() + ", SQL State: + " + e.getSQLState() + ")";
26      }
27  
28      public Throwable getCause(Throwable t)
29      {
30          SQLException e = (SQLException) t;
31          Throwable cause = e.getNextException();
32          if (cause == null)
33          {
34              cause = e.getCause();
35          }
36          return cause;
37      }
38  
39      public Class<?> getExceptionType()
40      {
41          return SQLException.class;
42      }
43  
44      /**
45       * Returns a map of the non-stanard information stored on the exception
46       * 
47       * @param t the exception to extract the information from
48       * @return a map of the non-stanard information stored on the exception
49       */
50      public Map<?, ?> getInfo(Throwable t)
51      {
52          SQLException e = (SQLException) t;
53          
54          Map<String, Object> info = new HashMap<String, Object>();
55          if (e.getErrorCode() != 0)
56          {
57              info.put("SQL Code", String.valueOf(e.getErrorCode()));
58          }
59          if (e.getSQLState() != null && !e.getSQLState().equals(StringUtils.EMPTY))
60          {
61              info.put("SQL State", e.getSQLState());
62          }
63          return info;
64      }
65  }