Java method step failures hide the real error behind `InvocationTargetException`

Aperta Adatta ai principianti
#808 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
90/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
java
Ambito
testing

Direzione di ricerca

Inizia da JavaMethodExecutorImpl.executeWithParams, dove reflection avvolge gli errori dell’executor in InvocationTargetException. Aggiungi un test per un executor che genera un’eccezione con un proprio messaggio, verificando che il log e l’errore propagato espongano l’eccezione di destinazione preservando il prefisso esistente e la catena delle cause. Esegui la suite di test unitari pertinente per JavaMethodExecutorImpl per confermare il comportamento.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

good first issue

Summary

When a custom Java Executor(or Answerer) method (a step with a url such as org.example.MyExecutor) throws an exception, the error log, the step's report message and the JUnit failure message only say Invocation failed for method X in class Y. The executor's own error message is lost.

Users have to search the console stack trace for the Caused by: line to find out what went wrong.

Problem

JavaMethodExecutorImpl.executeWithParams calls the executor through reflection (method.invoke). Any exception the executor throws arrives wrapped in a java.lang.reflect.InvocationTargetException, which has no message of its own. The generic catch block logs and rethrows it as is:

} catch (Exception e) {
    String errMsg = format("Java exec(): Invocation failed for method %s in class %s", methodName, qualifiedClassName);
    LOGGER.error(errMsg + ". Exception - " + e);
    throw new RuntimeException(errMsg, e);
}

For example, if MyExecutor.upload() throws IllegalArgumentException("Could not get OAuth2 token: ..."), users see:

Where What users see today
ERROR log (JavaMethodExecutorImpl) Java exec(): Invocation failed for method upload in class org.example.MyExecutor. Exception - java.lang.reflect.InvocationTargetException
Step exception message in the report (ZeroCodeMultiStepsScenarioRunnerImpl, .exceptionMessage(ex.getMessage())) java.lang.RuntimeException: Java exec(): Invocation failed for method upload in class org.example.MyExecutor
JUnit failure message ZeroCode Step execution failed. Details:java.lang.RuntimeException: Java exec(): Invocation failed for method upload in class org.example.MyExecutor

Could not get OAuth2 token: ... doesn't appears in any of them. It is only in the full stack trace that the runner prints to the console, deep in the output.

The workaround today is for every executor/answer-engine author to catch, log and printStackTrace() in their own code. That duplicates the trace, and it is easy to forget.

Expected behaviour

The message of the exception the executor actually threw should appear in the ERROR log, the report and the JUnit failure message, e.g.:

Java exec(): Invocation failed for method upload in class org.example.MyExecutor. Cause: java.lang.IllegalArgumentException: Could not get OAuth2 token: ...

Proposed fix

Catch InvocationTargetException separately in JavaMethodExecutorImpl.executeWithParams:

  • add the target exception to the message;
  • log the target exception's full stack trace through the logger.

The message prefix and the exception cause chain stay the same, so existing behaviour and tests are unaffected. It is a small change in one file plus a unit test coverage.

Lingua principale
Java
Stelle
1k
Fork
455
Merge medio
13h 35m
PR unite (30g)
5

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di authorjapps/zerocode

Tutte le issue di authorjapps/zerocode

Issue simili

Altre issue su Java

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.