diff --git a/src/errors.rs b/src/errors.rs index 4f34dc3..8d1981d 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -17,18 +17,20 @@ pub enum Error { impl warp::reject::Reject for Error {} #[rustfmt::skip] -pub fn into_response(e: Rejection) -> Result { +pub fn status_code(e: Rejection) -> StatusCode { if let Some(error) = e.find::() { match error { Error::DecryptionFailed | Error::InvalidOnionRequest | Error::InvalidRpcCall - | Error::NoSuchRoom | Error::ValidationFailed => return Ok(StatusCode::BAD_REQUEST.into_response()), - Error::NoAuthToken => return Ok(StatusCode::UNAUTHORIZED.into_response()), - Error::Unauthorized => return Ok(StatusCode::FORBIDDEN.into_response()), - Error::DatabaseFailedInternally => { - return Ok(StatusCode::INTERNAL_SERVER_ERROR.into_response()) - } + | Error::NoSuchRoom | Error::ValidationFailed => return StatusCode::BAD_REQUEST, + Error::NoAuthToken => return StatusCode::UNAUTHORIZED, + Error::Unauthorized => return StatusCode::FORBIDDEN, + Error::DatabaseFailedInternally => return StatusCode::INTERNAL_SERVER_ERROR }; } else { - return Ok(StatusCode::INTERNAL_SERVER_ERROR.into_response()); + return StatusCode::INTERNAL_SERVER_ERROR; } } + +pub fn into_response(e: Rejection) -> Result { + return Ok(status_code(e).into_response()); +} diff --git a/src/handlers.rs b/src/handlers.rs index 8d6d012..abc67b5 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -804,19 +804,66 @@ pub fn compact_poll( get_messages_query_params .insert("from_server_id".to_string(), from_message_server_id.to_string()); } - let messages = get_messages(get_messages_query_params, &auth_token, &pool)?; + let messages = match get_messages(get_messages_query_params, &auth_token, &pool) { + Ok(messages) => messages, + Err(e) => { + let status_code = super::errors::status_code(e); + let response_body = models::CompactPollResponseBody { + room_id, + status_code: status_code.as_u16(), + messages: vec![], + deletions: vec![], + moderators: vec![], + }; + response_bodies.push(response_body); + continue; + } + }; // Get the new deletions let mut get_deletions_query_params: HashMap = HashMap::new(); if let Some(from_deletion_server_id) = from_deletion_server_id { get_deletions_query_params .insert("from_server_id".to_string(), from_deletion_server_id.to_string()); } - let deletions = get_deleted_messages(get_deletions_query_params, &auth_token, &pool)?; + let deletions = match get_deleted_messages(get_deletions_query_params, &auth_token, &pool) { + Ok(deletions) => deletions, + Err(e) => { + let status_code = super::errors::status_code(e); + let response_body = models::CompactPollResponseBody { + room_id, + status_code: status_code.as_u16(), + messages: vec![], + deletions: vec![], + moderators: vec![], + }; + response_bodies.push(response_body); + continue; + } + }; // Get the moderators - let moderators = get_moderators(&auth_token, &pool)?; + let moderators = match get_moderators(&auth_token, &pool) { + Ok(moderators) => moderators, + Err(e) => { + let status_code = super::errors::status_code(e); + let response_body = models::CompactPollResponseBody { + room_id, + status_code: status_code.as_u16(), + messages: vec![], + deletions: vec![], + moderators: vec![], + }; + response_bodies.push(response_body); + continue; + } + }; // Add to the response - let response_body = - models::CompactPollResponseBody { room_id, deletions, messages, moderators }; + let response_body = models::CompactPollResponseBody { + room_id, + status_code: StatusCode::OK.as_u16(), + deletions, + messages, + moderators, + }; response_bodies.push(response_body); } // Return diff --git a/src/models.rs b/src/models.rs index 9f3cf7c..1545fcc 100644 --- a/src/models.rs +++ b/src/models.rs @@ -38,6 +38,7 @@ pub struct CompactPollRequestBody { #[derive(Debug, Deserialize, Serialize)] pub struct CompactPollResponseBody { pub room_id: String, + pub status_code: u16, pub deletions: Vec, pub messages: Vec, pub moderators: Vec,