Skip to content

Commit

Permalink
Make error message better in json shortcircuit
Browse files Browse the repository at this point in the history
Signed-off-by: Coline PILOQUET <coline.piloquet@rte-france.com>
  • Loading branch information
colinepiloquet committed Feb 22, 2023
1 parent c0b199b commit c19c80e
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
package com.powsybl.shortcircuit;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.powsybl.commons.json.JsonUtil;
import com.powsybl.shortcircuit.json.ShortCircuitAnalysisJsonModule;
Expand Down Expand Up @@ -77,7 +78,7 @@ static void write(List<Fault> faults, Path jsonFile) {

static List<Fault> read(Path jsonFile) {
try (InputStream is = Files.newInputStream(jsonFile)) {
return createObjectMapper().readerForListOf(Fault.class).readValue(is);
return createObjectMapper().disable(DeserializationFeature.WRAP_EXCEPTIONS).readerForListOf(Fault.class).readValue(is);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
package com.powsybl.shortcircuit;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.powsybl.commons.json.JsonUtil;
import com.powsybl.shortcircuit.json.ShortCircuitAnalysisJsonModule;
Expand Down Expand Up @@ -143,7 +144,7 @@ public static void write(List<FaultParameters> parameters, Path jsonFile) {

public static List<FaultParameters> read(Path jsonFile) {
try (InputStream is = Files.newInputStream(jsonFile)) {
return createObjectMapper().readerForListOf(FaultParameters.class).readValue(is);
return createObjectMapper().disable(DeserializationFeature.WRAP_EXCEPTIONS).readerForListOf(FaultParameters.class).readValue(is);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
Expand Down Expand Up @@ -99,7 +98,7 @@ void readUnexpectedField() throws IOException {
Files.copy(getClass().getResourceAsStream("/FaultParametersFileInvalid.json"), fileSystem.getPath("/FaultParametersFileInvalid.json"));

Path path = fileSystem.getPath("/FaultParametersFileInvalid.json");
UncheckedIOException e = assertThrows(UncheckedIOException.class, () -> FaultParameters.read(path));
assertEquals("com.fasterxml.jackson.databind.JsonMappingException: Unexpected field: unexpected (through reference chain: java.util.ArrayList[0])", e.getMessage());
IllegalStateException e = assertThrows(IllegalStateException.class, () -> FaultParameters.read(path));
assertEquals("Unexpected field: unexpected", e.getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
package com.powsybl.shortcircuit.json;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.powsybl.commons.json.JsonUtil;
import com.powsybl.commons.test.AbstractConverterTest;
Expand Down Expand Up @@ -43,7 +44,7 @@ private static void write(List<FortescueValue> values, Path jsonFile) {

private static List<FortescueValue> read(Path jsonFile) {
try (InputStream is = Files.newInputStream(jsonFile)) {
return createObjectMapper().readerForListOf(FortescueValue.class).readValue(is);
return createObjectMapper().disable(DeserializationFeature.WRAP_EXCEPTIONS).readerForListOf(FortescueValue.class).readValue(is);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Expand All @@ -61,7 +62,7 @@ void readUnexpectedField() throws IOException {
Files.copy(getClass().getResourceAsStream("/FortescueValueInvalid.json"), fileSystem.getPath("/FortescueValueInvalid.json"));

Path path = fileSystem.getPath("/FortescueValueInvalid.json");
UncheckedIOException e = assertThrows(UncheckedIOException.class, () -> read(path));
assertEquals("com.fasterxml.jackson.databind.JsonMappingException: Unexpected field: unexpected (through reference chain: java.util.ArrayList[0])", e.getMessage());
IllegalStateException e = assertThrows(IllegalStateException.class, () -> read(path));
assertEquals("Unexpected field: unexpected", e.getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import com.powsybl.shortcircuit.Fault;
import org.junit.jupiter.api.Test;

import java.io.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
Expand Down Expand Up @@ -40,16 +40,16 @@ void readUnexpectedField() throws IOException {
Files.copy(getClass().getResourceAsStream("/FaultsFileInvalid.json"), fileSystem.getPath("/FaultsFileInvalid.json"));

Path path = fileSystem.getPath("/FaultsFileInvalid.json");
UncheckedIOException e = assertThrows(UncheckedIOException.class, () -> Fault.read(path));
assertEquals("com.fasterxml.jackson.databind.JsonMappingException: Unexpected field: unexpected (through reference chain: java.util.ArrayList[0])", e.getMessage());
IllegalStateException e = assertThrows(IllegalStateException.class, () -> Fault.read(path));
assertEquals("Unexpected field: unexpected", e.getMessage());
}

@Test
void readNoType() throws IOException {
Files.copy(getClass().getResourceAsStream("/FaultsFileNoType.json"), fileSystem.getPath("/FaultsFileNoType.json"));

Path path = fileSystem.getPath("/FaultsFileNoType.json");
UncheckedIOException e = assertThrows(UncheckedIOException.class, () -> Fault.read(path));
assertEquals("com.fasterxml.jackson.databind.JsonMappingException: Required type field is missing (through reference chain: java.util.ArrayList[0])", e.getMessage());
IllegalStateException e = assertThrows(IllegalStateException.class, () -> Fault.read(path));
assertEquals("Required type field is missing", e.getMessage());
}
}

0 comments on commit c19c80e

Please sign in to comment.