code: private static boolean mySqlErrorCodeMatches(Exception e, int errorCode)
      {
          return Throwables.getCausalChain(e).stream()
                  .filter(SQLException.class::isInstance)
                  .map(SQLException.class::cast)
                  .filter(t -> t.getErrorCode() == errorCode)
                  .map(Throwable::getStackTrace)
                  .anyMatch(isMySQLException());
      }
docs: Check if an exception is caused by a MySQL exception of certain error code