Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » How to handle Status and inform enduser of failure
How to handle Status and inform enduser of failure [message #906813] Sun, 02 September 2012 16:55
Mikael Petterson is currently offline Mikael Petterson
Messages: 152
Registered: July 2009
Senior Member
Hi,


I have some code that executes a command and returns exit code and errorMessage ( if execution fails). The code that makes this call is the following (simplified):


public Status twoWayMerge(String file1, String file2) {
		String [] errMsg = null;
		String[] command = new String[] { getExec(), file1, file2 };
		CommandLauncher launcher = new CommandLauncher();
		launcher.execute(command, null, null, null);
		//Check if command was ok.
		if(launcher.getExitValue() != 0){
			errMsg = launcher.getErrorOutput();
			if(errMsg == null){
				return new Status(IStatus.ERROR, "Plugin id here", "An unknown error occurred!");
			}else{
				StringBuffer sb = new StringBuffer();
				for (int i = 0; i < errMsg.length; i++) {
					sb.append(errMsg[i]);
					sb.append('\n');
				}
				return new Status(IStatus.ERROR, "Plugin id here",sb.toString());
			}
		}
		
		//everything was ok!
		return new Status(IStatus.OK, "Plugin id here", "Merge was ok");
		
	}



Since it is a long running Job I use the following to make the call.


public void execute() throws TeamException{
		// execute
		Job job = new Job("Merge") {
			protected IStatus run(IProgressMonitor monitor) {
				IStatus status = null;
				monitor.beginTask("Merge started...", 10);
				// Run long running task here
				// Add a factory here that can decide which launcher to use.
				AbstractDifference merge = MergeFactory.getMergeTool(ClearCasePreferences.getExtMergeTool());
				String vExtPath1 = resource.getLocation().toOSString()+"@@"+comparableVersion;
				String path = resource.getLocation().toOSString();//Dont use version extended path. Since view selects current version.
				if(base == null){
					status = merge.twoWayMerge(file1, file2);
				}else{
					status = merge.threeWayMerge(file1, file2, path);
				}
				
				if(!status.isOK()){
					//Throw exception
					throw new TeamException(status);
				}
				monitor.done();
				return status;
			}
		};
		job.setUser(true);
		job.schedule();
	}


Then I need to provide the user with a message if I have a failure of the command. I use the following code:

		ExternalMergeOperation extMergeOp = new ExternalMergeOperation(resource, comparableVersion, base);
		try {
			extMergeOp.execute();
		} catch (TeamException te) {
			ErrorDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "External Merge title", te.getMessage(), te.getStatus());
		}



Is this the recommend way to do it or is there an easier way to do it? Is there anything I can improve in code? I would be very happy if you give your opinion.

br,

//mike
Previous Topic:Autocorrect - the apple way
Next Topic:Please stop adding features.
Goto Forum:
  


Current Time: Sat Oct 25 10:34:10 GMT 2014

Powered by FUDForum. Page generated in 0.04456 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software