CommandException to cleanly exit product-config on error.

Test: none
Change-Id: I06bb80fe1cc21d77ca3e32ac3110a08fc8b5af54
This commit is contained in:
Joe Onorato 2020-10-22 13:39:02 -07:00
parent 55abbc75d2
commit a5dbb0a8f6
2 changed files with 71 additions and 14 deletions

View File

@ -0,0 +1,39 @@
/*
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.build.config;
/**
* Exception to indicate that a fatal error has occurred. Throwing this
* will cause errors to be printed, cleanup to occur, and the command to
* exit with a failure code.
*
* These are user errors. Throwing other exceptions will result in
* the stack trace being shown.
*/
public class CommandException extends RuntimeException {
public CommandException() {
super();
}
public CommandException(String message) {
super(message);
}
public CommandException(String message, Throwable chain) {
super(message, chain);
}
}

View File

@ -38,27 +38,45 @@ public class Main {
// TODO: Get the variables that were defined in starlark and use that to write
// out the make, soong and bazel input files.
mErrors.add(mErrors.ERROR_COMMAND_LINE, "asdf");
throw new RuntimeException("poop");
}
public static void main(String[] args) {
Errors errors = new Errors();
int exitCode = 0;
Options options = Options.parse(errors, args);
if (errors.hadError()) {
Options.printHelp(System.err);
try {
Options options = Options.parse(errors, args);
if (errors.hadError()) {
Options.printHelp(System.err);
System.err.println();
throw new CommandException();
}
switch (options.getAction()) {
case DEFAULT:
(new Main(errors, options)).run();
return;
case HELP:
Options.printHelp(System.out);
return;
}
} catch (CommandException ex) {
// These are user errors, so don't show a stack trace
exitCode = 1;
} catch (Throwable ex) {
// These are programming errors in the code of this tool, so print the exception.
// We'll try to print this. If it's something unrecoverable, then we'll hope
// for the best. We will still print the errors below, because they can be useful
// for debugging.
ex.printStackTrace(System.err);
System.err.println();
exitCode = 1;
} finally {
// Print errors and warnings
errors.printErrors(System.err);
System.exit(1);
}
switch (options.getAction()) {
case DEFAULT:
(new Main(errors, options)).run();
errors.printErrors(System.err);
return;
case HELP:
Options.printHelp(System.out);
return;
}
System.exit(exitCode);
}
}