spring-projects / spring-shell

Spring based shell
http://projects.spring.io/spring-shell/
Apache License 2.0
734 stars 396 forks source link

Spring Boot leaks stacktrace in non-intercative mode #928

Closed oscarbrookssynacor closed 11 months ago

oscarbrookssynacor commented 11 months ago

Non-interactive mode, even with a global exception handler still prints a stacktrace on a custom exception.

Exception in thread "main" java.lang.IllegalStateException: Failed to execute ApplicationRunner
        at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:765)
        at org.springframework.boot.SpringApplication.lambda$callRunners$3(SpringApplication.java:752)
        at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
        at java.base/java.util.stream.SortedOps$SizedRefSortingSink.end(SortedOps.java:357)
        at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:510)
        at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
        at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
        at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
        at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
        at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:596)
        at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:750)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:323)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295)
        at com.example.cli.CliApplication.main(CliApplication.java:10)
Caused by: org.springframework.shell.boot.ExitCodeAutoConfiguration$ShellExitCodeException
        at org.springframework.shell.boot.ExitCodeAutoConfiguration.lambda$exitCodeExceptionProvider$0(ExitCodeAutoConfiguration.java:55)
        at org.springframework.shell.Shell.run(Shell.java:179)
        at org.springframework.shell.jline.NonInteractiveShellRunner.run(NonInteractiveShellRunner.java:146)
        at org.springframework.shell.DefaultShellApplicationRunner.run(DefaultShellApplicationRunner.java:65)
        at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:762)
        ... 14 more

There does not appear to be a way to suppress it.

@Component
public class GlobalExceptionResolver implements CommandExceptionResolver {

    public GlobalExceptionResolver() {
        super();
    }

    @Override
    public CommandHandlingResult resolve(Exception e) {
        if (e instanceof AbstractException) {
            return ((AbstractException) e).handle();
        }
        return CommandHandlingResult.of("An unexpected exception occured", 1);
    }
}
public class ServiceUnavailableException extends AbstractException {

    private String message = "";

    public ServiceUnavailableException() {
        super();
    }

    public ServiceUnavailableException(String message) {
        super(message);
        this.message = message;
    }

    @Override
    public CommandHandlingResult handle(){
        return CommandHandlingResult.of(String.format("Admin Service unavailable. Error: %s\n", message), 42);
    }
}
oscarbrookssynacor commented 11 months ago

One way to suppress it appears to be by doing the following

@PostConstruct
    public void exitCodeExceptionProvider() {
        shell.setExitCodeExceptionProvider(null);
    }

However in that case the exit code is always 0

oscarbrookssynacor commented 11 months ago

My apologies, it appears that I just missed this setting: logging.pattern.console=

hopefully someone finds this helpful in the future.

kulkarnipradnyas commented 1 month ago

My apologies, it appears that I just missed this setting: logging.pattern.console=

hopefully someone finds this helpful in the future.

Can you please share console pattern here