fix: log deserialization failures on log ingestion endpoint
Spring's default handler silently returns 400 for malformed payloads with no server-side log. Added @ExceptionHandler to catch and WARN with the agent instance ID and root cause message. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -15,6 +15,8 @@ import org.slf4j.LoggerFactory;
|
|||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.security.core.Authentication;
|
import org.springframework.security.core.Authentication;
|
||||||
import org.springframework.security.core.context.SecurityContextHolder;
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
|
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||||
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
@@ -94,6 +96,14 @@ public class LogIngestionController {
|
|||||||
return ResponseEntity.accepted().build();
|
return ResponseEntity.accepted().build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(HttpMessageNotReadableException.class)
|
||||||
|
public ResponseEntity<Void> handleDeserializationError(HttpMessageNotReadableException ex) {
|
||||||
|
String instanceId = extractAgentId();
|
||||||
|
log.warn("Log ingestion from instance={}: failed to deserialize request body: {}",
|
||||||
|
instanceId != null ? instanceId : "unknown", ex.getMostSpecificCause().getMessage());
|
||||||
|
return ResponseEntity.badRequest().build();
|
||||||
|
}
|
||||||
|
|
||||||
private String extractAgentId() {
|
private String extractAgentId() {
|
||||||
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
||||||
return auth != null ? auth.getName() : null;
|
return auth != null ? auth.getName() : null;
|
||||||
|
|||||||
Reference in New Issue
Block a user