001/* 002 * SPDX-License-Identifier: Apache-2.0 003 * 004 * Copyright 2025-2026 The Enola <https://enola.dev> Authors 005 * 006 * Licensed under the Apache License, Version 2.0 (the "License"); 007 * you may not use this file except in compliance with the License. 008 * You may obtain a copy of the License at 009 * 010 * https://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package dev.enola.cli.common; 019 020import picocli.CommandLine; 021 022public class QuietExecutionExceptionHandler implements CommandLine.IExecutionExceptionHandler { 023 024 private final Application app; 025 026 public QuietExecutionExceptionHandler(Application app) { 027 this.app = app; 028 } 029 030 @Override 031 public int handleExecutionException( 032 Exception ex, CommandLine cmd, CommandLine.ParseResult parseResult) throws Exception { 033 if (app.loggingVerbosity > 0) { 034 cmd.getErr().println(cmd.getColorScheme().richStackTraceString(ex)); 035 } else { 036 var intro = "Internal Problem occurred, add -vvv flags for technical details: "; 037 cmd.getErr().print(cmd.getColorScheme().optionText(intro)); 038 Throwable e = ex; 039 while (e != null) { 040 var type = e.getClass().getSimpleName(); 041 var msg = e.getMessage(); 042 var full = type + (msg != null ? ": " + msg : ""); 043 cmd.getErr().println(cmd.getColorScheme().errorText(full)); 044 e = e.getCause(); 045 if (e != null) { 046 cmd.getErr().print("caused by: "); 047 } 048 } 049 } 050 cmd.getErr().flush(); 051 return cmd.getExitCodeExceptionMapper() != null 052 ? cmd.getExitCodeExceptionMapper().getExitCode(ex) 053 : cmd.getCommandSpec().exitCodeOnExecutionException(); 054 } 055}