Skip to content

Commit f59800b

Browse files
Add error_for_status_code methods to Response (#46)
Closes #28
1 parent 755cdae commit f59800b

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

elasticsearch/src/http/response.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,22 @@ impl Response {
2121
self.0.status()
2222
}
2323

24+
/// Turn the response into an `Error` if Elasticsearch returned an error.
25+
pub fn error_for_status_code(self) -> Result<Self, Error> {
26+
match self.0.error_for_status_ref() {
27+
Ok(_) => Ok(self),
28+
Err(err) => Err(err.into()),
29+
}
30+
}
31+
32+
/// Turn the response into an `Error` if Elasticsearch returned an error.
33+
pub fn error_for_status_code_ref(&self) -> Result<&Self, Error> {
34+
match self.0.error_for_status_ref() {
35+
Ok(_) => Ok(self),
36+
Err(err) => Err(err.into()),
37+
}
38+
}
39+
2440
/// The response headers
2541
pub fn headers(&self) -> &HeaderMap {
2642
self.0.headers()

0 commit comments

Comments
 (0)