spotify_cli/spotify/
error.rs

1use reqwest::StatusCode;
2
3pub fn format_api_error(operation: &str, status: StatusCode, body: &str) -> String {
4    let mut message = format!("{operation}: {} {}", status, body);
5
6    if body.contains("Insufficient client scope") {
7        message.push_str("; hint: missing scope, re-run `spotify auth login` and approve scopes");
8    } else if status == StatusCode::UNAUTHORIZED {
9        message.push_str("; hint: token expired or invalid, run `spotify auth login`");
10    } else if status == StatusCode::FORBIDDEN {
11        message.push_str("; hint: playlist may be read-only or missing modify scope, re-run `spotify auth login`");
12    }
13
14    message
15}
16
17#[cfg(test)]
18mod tests {
19    use super::format_api_error;
20    use reqwest::StatusCode;
21
22    #[test]
23    fn adds_scope_hint() {
24        let message = format_api_error(
25            "spotify search failed",
26            StatusCode::FORBIDDEN,
27            r#"{"error":{"message":"Insufficient client scope"}}"#,
28        );
29        assert!(message.contains("missing scope"));
30    }
31
32    #[test]
33    fn adds_unauthorized_hint() {
34        let message = format_api_error("spotify request failed", StatusCode::UNAUTHORIZED, "{}");
35        assert!(message.contains("token expired"));
36    }
37}