Unverified Commit a4220540 authored by Sam Ko's avatar Sam Ko Committed by GitHub

fix(next/font): update axes error logic (#72442)

## Why?

The current error message when you have have a set `weight: ['400', '700']` and set `axes: ['wdth']` for a variable font is confusing—it should mention to correct your weight property (e.g., `weight: 'variable'` or nothing at all for `weight`).

- Fixes https://github.com/vercel/next.js/issues/72413
parent 49ac1011
...@@ -99,8 +99,9 @@ pub(super) fn options_from_request( ...@@ -99,8 +99,9 @@ pub(super) fn options_from_request(
}) })
.unwrap_or_default(); .unwrap_or_default();
let supports_variable_weight = font_data.weights.iter().any(|el| el == "variable");
let weights = if requested_weights.is_empty() { let weights = if requested_weights.is_empty() {
if !font_data.weights.contains(&"variable".into()) { if !supports_variable_weight {
return Err(anyhow!( return Err(anyhow!(
"Missing weight for {}. Available weights: {}", "Missing weight for {}. Available weights: {}",
font_family, font_family,
...@@ -170,8 +171,17 @@ pub(super) fn options_from_request( ...@@ -170,8 +171,17 @@ pub(super) fn options_from_request(
} }
if let Some(axes) = argument.axes.as_ref() { if let Some(axes) = argument.axes.as_ref() {
if !axes.is_empty() && !matches!(weights, FontWeights::Variable) { if !axes.is_empty() {
return Err(anyhow!("Axes can only be defined for variable fonts")); if !supports_variable_weight {
return Err(anyhow!("Axes can only be defined for variable fonts."));
}
if weights != FontWeights::Variable {
return Err(anyhow!(
"Axes can only be defined for variable fonts when the weight property is \
nonexistent or set to `variable`."
));
}
} }
} }
...@@ -533,7 +543,7 @@ mod tests { ...@@ -533,7 +543,7 @@ mod tests {
} }
#[test] #[test]
fn test_errors_on_axes_without_variable() -> Result<()> { fn test_errors_on_axes_without_variable_weight() -> Result<()> {
let data: FxIndexMap<RcStr, FontDataEntry> = parse_json_with_source_context( let data: FxIndexMap<RcStr, FontDataEntry> = parse_json_with_source_context(
r#" r#"
{ {
...@@ -564,7 +574,48 @@ mod tests { ...@@ -564,7 +574,48 @@ mod tests {
Err(err) => { Err(err) => {
assert_eq!( assert_eq!(
err.to_string(), err.to_string(),
"Axes can only be defined for variable fonts" "Axes can only be defined for variable fonts when the weight property is \
nonexistent or set to `variable`."
)
}
}
Ok(())
}
#[test]
fn test_errors_on_axes_without_variable_font() -> Result<()> {
let data: FxIndexMap<RcStr, FontDataEntry> = parse_json_with_source_context(
r#"
{
"ABeeZee": {
"weights": ["400", "700"],
"styles": ["normal", "italic"]
}
}
"#,
)?;
let request: NextFontRequest = parse_json_with_source_context(
r#"
{
"import": "ABeeZee",
"path": "index.js",
"variableName": "abeezee",
"arguments": [{
"weight": ["400", "700"],
"axes": ["wght"]
}]
}
"#,
)?;
match options_from_request(&request, &data) {
Ok(_) => panic!(),
Err(err) => {
assert_eq!(
err.to_string(),
"Axes can only be defined for variable fonts."
) )
} }
} }
......
...@@ -101,7 +101,19 @@ describe('validateFontFunctionCall errors', () => { ...@@ -101,7 +101,19 @@ describe('validateFontFunctionCall errors', () => {
subsets: ['latin'], subsets: ['latin'],
}) })
).toThrowErrorMatchingInlineSnapshot( ).toThrowErrorMatchingInlineSnapshot(
`"Axes can only be defined for variable fonts"` `"Axes can only be defined for variable fonts."`
)
})
test('Setting axes on variable font with incorrect weight', async () => {
expect(() =>
validateGoogleFontFunctionCall('Roboto_Flex', {
weight: ['400', '700'],
axes: ['wght'],
subsets: ['latin'],
})
).toThrowErrorMatchingInlineSnapshot(
'"Axes can only be defined for variable fonts when the weight property is nonexistent or set to `variable`."'
) )
}) })
}) })
...@@ -135,8 +135,16 @@ export function validateGoogleFontFunctionCall( ...@@ -135,8 +135,16 @@ export function validateGoogleFontFunctionCall(
) )
} }
if (weights[0] !== 'variable' && axes) { if (axes) {
nextFontError('Axes can only be defined for variable fonts') if (!fontWeights.includes('variable')) {
nextFontError('Axes can only be defined for variable fonts.')
}
if (weights[0] !== 'variable') {
nextFontError(
'Axes can only be defined for variable fonts when the weight property is nonexistent or set to `variable`.'
)
}
} }
return { return {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment