Skip to content

Commit

Permalink
feat: Add error handling and logging interceptors (#1449)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinWitt authored Jan 12, 2024
1 parent 4f3aa2f commit c240ea3
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,27 @@ public List<GitHubCommit> getCommits() {
return commits;
}

/**
* Adds a GitHub commit to the list of commits in the RemoteProject. If the commit is already in
* the list, it is not added.
*
* @param commit the GitHub commit to add.
*/
public void addCommitHash(GitHubCommit commit) {
if (commits.contains(commit)) {
return;
}
commits.add(commit);
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
RemoteProject that = (RemoteProject) obj;
return Objects.equals(projectName, that.projectName)
&& Objects.equals(projectUrl, that.projectUrl)
Expand All @@ -60,4 +73,18 @@ public int hashCode() {
public RemoteProject withProjectUrl(String projectUrl) {
return new RemoteProject(projectName, projectUrl);
}

@Override
public String toString() {
return "RemoteProject{"
+ "projectName='"
+ projectName
+ '\''
+ ", projectUrl='"
+ projectUrl
+ '\''
+ ", commits="
+ commits
+ '}';
}
}
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 {}
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);
}
}
}
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 "";
}
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();
}
}
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");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.martinwitt.laughing_train.mining;

import static org.assertj.core.api.Assertions.assertThat;

import io.github.martinwitt.laughing_train.commons.GitProject;
import io.github.martinwitt.laughing_train.data.result.CodeAnalyzerResult;
import io.github.martinwitt.laughing_train.domain.entity.RemoteProject;
Expand All @@ -12,8 +14,6 @@
import org.instancio.Instancio;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

@QuarkusTest
class AnalyzerResultsPersistenceTest {

Expand Down

0 comments on commit c240ea3

Please sign in to comment.