Make "no adapter" error message friendlier.

In the very common case, there are no JsonQualifier annotations for the type.
This commit is contained in:
Eric Cochran
2018-04-08 00:48:38 -07:00
parent 2cc878da81
commit ba1318cc45
8 changed files with 28 additions and 20 deletions

View File

@@ -159,7 +159,7 @@ class KotlinCodeGenTest {
moshi.adapter(Triple::class.java) moshi.adapter(Triple::class.java)
fail() fail()
} catch (e: IllegalArgumentException) { } catch (e: IllegalArgumentException) {
assertThat(e).hasMessage("Platform class kotlin.Triple annotated [] " assertThat(e).hasMessage("Platform class kotlin.Triple (with no annotations) "
+ "requires explicit JsonAdapter to be registered") + "requires explicit JsonAdapter to be registered")
} }
} }
@@ -330,7 +330,7 @@ class KotlinCodeGenTest {
fail() fail()
} catch (e: IllegalArgumentException) { } catch (e: IllegalArgumentException) {
assertThat(e).hasMessage("No JsonAdapter for interface " + assertThat(e).hasMessage("No JsonAdapter for interface " +
"com.squareup.moshi.KotlinJsonAdapterTest\$Interface annotated []") "com.squareup.moshi.KotlinJsonAdapterTest\$Interface (with no annotations)")
} }
} }

View File

@@ -438,7 +438,7 @@ class KotlinJsonAdapterTest {
moshi.adapter(Triple::class.java) moshi.adapter(Triple::class.java)
fail() fail()
} catch (e: IllegalArgumentException) { } catch (e: IllegalArgumentException) {
assertThat(e).hasMessage("Platform class kotlin.Triple annotated [] " assertThat(e).hasMessage("Platform class kotlin.Triple (with no annotations) "
+ "requires explicit JsonAdapter to be registered") + "requires explicit JsonAdapter to be registered")
} }
} }
@@ -609,7 +609,7 @@ class KotlinJsonAdapterTest {
fail() fail()
} catch (e: IllegalArgumentException) { } catch (e: IllegalArgumentException) {
assertThat(e).hasMessage("No JsonAdapter for interface " + assertThat(e).hasMessage("No JsonAdapter for interface " +
"com.squareup.moshi.kotlin.KotlinJsonAdapterTest\$Interface annotated []") "com.squareup.moshi.kotlin.KotlinJsonAdapterTest\$Interface (with no annotations)")
} }
} }

View File

@@ -29,6 +29,7 @@ import javax.annotation.Nullable;
import static com.squareup.moshi.internal.Util.canonicalize; import static com.squareup.moshi.internal.Util.canonicalize;
import static com.squareup.moshi.internal.Util.jsonAnnotations; import static com.squareup.moshi.internal.Util.jsonAnnotations;
import static com.squareup.moshi.internal.Util.typeAnnotatedWithAnnotations;
final class AdapterMethodsFactory implements JsonAdapter.Factory { final class AdapterMethodsFactory implements JsonAdapter.Factory {
private final List<AdapterMethod> toAdapters; private final List<AdapterMethod> toAdapters;
@@ -52,7 +53,7 @@ final class AdapterMethodsFactory implements JsonAdapter.Factory {
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
String missingAnnotation = toAdapter == null ? "@ToJson" : "@FromJson"; String missingAnnotation = toAdapter == null ? "@ToJson" : "@FromJson";
throw new IllegalArgumentException("No " + missingAnnotation + " adapter for " throw new IllegalArgumentException("No " + missingAnnotation + " adapter for "
+ type + " annotated " + annotations); + typeAnnotatedWithAnnotations(type, annotations));
} }
} else { } else {
delegate = null; delegate = null;

View File

@@ -29,6 +29,7 @@ import java.util.TreeMap;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import static com.squareup.moshi.internal.Util.resolve; import static com.squareup.moshi.internal.Util.resolve;
import static com.squareup.moshi.internal.Util.typeAnnotatedWithAnnotations;
/** /**
* Emits a regular class as a JSON object by mapping Java fields to JSON object properties. * Emits a regular class as a JSON object by mapping Java fields to JSON object properties.
@@ -56,9 +57,7 @@ final class ClassJsonAdapter<T> extends JsonAdapter<T> {
if (rawType.isInterface() || rawType.isEnum()) return null; if (rawType.isInterface() || rawType.isEnum()) return null;
if (Util.isPlatformType(rawType) && !Types.isAllowedPlatformType(rawType)) { if (Util.isPlatformType(rawType) && !Types.isAllowedPlatformType(rawType)) {
throw new IllegalArgumentException("Platform " throw new IllegalArgumentException("Platform "
+ type + typeAnnotatedWithAnnotations(type, annotations)
+ " annotated "
+ annotations
+ " requires explicit JsonAdapter to be registered"); + " requires explicit JsonAdapter to be registered");
} }
if (!annotations.isEmpty()) return null; if (!annotations.isEmpty()) return null;

View File

@@ -31,6 +31,7 @@ import javax.annotation.CheckReturnValue;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import static com.squareup.moshi.internal.Util.canonicalize; import static com.squareup.moshi.internal.Util.canonicalize;
import static com.squareup.moshi.internal.Util.typeAnnotatedWithAnnotations;
/** /**
* Coordinates binding between JSON values and Java objects. * Coordinates binding between JSON values and Java objects.
@@ -139,7 +140,8 @@ public final class Moshi {
} }
} }
throw new IllegalArgumentException("No JsonAdapter for " + type + " annotated " + annotations); throw new IllegalArgumentException(
"No JsonAdapter for " + typeAnnotatedWithAnnotations(type, annotations));
} }
@CheckReturnValue @CheckReturnValue
@@ -159,7 +161,7 @@ public final class Moshi {
if (result != null) return result; if (result != null) return result;
} }
throw new IllegalArgumentException("No next JsonAdapter for " throw new IllegalArgumentException("No next JsonAdapter for "
+ type + " annotated " + annotations); + typeAnnotatedWithAnnotations(type, annotations));
} }
/** Returns a new builder containing all custom factories used by the current instance. */ /** Returns a new builder containing all custom factories used by the current instance. */

View File

@@ -430,4 +430,9 @@ public final class Util {
} }
} }
} }
public static String typeAnnotatedWithAnnotations(Type type,
Set<? extends Annotation> annotations) {
return type + (annotations.isEmpty() ? " (with no annotations)" : " annotated " + annotations);
}
} }

