From a5dbb0a8f66526cec574ea4390ecf1c953b0b24a Mon Sep 17 00:00:00 2001 From: Joe Onorato Date: Thu, 22 Oct 2020 13:39:02 -0700 Subject: [PATCH] CommandException to cleanly exit product-config on error. Test: none Change-Id: I06bb80fe1cc21d77ca3e32ac3110a08fc8b5af54 --- .../build/config/CommandException.java | 39 ++++++++++++++++ .../src/com/android/build/config/Main.java | 46 +++++++++++++------ 2 files changed, 71 insertions(+), 14 deletions(-) create mode 100644 tools/product_config/src/com/android/build/config/CommandException.java diff --git a/tools/product_config/src/com/android/build/config/CommandException.java b/tools/product_config/src/com/android/build/config/CommandException.java new file mode 100644 index 000000000..f1a2c39f0 --- /dev/null +++ b/tools/product_config/src/com/android/build/config/CommandException.java @@ -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); + } +} diff --git a/tools/product_config/src/com/android/build/config/Main.java b/tools/product_config/src/com/android/build/config/Main.java index 766974293..6d6214d14 100644 --- a/tools/product_config/src/com/android/build/config/Main.java +++ b/tools/product_config/src/com/android/build/config/Main.java @@ -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); } }