-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add error handling and logging interceptors (#1449)
- Loading branch information
1 parent
4f3aa2f
commit c240ea3
Showing
7 changed files
with
136 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
github-bot/src/main/java/io/github/martinwitt/laughing_train/commons/cdi/ErrorHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package io.github.martinwitt.laughing_train.commons.cdi; | ||
|
||
import jakarta.interceptor.InterceptorBinding; | ||
import java.lang.annotation.*; | ||
|
||
/** | ||
* This annotation is used to mark a method as an error handled. This means that if an exception is | ||
* thrown in the method, it will be caught and the method will return a {@link | ||
* io.github.martinwitt.laughing_train.commons.result.Result} with the error. | ||
*/ | ||
@InterceptorBinding | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR}) | ||
@Inherited | ||
public @interface ErrorHandler {} |
21 changes: 21 additions & 0 deletions
21
...rc/main/java/io/github/martinwitt/laughing_train/commons/cdi/ErrorHandlerInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package io.github.martinwitt.laughing_train.commons.cdi; | ||
|
||
import io.github.martinwitt.laughing_train.commons.result.Result; | ||
import io.quarkus.logging.Log; | ||
import jakarta.interceptor.AroundInvoke; | ||
import jakarta.interceptor.Interceptor; | ||
import jakarta.interceptor.InvocationContext; | ||
|
||
@ErrorHandler | ||
@Interceptor | ||
public class ErrorHandlerInterceptor { | ||
@AroundInvoke | ||
Object errorToResult(InvocationContext context) { | ||
try { | ||
return context.proceed(); | ||
} catch (Exception e) { | ||
Log.error("Error in method " + context.getMethod().getName() + ": " + e.getMessage()); | ||
return Result.error(e); | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
github-bot/src/main/java/io/github/martinwitt/laughing_train/commons/cdi/Logged.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package io.github.martinwitt.laughing_train.commons.cdi; | ||
|
||
import jakarta.interceptor.InterceptorBinding; | ||
import java.lang.annotation.*; | ||
|
||
@InterceptorBinding | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR}) | ||
@Inherited | ||
public @interface Logged { | ||
|
||
String value() default ""; | ||
} |
26 changes: 26 additions & 0 deletions
26
...bot/src/main/java/io/github/martinwitt/laughing_train/commons/cdi/LoggingInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package io.github.martinwitt.laughing_train.commons.cdi; | ||
|
||
import io.quarkus.logging.Log; | ||
import jakarta.interceptor.AroundInvoke; | ||
import jakarta.interceptor.Interceptor; | ||
import jakarta.interceptor.InvocationContext; | ||
import java.lang.reflect.Method; | ||
|
||
@Logged | ||
@Interceptor | ||
public class LoggingInterceptor { | ||
|
||
@AroundInvoke | ||
Object logInvocation(InvocationContext context) throws Exception { | ||
Method method = context.getMethod(); | ||
if (!method.isAnnotationPresent(Logged.class)) { | ||
return context.proceed(); | ||
} | ||
String value = method.getAnnotation(Logged.class).value(); | ||
if (value.isEmpty()) { | ||
value = "Invoking: " + method.getName(); | ||
} | ||
Log.info(value); | ||
return context.proceed(); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...est/java/io/github/martinwitt/laughing_train/commons/cdi/ErrorHandlerInterceptorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package io.github.martinwitt.laughing_train.commons.cdi; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import io.github.martinwitt.laughing_train.commons.result.Result; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.inject.Inject; | ||
import org.junit.jupiter.api.Test; | ||
|
||
@QuarkusTest | ||
class ErrorHandlerInterceptorTest { | ||
|
||
@Inject Foo foo; | ||
|
||
@Test | ||
void errorToResult() { | ||
var a = foo.bar(); | ||
assertThat(a).isInstanceOf(Result.class); | ||
} | ||
|
||
@ApplicationScoped | ||
static class Foo { | ||
@ErrorHandler | ||
@Logged | ||
public Object bar() { | ||
throw new RuntimeException("Test"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters