mirror of
				https://github.com/juspay/hyperswitch.git
				synced 2025-10-31 18:17:13 +08:00 
			
		
		
		
	refactor(opensearch): Add Error Handling for Empty Query and Filters in Request (#5432)
Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
This commit is contained in:
		| @ -71,6 +71,8 @@ pub enum OpenSearchError { | |||||||
|     ConnectionError, |     ConnectionError, | ||||||
|     #[error("Opensearch NON-200 response content: '{0}'")] |     #[error("Opensearch NON-200 response content: '{0}'")] | ||||||
|     ResponseNotOK(String), |     ResponseNotOK(String), | ||||||
|  |     #[error("Opensearch bad request error")] | ||||||
|  |     BadRequestError(String), | ||||||
|     #[error("Opensearch response error")] |     #[error("Opensearch response error")] | ||||||
|     ResponseError, |     ResponseError, | ||||||
|     #[error("Opensearch query building error")] |     #[error("Opensearch query building error")] | ||||||
| @ -98,6 +100,9 @@ impl ErrorSwitch<ApiErrorResponse> for OpenSearchError { | |||||||
|                 "Connection error", |                 "Connection error", | ||||||
|                 None, |                 None, | ||||||
|             )), |             )), | ||||||
|  |             Self::BadRequestError(response) => { | ||||||
|  |                 ApiErrorResponse::BadRequest(ApiError::new("IR", 1, response.to_string(), None)) | ||||||
|  |             } | ||||||
|             Self::ResponseNotOK(response) => ApiErrorResponse::InternalServerError(ApiError::new( |             Self::ResponseNotOK(response) => ApiErrorResponse::InternalServerError(ApiError::new( | ||||||
|                 "IR", |                 "IR", | ||||||
|                 1, |                 1, | ||||||
|  | |||||||
| @ -16,6 +16,17 @@ pub async fn msearch_results( | |||||||
|     merchant_id: &common_utils::id_type::MerchantId, |     merchant_id: &common_utils::id_type::MerchantId, | ||||||
|     indexes: Vec<SearchIndex>, |     indexes: Vec<SearchIndex>, | ||||||
| ) -> CustomResult<Vec<GetSearchResponse>, OpenSearchError> { | ) -> CustomResult<Vec<GetSearchResponse>, OpenSearchError> { | ||||||
|  |     if req.query.trim().is_empty() | ||||||
|  |         && req | ||||||
|  |             .filters | ||||||
|  |             .as_ref() | ||||||
|  |             .map_or(true, |filters| filters.is_all_none()) | ||||||
|  |     { | ||||||
|  |         return Err(OpenSearchError::BadRequestError( | ||||||
|  |             "Both query and filters are empty".to_string(), | ||||||
|  |         ) | ||||||
|  |         .into()); | ||||||
|  |     } | ||||||
|     let mut query_builder = |     let mut query_builder = | ||||||
|         OpenSearchQueryBuilder::new(OpenSearchQuery::Msearch(indexes.clone()), req.query); |         OpenSearchQueryBuilder::new(OpenSearchQuery::Msearch(indexes.clone()), req.query); | ||||||
|  |  | ||||||
|  | |||||||
| @ -10,6 +10,15 @@ pub struct SearchFilters { | |||||||
|     pub customer_email: Option<Vec<HashedString<common_utils::pii::EmailStrategy>>>, |     pub customer_email: Option<Vec<HashedString<common_utils::pii::EmailStrategy>>>, | ||||||
|     pub search_tags: Option<Vec<HashedString<WithType>>>, |     pub search_tags: Option<Vec<HashedString<WithType>>>, | ||||||
| } | } | ||||||
|  | impl SearchFilters { | ||||||
|  |     pub fn is_all_none(&self) -> bool { | ||||||
|  |         self.payment_method.is_none() | ||||||
|  |             && self.currency.is_none() | ||||||
|  |             && self.status.is_none() | ||||||
|  |             && self.customer_email.is_none() | ||||||
|  |             && self.search_tags.is_none() | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
| #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] | #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] | ||||||
| #[serde(rename_all = "camelCase")] | #[serde(rename_all = "camelCase")] | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 Abhishek Kanojia
					Abhishek Kanojia