Skip to content

Commit 0951f87

Browse files
committed
Improve error messages in SourceMethodArgumentResolver
Closes gh-429
1 parent 28e3a8c commit 0951f87

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

spring-graphql/src/main/java/org/springframework/graphql/data/method/annotation/support/SourceMethodArgumentResolver.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,8 +26,8 @@
2626

2727
import org.springframework.core.MethodParameter;
2828
import org.springframework.graphql.data.method.HandlerMethodArgumentResolver;
29-
import org.springframework.util.Assert;
3029
import org.springframework.util.ClassUtils;
30+
import org.springframework.util.StringUtils;
3131

3232
/**
3333
* Resolver for the source/parent of a field, obtained via
@@ -66,10 +66,21 @@ private static boolean isExcludedSimpleValueType(Class<?> type) {
6666
@Override
6767
public Object resolveArgument(MethodParameter parameter, DataFetchingEnvironment environment) {
6868
Object source = environment.getSource();
69-
Assert.isInstanceOf(parameter.getParameterType(), source,
70-
"The declared parameter of type '" + parameter.getParameterType() + "' " +
71-
"does not match the type of the source Object '" + source.getClass() + "'.");
69+
if (source == null) {
70+
throw new IllegalStateException(formatArgumentError(parameter,
71+
" was not recognized by any resolver and there is no source/parent either. " +
72+
"Please, refer to the documentation for the full list of supported parameters."));
73+
}
74+
if (!parameter.getParameterType().isInstance(source)) {
75+
throw new IllegalStateException(formatArgumentError(parameter,
76+
" does not match the source Object type '" + source.getClass() + "'."));
77+
}
7278
return source;
7379
}
7480

81+
private static String formatArgumentError(MethodParameter param, String message) {
82+
return "Parameter [" + param.getParameterIndex() + "] in " +
83+
param.getExecutable().toGenericString() + (StringUtils.hasText(message) ? ": " + message : "");
84+
}
85+
7586
}

0 commit comments

Comments
 (0)