davsclaus opened a new pull request, #28172:
URL: https://github.com/apache/camel/pull/28172

   https://issues.apache.org/jira/browse/CAMEL-25876
   
   `MainSupport.run()` passes a failure to start to the caller on purpose. The 
main class of an application (camel export, the archetypes, most apps) does not 
catch it, so the exception leaves `main()` and only the JVM prints it, on 
stderr. The log file of the app ended before the failure. With the fat jar of 
camel export, the failure is also wrapped in an `InvocationTargetException`, 
because the launcher calls `main` by reflection. KameletMain, the default 
runtime of `camel run`, already logs the failure. Spring Boot logs "Application 
run failed" and Quarkus "Failed to start application", each once.
   
   ## Changes
   
   - `MainSupport.logStartupFailure(e)` logs `Error starting Camel: ...` with 
the stack trace. `Main` (and so the fat-jar `Main` of camel export) and 
`KameletMain` call it from `doFail`. KameletMain logs the same line as before, 
with the same logger.
   - It is not in the base class: Camel Quarkus's `CamelMain` extends 
`MainCommandLineSupport` and logs the failure itself.
   - As Spring Boot does, the JVM then does not print the same failure a second 
time. The thread that started Camel gets an uncaught exception handler 
(`LoggedStartupFailure`) that skips the logged failure, also when it is wrapped 
(`RuntimeCamelException`, `InvocationTargetException`). Any other exception 
goes to the handler the thread had before. The handler is only set when ERROR 
is logged, so an app without logging still sees the failure on stderr.
   - The 4.24 upgrade guide has a section on it.
   
   ## Tested end to end
   
   With `camel run --runtime=main` of a route that fails at startup, using this 
camel-main and #28171:
   - the console shows `ERROR ... Error starting Camel: 
org.apache.camel.FailedToCreateRouteException: ...` once, and no `Exception in 
thread "main"`;
   - the exit code is still 1;
   - the log file, kept as the previous run (CAMEL-25832), has the failure, so 
`camel log --previous` says *"... with exit code 1: Error starting Camel: 
...FailedToCreateRouteException: Failed to create route: boom ..."*.
   
   ## Tests
   
   - `LoggedStartupFailureTest` (3): the logged failure is not printed again, 
also wrapped; another exception is; a later failure replaces the one before.
   - `MainStartupFailureTest` (2): a Main whose routes fail to configure logs 
"Error starting Camel" once, and the JVM does not print it again.
   - camel-main 304 tests pass, camel-kamelet-main 61.
   
   _Claude Code on behalf of davsclaus_
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   
   https://claude.ai/code/session_01STT6whBgK1AqsSsUKrnE8m


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to