CLIStarter: exceptions handling
authorFrantišek Kučera <franta-hg@frantovo.cz>
Wed, 09 Jul 2014 00:20:49 +0200
changeset 5788836193c9ba
parent 56 5e79450a521e
child 58 c11792ae6489
CLIStarter: exceptions handling
java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLIStarter.java
     1.1 --- a/java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLIStarter.java	Thu Jul 03 00:42:50 2014 +0200
     1.2 +++ b/java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLIStarter.java	Wed Jul 09 00:20:49 2014 +0200
     1.3 @@ -20,17 +20,17 @@
     1.4  import cz.frantovo.alt2xml.out.Action;
     1.5  import cz.frantovo.alt2xml.out.ActionContext;
     1.6  import cz.frantovo.alt2xml.out.ActionFactory;
     1.7 -import java.io.File;
     1.8 -import java.io.OutputStream;
     1.9 -import java.util.Arrays;
    1.10 +import cz.frantovo.alt2xml.out.OutputActionException;
    1.11  import java.util.HashMap;
    1.12  import java.util.Map;
    1.13  import java.util.ServiceLoader;
    1.14  import java.util.logging.Level;
    1.15 +import java.util.logging.LogRecord;
    1.16  import java.util.logging.Logger;
    1.17 +import javax.xml.parsers.ParserConfigurationException;
    1.18  import javax.xml.parsers.SAXParser;
    1.19  import javax.xml.parsers.SAXParserFactory;
    1.20 -import org.xml.sax.InputSource;
    1.21 +import org.xml.sax.SAXException;
    1.22  
    1.23  /**
    1.24   *
    1.25 @@ -38,9 +38,22 @@
    1.26   */
    1.27  public class CLIStarter {
    1.28  
    1.29 +	// help:exit-codes
    1.30 +	public static final int EXIT_SUCCESS = 0; // doc:success
    1.31 +	public static final int EXIT_UNEXPECTED_ERROR = 1; // doc:unexpected error (probably bug)
    1.32 +	// 2 is reserved: http://www.tldp.org/LDP/abs/html/exitcodes.html#EXITCODESREF
    1.33 +	public static final int EXIT_SAX_ERROR = 3; // doc:SAX error
    1.34 +	public static final int EXIT_CLI_PARSE_ERROR = 4; // doc:CLI options parse error
    1.35 +	public static final int EXIT_CLI_VALIDATE_ERROR = 5; // doc:CLI options validation error
    1.36 +	public static final int EXIT_CONFIGURATION_ERROR = 6; // doc:configuration error
    1.37 +	public static final int EXIT_OUTPUT_ACTION_ERROR = 7; // doc:formatting error
    1.38 +	// public static final int EXIT_BATCH_ERROR = 8; // doc:batch error
    1.39  	private static final Logger log = Logger.getLogger(CLIStarter.class.getName());
    1.40 +	private static final String PROGRAM_NAME = "alt2xml";
    1.41  
    1.42  	public static void main(String[] args) {
    1.43 +		log.log(Level.FINE, "Starting " + PROGRAM_NAME);
    1.44 +		int exitCode;
    1.45  
    1.46  		try {
    1.47  			CLIParser cliParser = new CLIParser();
    1.48 @@ -58,7 +71,9 @@
    1.49  			ActionFactory actionFactory = actionFactories.get(actionCode);
    1.50  
    1.51  			if (actionFactory == null) {
    1.52 -				log.log(Level.SEVERE, "No such output action with code: {0}", actionCode);
    1.53 +				InvalidOptionsException e = new InvalidOptionsException();
    1.54 +				e.addProblem(new InvalidOptionsException.OptionProblem("No such output action with code: " + actionCode));
    1.55 +				throw e;
    1.56  			} else {
    1.57  
    1.58  				ActionContext actionContext = new ActionContext(cliOptions.getOutputStream());
    1.59 @@ -70,9 +85,28 @@
    1.60  				action.run(p, cliOptions.getInputSource());
    1.61  			}
    1.62  
    1.63 -		} catch (Exception e) {
    1.64 -			log.log(Level.SEVERE, "Error during processing: " + Arrays.toString(args), e);
    1.65 +			log.log(Level.FINE, "All done");
    1.66 +			exitCode = EXIT_SUCCESS;
    1.67 +		} catch (CLIParserException e) {
    1.68 +			log.log(Level.SEVERE, "Unable to parse CLI options", e);
    1.69 +			exitCode = EXIT_CLI_PARSE_ERROR;
    1.70 +		} catch (InvalidOptionsException e) {
    1.71 +			log.log(Level.SEVERE, "Invalid CLI options", e);
    1.72 +			for (InvalidOptionsException.OptionProblem p : e.getProblems()) {
    1.73 +				LogRecord r = new LogRecord(Level.SEVERE, "Option problem: {0}");
    1.74 +				r.setThrown(p.getException());
    1.75 +				r.setParameters(new Object[]{p.getDescription()});
    1.76 +				log.log(r);
    1.77 +			}
    1.78 +			exitCode = EXIT_CLI_VALIDATE_ERROR;
    1.79 +		} catch (OutputActionException e) {
    1.80 +			log.log(Level.SEVERE, "Ouput module problem", e);
    1.81 +			exitCode = EXIT_OUTPUT_ACTION_ERROR;
    1.82 +		} catch (ParserConfigurationException | SAXException e) {
    1.83 +			log.log(Level.SEVERE, "Unable to create SAX parser", e);
    1.84 +			exitCode = EXIT_SAX_ERROR;
    1.85  		}
    1.86  
    1.87 +		System.exit(exitCode);
    1.88  	}
    1.89  }