View File

@@ -462,7 +462,7 @@ public final class AdapterMethodsTest {
fail(); fail();
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
assertThat(e).hasMessage("No @FromJson adapter for interface " assertThat(e).hasMessage("No @FromJson adapter for interface "
+ "com.squareup.moshi.AdapterMethodsTest$Shape annotated []"); + "com.squareup.moshi.AdapterMethodsTest$Shape (with no annotations)");
} }
} }
@@ -481,7 +481,7 @@ public final class AdapterMethodsTest {
fail(); fail();
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
assertThat(e).hasMessage("No @ToJson adapter for interface " assertThat(e).hasMessage("No @ToJson adapter for interface "
+ "com.squareup.moshi.AdapterMethodsTest$Shape annotated []"); + "com.squareup.moshi.AdapterMethodsTest$Shape (with no annotations)");
} }
} }

View File

@@ -541,7 +541,8 @@ public final class MoshiTest {
moshi.adapter(Types.subtypeOf(String.class)); moshi.adapter(Types.subtypeOf(String.class));
fail(); fail();
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
assertThat(e).hasMessage("No JsonAdapter for ? extends java.lang.String annotated []"); assertThat(e).hasMessage(
"No JsonAdapter for ? extends java.lang.String (with no annotations)");
} }
} }
@@ -551,7 +552,7 @@ public final class MoshiTest {
moshi.adapter(Types.supertypeOf(String.class)); moshi.adapter(Types.supertypeOf(String.class));
fail(); fail();
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
assertThat(e).hasMessage("No JsonAdapter for ? super java.lang.String annotated []"); assertThat(e).hasMessage("No JsonAdapter for ? super java.lang.String (with no annotations)");
} }
} }
@@ -908,22 +909,22 @@ public final class MoshiTest {
moshi.adapter(File.class); moshi.adapter(File.class);
fail(); fail();
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
assertThat(e).hasMessage( assertThat(e).hasMessage("Platform class java.io.File (with no annotations) "
"Platform class java.io.File annotated [] requires explicit JsonAdapter to be registered"); + "requires explicit JsonAdapter to be registered");
} }
try { try {
moshi.adapter(KeyGenerator.class); moshi.adapter(KeyGenerator.class);
fail(); fail();
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
assertThat(e).hasMessage( assertThat(e).hasMessage("Platform class javax.crypto.KeyGenerator (with no annotations) "
"Platform class javax.crypto.KeyGenerator annotated [] requires explicit JsonAdapter to be registered"); + "requires explicit JsonAdapter to be registered");
} }
try { try {
moshi.adapter(Pair.class); moshi.adapter(Pair.class);
fail(); fail();
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
assertThat(e).hasMessage( assertThat(e).hasMessage("Platform class android.util.Pair (with no annotations) "
"Platform class android.util.Pair annotated [] requires explicit JsonAdapter to be registered"); + "requires explicit JsonAdapter to be registered");
} }
} }