Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.jspecify.annotations.Nullable;

import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;

import io.grpc.Metadata;
import io.grpc.Status;
Expand All @@ -38,6 +39,7 @@
* methods in {@link GrpcAdvice @GrpcAdvice} beans.
*
* @author Oleksandr Shevchenko
* @author Jialin Chen
* @see GrpcAdvice
* @see GrpcExceptionHandler
*/
Expand Down Expand Up @@ -168,6 +170,7 @@ private Class<?> convertToClass(Parameter parameter) {
private Object invokeMappedMethodSafely(Method mappedMethod, Object instanceOfMappedMethod,
Object[] instancedParams) throws Throwable {
try {
ReflectionUtils.makeAccessible(mappedMethod);
return mappedMethod.invoke(instanceOfMappedMethod, instancedParams);
}
catch (InvocationTargetException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.grpc.server.advice.support.AdviceVisibilitySamples;

import io.grpc.Metadata;
import io.grpc.Status;
Expand All @@ -36,9 +37,32 @@
* Tests for {@link GrpcAdviceExceptionHandler}.
*
* @author Oleksandr Shevchenko
* @author Jialin Chen
*/
class GrpcAdviceExceptionHandlerTests {

@Test
void handlesExceptionInPublicAdviceFromAnotherPackage() {
assertAdviceHandlesException(AdviceVisibilitySamples.PublicAdvice.class);
}

@Test
void handlesExceptionInPackagePrivateAdviceFromAnotherPackage() {
assertAdviceHandlesException(AdviceVisibilitySamples.packagePrivateAdviceType());
}

private void assertAdviceHandlesException(Class<?> adviceType) {
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(adviceType,
GrpcAdviceDiscoverer.class, GrpcExceptionHandlerMethodResolver.class,
GrpcAdviceExceptionHandler.class)) {
GrpcAdviceExceptionHandler handler = context.getBean(GrpcAdviceExceptionHandler.class);
StatusException result = handler.handleException(new IllegalArgumentException("bad argument"));
assertThat(result).isNotNull();
assertThat(result.getStatus().getCode()).isEqualTo(Status.Code.INVALID_ARGUMENT);
assertThat(result.getStatus().getDescription()).isEqualTo("bad argument");
}
}

@Test
void handlesExceptionWithMappedHandler() {
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfig.class)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2026-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.grpc.server.advice.support;

import org.springframework.grpc.server.advice.GrpcAdvice;
import org.springframework.grpc.server.advice.GrpcExceptionHandler;

import io.grpc.Status;

/**
* Advice fixtures in a different package from the exception handler implementation.
*
* @author Jialin Chen
*/
public final class AdviceVisibilitySamples {

private AdviceVisibilitySamples() {
}

public static Class<?> packagePrivateAdviceType() {
return PackagePrivateAdvice.class;
}

@GrpcAdvice
public static class PublicAdvice {

@GrpcExceptionHandler
public Status handleIllegalArgument(IllegalArgumentException exception) {
return Status.INVALID_ARGUMENT.withDescription(exception.getMessage());
}

}

@GrpcAdvice
static class PackagePrivateAdvice {

@GrpcExceptionHandler
public Status handleIllegalArgument(IllegalArgumentException exception) {
return Status.INVALID_ARGUMENT.withDescription(exception.getMessage());
}

}

}
Loading