/// Return a formatted error message from the API if present, or raw value if not
Return a formatted error message from the API if present, or raw value if not
Return a formatted error message from the API if present or raw value if not
pub fn format_api_errors(raw: String) -> String {
    let mut msg = "Something went wrong with the request to Cloudflare...\n".to_string();
    if let Ok(api_errors) = serde_json::from_str::<ApiErrors>(&raw) {
        // handle possible case of opt-in required modules usage
        // TODO: remove this after DO beta restrictions are lifted
        if api_errors
            .errors
            .iter()
            .any(|e| e.message.contains("workers.api.error.not_entitled"))
        {
            msg.push_str("\n\n");
            msg.push_str(TEMP_NOTICE_ES_MODULES_DO_BETA)
        }
        // add all api errors to the accumulator string
        let formatted_errors: Vec<String> = api_errors
            .errors
            .iter()
            .map(|e| format!("{} [API code: {}]", e.message.clone(), e.code))
            .collect();
        msg.push_str(&formatted_errors.join("\n"));
        return msg;
    }
    // if we have no useful detail to extract from the API error, it is likely better to print the
    // raw response value so the end-user can attempt to resolve an issue
    msg.push('\n');
    msg.push_str(&raw);
    msg
}
pub fn format_api_errors ( raw : String ) -> String { let mut msg = " \n " . to_string ( ) ; if let Ok ( api_errors ) = serde_json :: from_str :: < ApiErrors > ( & raw ) { // handle possible case of opt-in required modules usage // TODO: remove this after DO beta restrictions are lifted if api_errors . errors . iter ( ) . any ( | e | e . message . contains ( " " ) ) { msg . push_str ( " \n \n " ) ; msg . push_str ( TEMP_NOTICE_ES_MODULES_DO_BETA ) } // add all api errors to the accumulator string let formatted_errors : Vec < String > = api_errors . errors . iter ( ) . map ( | e | format ! ( " " e message clone ( ) e code ) ) . collect ( ) ; msg . push_str ( & formatted_errors . join ( " \n " ) ) ; return msg ; } // if we have no useful detail to extract from the API error, it is likely better to print the // raw response value so the end-user can attempt to resolve an issue msg . push ( '\n' ) ; msg . push_str ( & raw ) ; msg }
Return a formatted error message from the API if present, or raw value if not
Return a formatted error message from the API if present or raw value if not
raw
String