fix: Return a 422 for limits on a partial write (#25729)

Prior to this change we would error correctly with a 422 if a limit was
hit. However, we would not send back the correct error in the case of a
limit being hit that caused a partial write. This change fixes that by
checking the error messages for failed lines and if one is found that
caused a limit to be hit, then a 422 is returned rather than a 400 as
we would have been able to process the line otherwise, but the limit was
hit instead

Closes #25208
pull/25733/head
Michael Gattozzi 2025-01-02 14:01:23 -05:00 committed by GitHub
parent 237ab358f6
commit 636503ff46
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 2 deletions

View File

@ -76,7 +76,7 @@ async fn limits() -> Result<(), Error> {
else {
panic!("Did not error when adding 501st column");
};
assert_eq!(code, StatusCode::BAD_REQUEST);
assert_eq!(code, StatusCode::UNPROCESSABLE_ENTITY);
Ok(())
}

View File

@ -347,6 +347,13 @@ impl Error {
.unwrap()
}
Self::PartialLpWrite(data) => {
let limit_hit = data.invalid_lines.iter().any(|err| {
err.error_message
.starts_with("Update to schema would exceed number of")
|| err
.error_message
.starts_with("Adding a new database would exceed limit of")
});
let err = ErrorMessage {
error: "partial write of line protocol occurred".into(),
data: Some(data.invalid_lines),
@ -354,7 +361,11 @@ impl Error {
let serialized = serde_json::to_string(&err).unwrap();
let body = Body::from(serialized);
Response::builder()
.status(StatusCode::BAD_REQUEST)
.status(if limit_hit {
StatusCode::UNPROCESSABLE_ENTITY
} else {
StatusCode::BAD_REQUEST
})
.body(body)
.unwrap()
}