Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't wrap exceptions thrown by component adapters #1443

Merged
merged 1 commit into from
Dec 4, 2021
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 @@ -17,10 +17,13 @@

import static com.google.common.truth.Truth.assertThat;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import static org.junit.Assert.fail;

import com.squareup.moshi.FromJson;
import com.squareup.moshi.Json;
import com.squareup.moshi.JsonQualifier;
import com.squareup.moshi.JsonReader;
import com.squareup.moshi.JsonWriter;
import com.squareup.moshi.Moshi;
import com.squareup.moshi.ToJson;
import com.squareup.moshi.Types;
Expand All @@ -31,6 +34,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import okio.Buffer;
import org.junit.Test;

public final class RecordsTest {
Expand Down Expand Up @@ -192,4 +196,41 @@ public void jsonName() throws IOException {
}

public static record JsonName(@Json(name = "actualValue") int value) {}

/**
* We had a bug where we were incorrectly wrapping exceptions thrown when delegating to the
* JsonAdapters of component fields.
*/
@Test
public void memberEncodeDecodeThrowsExceptionException() throws IOException {
var throwingAdapter =
new Object() {
@ToJson
void booleanToJson(JsonWriter writer, boolean value) throws IOException {
throw new IOException("boom!");
}

@FromJson
boolean booleanFromJson(JsonReader reader) throws IOException {
throw new IOException("boom!");
}
};
var json = "{\"value\":true}";
Moshi throwingMoshi = this.moshi.newBuilder().add(throwingAdapter).build();
var adapter = throwingMoshi.adapter(BooleanRecord.class);
try {
adapter.fromJson(json);
fail();
} catch (IOException expected) {
assertThat(expected).hasMessageThat().isEqualTo("boom!");
}
try {
adapter.toJson(new Buffer(), new BooleanRecord(true));
fail();
} catch (IOException expected) {
assertThat(expected).hasMessageThat().isEqualTo("boom!");
}
}

public static record BooleanRecord(boolean value) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,9 @@ public void toJson(JsonWriter writer, T value) throws IOException {

for (var binding : componentBindingsArray) {
writer.name(binding.jsonName);
Object componentValue;
try {
binding.adapter.toJson(writer, binding.accessor.invoke(value));
componentValue = binding.accessor.invoke(value);
} catch (Throwable e) {
if (e instanceof InvocationTargetException ite) {
Throwable cause = ite.getCause();
Expand All @@ -209,6 +210,7 @@ public void toJson(JsonWriter writer, T value) throws IOException {
throw new AssertionError(e);
}
}
binding.adapter.toJson(writer, componentValue);
}

writer.endObject();
Expand Down