dani-garcia/vaultwarden · error
Can't convert to number
Error message
Can't convert to number
What it means
NumberOrString::into_i32 parses a String variant into i32. When the string is not a valid integer (ParseIntError), it is mapped to a crate Error with message 'Can't convert to number' and the parse error as detail.
Solutions
- Ensure the string is a plain base-10 integer before calling into_i32
- Validate/sanitize client input earlier in the API handler and return a 400 instead
- Use into_i64() if values may exceed the i32 range
- Log the error detail (e.to_string()) which contains the exact parse failure
Example fix
// before let n = value.into_i32()?; // "12abc" -> Can't convert to number // after let n = value.into_i64()?; // or validate: s.trim().parse::<i64>().is_ok()
Defensive patterns
Strategy: try-catch
Validate before calling
function isI32String(s) { const n = Number(s); return Number.isInteger(n) && n >= -2147483648 && n <= 2147483647; } Type guard
function asI32(v) { return typeof v === 'string' && /^-?\d+$/.test(v.trim()) && Number(v) <= 2147483647 ? Number(v) : null; } Try / catch
match value.into_i32() {
Ok(n) => n,
Err(e) => { warn!("Non-numeric field: {}", e); return Err(e); }
} Prevention
- Validate numeric string fields at the API boundary before domain logic
- Reject decimals, whitespace, and empty strings explicitly
- Use into_i64 for fields that may exceed i32 range
- Include the parse-error detail in client-facing 400 responses
When it happens
Trigger: Calling into_i32() on a NumberOrString::String holding non-numeric or out-of-i32-range text, e.g. client-supplied JSON fields like folder ids' iteration counts or API request numbers sent as strings.
Common situations: Bitwarden clients sending numeric fields as strings with formatting (spaces, signs, underscores); empty string ''; values exceeding i32 range (use into_i64 for those, though that yields a distinct 'does not fit' error path).
Understand the failure class
Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.
Related errors
AI-assisted analysis of dani-garcia/vaultwarden@cc67d644f6 (2026-09-19).
Data as JSON: /api/errors/27b73cf79f25c84d.
Report an issue: GitHub.
Appendix: source
Thrown at src/util.rs:707
match self {
NumberOrString::Number(n) => n.to_string(),
NumberOrString::String(s) => s,
}
}
#[expect(clippy::wrong_self_convention)]
pub fn into_i32(&self) -> Result<i32, crate::Error> {
use std::num::ParseIntError as PIE;
match self {
NumberOrString::Number(n) => {
if let Some(n) = n.to_i32() {
Ok(n)
} else {
err!("Number does not fit in i32")
}
}
NumberOrString::String(s) => {
s.parse().map_err(|e: PIE| crate::Error::new("Can't convert to number", e.to_string()))
}
}
}
#[expect(clippy::wrong_self_convention)]
pub fn into_i64(&self) -> Result<i64, crate::Error> {
use std::num::ParseIntError as PIE;
match self {
NumberOrString::Number(n) => Ok(*n),
NumberOrString::String(s) => {
s.parse().map_err(|e: PIE| crate::Error::new("Can't convert to number", e.to_string()))
}
}
}
}
//
// Retry methodsView on GitHub (pinned to cc67d644f6)