-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask5.java
More file actions
59 lines (51 loc) · 2.49 KB
/
Copy pathTask5.java
File metadata and controls
59 lines (51 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
class DocumentValidator {
private static final Logger log = LoggerFactory.getLogger(DocumentValidator.class);
public ValidationResult validate(Document doc) {
try {
if (doc == null) {
// FIX: Treat null document as an expected validation failure instead of throwing RuntimeException.
log.warn("Validation failed: document is null");
return ValidationResult.invalid("Document is null");
}
String content = doc.extractContent();
if (content == null || content.isEmpty()) {
// FIX: Return a validation failure result for empty content instead of flooding logs with stack traces.
log.warn("Validation failed: empty content");
return ValidationResult.invalid("Empty content");
}
return runValidationRules(content);
} catch (IllegalArgumentException e) {
// FIX: Expected rule-level validation failure is logged without stack trace and returned as invalid result.
log.warn("Validation failed: {}", e.getMessage());
return ValidationResult.invalid(e.getMessage());
} catch (Exception e) {
// FIX: Replace printStackTrace() and null return with structured SLF4J logging plus exception propagation.
log.error("Unexpected error while validating document", e);
throw new IllegalStateException("Unexpected error while validating document", e);
}
}
public void validateBatch(List<Document> docs) {
for (Document doc : docs) {
try {
ValidationResult r = validate(doc);
// FIX: Guard against null ValidationResult before calling isValid() to avoid NullPointerException.
if (r != null && r.isValid()) {
saveResult(r);
}
} catch (Exception e) {
// FIX: Do not silently swallow batch exceptions; log them with context for production support analysis.
log.error("Document validation failed during batch processing", e);
}
}
}
private ValidationResult runValidationRules(String content) {
// Existing validation rule logic remains unchanged.
return ValidationResult.valid();
}
private void saveResult(ValidationResult result) {
// Existing persistence logic remains unchanged.
}
}