Repository: juspay/hyperswitch
File: crates/router/src/services/api.rs (Rust)

Code before:
            });
            let proxy_connector_http_status_code = if state
                .conf
                .proxy_status_mapping
                .proxy_connector_http_status_code
            {
                headers
                    .iter()
                    .find(|(key, _)| key == headers::X_CONNECTOR_HTTP_STATUS_CODE)
                    .and_then(|(_, value)| {
                        match value.clone().into_inner().parse::<u16>() {
                            Ok(code) => match http::StatusCode::from_u16(code) {
                                Ok(status_code) => Some(status_code),
                                Err(err) => {
                                    logger::error!(
                                        "Invalid HTTP status code parsed from connector_http_status_code: {}",
                                        err
                                    );
                                    None
                                }
                            },
                            Err(err) => {
                                logger::error!(
                                    "Failed to parse connector_http_status_code from header: {}",
                                    err
                                );
                                None
                            }
                        }
                    })
            } else {
                None
            };
            match serde_json::to_string(&response) {
                Ok(res) => http_response_json_with_headers(
                    res,
                    headers,
                    request_elapsed_time,
                    proxy_connector_http_status_code,
                ),
                Err(_) => http_response_err(
                    r#"{
                        "error": {
                            "message": "Error serializing response from connector"
                        }
                    }"#,
                ),
            }
        }
        Err(error) => log_and_return_error_response(error),
    };

Review comment (bug):
Can we consider using the `Debug` impl when logging errors?

Code after:
            });
            let proxy_connector_http_status_code = if state
                .conf
                .proxy_status_mapping
                .proxy_connector_http_status_code
            {
                headers
                    .iter()
                    .find(|(key, _)| key == headers::X_CONNECTOR_HTTP_STATUS_CODE)
                    .and_then(|(_, value)| {
                        match value.clone().into_inner().parse::<u16>() {
                            Ok(code) => match http::StatusCode::from_u16(code) {
                                Ok(status_code) => Some(status_code),
                                Err(err) => {
                                    logger::error!(
                                        "Invalid HTTP status code parsed from connector_http_status_code: {:?}",
                                        err
                                    );
                                    None
                                }
                            },
                            Err(err) => {
                                logger::error!(
                                    "Failed to parse connector_http_status_code from header: {:?}",
                                    err
                                );
                                None
                            }
                        }
                    })
            } else {
                None
            };
            match serde_json::to_string(&response) {
                Ok(res) => http_response_json_with_headers(
                    res,
                    headers,
                    request_elapsed_time,
                    proxy_connector_http_status_code,
                ),
                Err(_) => http_response_err(
                    r#"{
                        "error": {
                            "message": "Error serializing response from connector"
                        }
                    }"#,
                ),
            }
        }
        Err(error) => log_and_return_error_response(error),
    };