exceptions: Jackson 3 unchecked JacksonException hierarchy, Spring Boot 4.2 error handling, OpenRewrite and sed migration
Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b4HkWoC7hLzk61vZa8
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
package com.ankurm.orders;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/** A caller that sits above OrderCodec.read and trusts its throws clause. */
|
||||
public class Importer {
|
||||
|
||||
private final OrderCodec codec = new OrderCodec();
|
||||
|
||||
public String importOne(String json) {
|
||||
try (var in = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8))) {
|
||||
return "imported " + codec.read(in).id();
|
||||
} catch (IOException e) {
|
||||
return "rejected";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
package com.ankurm.orders;
|
||||
|
||||
public record Order(long id, int quantity) {}
|
||||
@@ -0,0 +1,70 @@
|
||||
package com.ankurm.orders;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Map;
|
||||
|
||||
/** Six common ways Jackson 2 code deals with failure. */
|
||||
public class OrderCodec {
|
||||
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
/** 1. declare it */
|
||||
public Order parse(String json) throws JsonProcessingException {
|
||||
return mapper.readValue(json, Order.class);
|
||||
}
|
||||
|
||||
/** 2. catch the subtypes, most specific first */
|
||||
public String diagnose(String json) {
|
||||
try {
|
||||
mapper.readValue(json, Order.class);
|
||||
return "ok";
|
||||
} catch (JsonParseException e) {
|
||||
return "not JSON";
|
||||
} catch (JsonMappingException e) {
|
||||
return "bad field " + e.getPath().get(0).getFieldName();
|
||||
} catch (JsonProcessingException e) {
|
||||
return "other";
|
||||
}
|
||||
}
|
||||
|
||||
/** 3. catch IOException around a call that only Jackson can fail */
|
||||
public Order orDefault(String json) {
|
||||
try {
|
||||
return mapper.readValue(json, Order.class);
|
||||
} catch (IOException e) {
|
||||
return new Order(0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
/** 4. one catch for the file read and the parse */
|
||||
public Order load(Path file) {
|
||||
try {
|
||||
String json = Files.readString(file);
|
||||
return mapper.readValue(json, Order.class);
|
||||
} catch (IOException e) {
|
||||
return new Order(0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
/** 5. the caller was told to expect IOException */
|
||||
public Order read(InputStream in) throws IOException {
|
||||
return mapper.readValue(in, Order.class);
|
||||
}
|
||||
|
||||
/** 6. convertValue reported problems as IllegalArgumentException */
|
||||
public boolean isOrder(Map<String, Object> map) {
|
||||
try {
|
||||
mapper.convertValue(map, Order.class);
|
||||
return true;
|
||||
} catch (IllegalArgumentException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.ankurm.orders;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Map;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
/** Pins the behaviour before the upgrade. The migrated copy has to pass the same assertions. */
|
||||
class OrderCodecTest {
|
||||
|
||||
static final String BAD = "{\"id\": 1, \"quantity\": }";
|
||||
|
||||
final OrderCodec codec = new OrderCodec();
|
||||
|
||||
@Test
|
||||
void diagnoses() {
|
||||
assertEquals("not JSON", codec.diagnose(BAD));
|
||||
assertEquals("bad field quantity", codec.diagnose("{\"id\":1,\"quantity\":\"two\"}"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void fallsBackOnBadText() {
|
||||
assertEquals(new Order(0, 0), codec.orDefault(BAD));
|
||||
}
|
||||
|
||||
@Test
|
||||
void fallsBackOnBadFile(@TempDir Path dir) throws IOException {
|
||||
Path file = Files.writeString(dir.resolve("order.json"), BAD);
|
||||
assertEquals(new Order(0, 0), codec.load(file));
|
||||
}
|
||||
|
||||
@Test
|
||||
void callerRejectsBadStream() {
|
||||
assertEquals("rejected", new Importer().importOne(BAD));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsBadMap() {
|
||||
assertFalse(codec.isOrder(Map.of("id", "abc")));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user