Pass backend errors to the frontend

This commit is contained in:
Sandra
2022-01-31 05:36:01 +01:00
parent eb903752e6
commit 89b13a90d9

View File

@@ -45,7 +45,7 @@ public class ParseController {
} }
String[] parts = line.substring(1).split(":", 2); String[] parts = line.substring(1).split(":", 2);
if (parts.length != 2) { if (parts.length != 2) {
throw new RuntimeException("Cannot parse metadata: '" + line + "'"); return ResponseEntity.badRequest().body(Map.of("error", "Cannot parse metadata: '" + line + "'"));
} }
String key = parts[0].trim().toLowerCase(Locale.ROOT); String key = parts[0].trim().toLowerCase(Locale.ROOT);
String value = parts[1].trim(); String value = parts[1].trim();
@@ -57,7 +57,7 @@ public class ParseController {
if (header == null) { if (header == null) {
String[] parts = line.split(","); String[] parts = line.split(",");
if (parts.length < 2) { if (parts.length < 2) {
throw new RuntimeException("Cannot parse header: '" + line + "'"); return ResponseEntity.badRequest().body(Map.of("error", "Cannot parse header: '" + line + "'"));
} }
header = Arrays.asList(parts); header = Arrays.asList(parts);
continue; continue;
@@ -66,9 +66,10 @@ public class ParseController {
// data // data
String[] parts = line.split(","); String[] parts = line.split(",");
if (parts.length != header.size()) { if (parts.length != header.size()) {
throw new RuntimeException("Cannot parse record: '" + line + "'"); return ResponseEntity.badRequest().body(Map.of("error", "Cannot parse record: '" + line + "'"));
} }
try {
records.add(Arrays.stream(parts) records.add(Arrays.stream(parts)
.map(value -> { .map(value -> {
try { try {
@@ -78,10 +79,13 @@ public class ParseController {
} }
}) })
.collect(Collectors.toList())); .collect(Collectors.toList()));
} catch (Exception exception) {
return ResponseEntity.badRequest().body(Map.of("error", exception.getMessage()));
}
} }
if (header == null || records.size() < 2) { if (header == null || records.size() < 2) {
throw new RuntimeException("Invalid report"); return ResponseEntity.badRequest().body(Map.of("error", "Invalid report"));
} }
Map<String, Object> stats = Map.of( Map<String, Object> stats = Map.of(