Recipe Book0.6.0

5.3Errors

invalid_argument

A function, part, or hook received an argument it can't use, such as an amount of 0, a range whose minimum is above its maximum, an unknown system, or unparsed recipe input.

§ 5.310 min readView as Markdown

A function from @recipe-book/core, or a part or hook from @recipe-book/react, received an argument it can’t use. The message names the function, part, or hook, what it expects, and usually the value it received:

Text
RecipeBookError: convert: amount must be a finite number greater than 0, received NaN.
See https://recipe-book.siraj.page/errors/invalid_argument/

Recipe Book checks arguments rather than returning NaN or a wrong amount that would surface later in a recipe. The value usually comes from data, such as a form field or a URL, that your code passed in without checking.

The name is one of the core functions convert, toSystem, convertTemperature, segmentStepText, segmentStepQuantities, scaleQuantity, resolveIngredient, formatQuantity, formatQuantityToParts, formatIngredient, formatServings, formatYield, formatTemperature, formatDuration, labelPack, parseRecipe, parseQuantity, parseIngredientLine, createRecipeSchema, findRecipeJsonLd, fromJsonLd, and toJsonLd, or the React parts <Recipe.Root>, <Recipe.Provider>, <Recipe.Equipment>, and <Recipe.Nutrition>, or hooks useRecipeState() and useChecklistState(). When toJsonLd rejects an ingredient’s quantity or one of its equivalents as the recipe gives it, the value’s path comes after the name, as in toJsonLd: ingredients[0].items[0].quantity: amount must be a finite number greater than 0, received 0. for an amount of 0. A finite amount, or an end of a range, above 1e290 or between 0 and 1e-300 isn’t checked, and where scaling or conversion makes one Infinity or 0, the message gives that Infinity or 0 and no path, as in toJsonLd: amount must be a finite number greater than 0, received Infinity.

Message, after the name Fix
amount must be a finite number greater than 0, received NaN. Check amounts from data.
amount must be a number or a { min, max } range, received "2". Convert the string with Number, then check it.
range minimum (3) must not be greater than the maximum (2). Put the low end in min, or check both ends when they come from data.
range ends must be finite numbers greater than 0, received { min: 0, max: 2 }. Check both ends, as you would an amount.
expected a quantity such as { amount: 2, unit: "cup" }, received null. Pass a Quantity object.
a quantity has no max; use amount: { min, max }, or formatServings or formatYield. Write a quantity’s range as amount: { min, max }, and use formatServings or formatYield for servings and yields.
factor must be a finite number greater than 0, received 0. Check the factor, as you would an amount.
scale must be a finite number greater than 0, received NaN. Check the scale, as you would an amount.
the result must be a finite number greater than 0, but it is Infinity. Check the amount, and scaleQuantity’s factor, as you would an amount: the result is past the largest number, about 1.8e308, or below about 2.5e-324, where it rounds to 0 rather than up to the smallest number, 5e-324. toSystem also throws it when the measure it lands on is past the largest number.
system must be "metric" or "us", received "US". Check the measurement system.
temperature unit must be "C" or "F", received "K". Use 'C' or 'F' for the Temperature unit.
target unit must be "C" or "F", received "celsius". Pass 'C' or 'F' as the unit to convert to.
value must be a finite number, received NaN. Check the temperature’s value, as you would an amount.
value out of range. Check the temperature’s value, as you would an amount: when a value beyond about ±2e307 °C is converted to °F or one beyond about ±3.6e307 °F is converted to °C, the arithmetic overflows to Infinity or -Infinity even when the result itself would be finite.
minutes must be a finite number, 0 or more, received -1. Check the duration, as you would an amount.
unitDisplay must be "short" or "long", received "narrow". Pass a value the message lists, or leave the option out for its default.
fractions must be true or false, received "yes". Pass a value the message lists, or leave the option out for its default.
rounding must be "kitchen" or "none", received "oven". Pass a value the message lists, or leave the option out for its default.
rounding must be "kitchen", "oven", or "none", received "exact". Pass a value the message lists, or leave the option out for its default.
locale must be a language tag that Intl accepts, such as "en-US", received "en_US". Use a tag Intl accepts, written with hyphens, as in 'en-US'.
labels must be a label pack such as de from "@recipe-book/core/labels", received "de". Import the pack from @recipe-book/core/labels, or in a React app from @recipe-book/react/core/labels, and pass the object, not its name. A pack from @recipe-book/react/labels formats in client code. In a Server Component it’s a client reference: on Next 15 and later, a formatting function given one throws this too, and on Next 14, Next throws “Cannot read Symbol exports. Only named exports are supported on a client module imported on the server.” first. Either way, pass a pack from @recipe-book/core/labels in server code. For your own words, or a changed copy of a shipped pack, make the pack with labelPack. A spread copy of a pack isn’t one, and TypeScript rejects it.
labels must be an object, received "de". labelPack throws it when given a value that isn’t an object, such as a pack’s name. Pass an object with your words, as labelPack’s example does. To use a shipped pack, import it instead.
labels.language must be a language tag such as "de", received "German". labelPack throws it when it makes the pack, and so does labelPack from @recipe-book/react/labels. Give language as a tag, such as 'de'.
labels.units must be an object, received null. labelPack throws it when it makes the pack. Give units, durations, temperature, and each entry in units and durations as an object, or leave it out.
labels.durations.hour.long.other must be a string, received undefined. labelPack throws it when it makes the pack. Give the entry all four labels: short.one, short.other, long.one, and long.other. temperature takes degree.one, degree.other, C, and F.
expected servings such as { amount: 4, max: 6 }, received undefined. Pass a Servings object, and check that the recipe has servings first.
expected a yield such as { amount: 24, unit: "cookie" }, received undefined. Pass a Yield object, and check that the recipe has a yield first.
max must be a finite number greater than amount (4), received 4. Leave max out for a single amount. Leave max out when it equals amount. Otherwise check it, as you would an amount.
unit must be a string such as "cookie", received undefined. Give the yield a unit, the word for what it makes, such as 'cookie'.
plural must be a string, received 42. Write plural as a string, such as 'cookies', or leave it out.
expected { ingredient, quantity? }, such as a resolved ingredient, received null. Pass { ingredient } with an Ingredient. In React, pass the item from <Recipe.IngredientList> or useRecipe().ingredients.
ingredient.name must be a string, received 42. Parse the recipe with parseRecipe first.
ingredient.plural must be a string, received 42. Parse the recipe with parseRecipe first.
ingredient.note must be a string, received 42. Parse the recipe with parseRecipe first.
parseIngredientLine must be a function such as parseIngredientLine, received null. Pass parseIngredientLine, or leave the option out to read ingredient objects only.
options must be an object such as { decimal: "," }, received ",". Pass the options in an object, as in { decimal: ',' }. A locale string doesn’t set them.
decimal must be "." or ",", received ";". Pass a value the message lists, or leave the option out for its default.
units must be an object such as { cup: "cup-metric" }, received "metric". Pass an object with the units to read, or leave the option out for US cups and tablespoons.
units.cup must be "cup" or "cup-metric", received "cups". Pass a value the message lists, or leave the key out for the US cup.
units.tbsp must be "tbsp" or "tbsp-au", received "tbsp-uk". Pass a value the message lists, or leave the key out for the US tablespoon.
each key of units must be "cup" or "tbsp", received "pint". Remove the key. Only cups and tablespoons have regional sizes, as ParseQuantityOptions lists.
recipe must be a parsed recipe, received a parse result instead of its data. … Pass result.data, after checking result.success, as Parse recipe input first shows. Only in development. In production, <Recipe.Root> gives the next row’s.
recipe must be a parsed recipe, received an object that parseRecipe hasn't normalized. … Parse recipe input first.
expected options such as { recipe }, received undefined. … To read the state of the nearest <Recipe.Root>, call useRecipe() instead, or useChecklist() for the ticks of a <Recipe.Checklist>. Otherwise pass { recipe }. Only in development. In production, a hook called without options throws a TypeError instead.
expected options such as { recipe }, received an object without a recipe property. Pass the parsed recipe as { recipe }. Pass { recipe }, not the recipe itself, to useRecipeState or useChecklistState, or call useRecipe() in a Root. Only in development. In production, a hook given the recipe itself throws a TypeError instead.
labels must be a pack from @recipe-book/react/labels, such as de or labelPack({ servings: "Portions" }), received "de". … Import a pack from @recipe-book/react/labels, or make one with its labelPack, and pass the pack itself. A plain object, or a copy of a pack, throws it too, because the parts write with the words a pack holds under a symbol key. TypeScript rejects a spread copy, such as labels={{ ...de }}, but not one made with Object.assign or structuredClone. A Server Component passes a pack from @recipe-book/react/labels, or one it imports by name from a 'use client' module, as a client reference, which keeps it, but its props drop that key from a pack it makes or imports from any other module, such as core’s de from @recipe-book/react/core/labels, so make your own pack in a 'use client' module, as Change one word shows.
labels must be a label pack. The production message for the row above. Fix it the same way.
text must be a string, received 42. Pass a string, such as a Step’s text.
assumeTemperatureUnit must be "C" or "F", received "K". Pass 'C' or 'F', or leave the option out to keep temperatures without a unit as text.
html must be a string, such as await response.text(), received 42. Pass the string from await response.text(), not the Response or its Promise.

Check amounts from data

Number returns NaN for text it can’t read, such as '½', so convert throws:

TypeScript
import { convert } from '@recipe-book/core'

function cupsToMilliliters(text: string) {
  return convert({ amount: Number(text), unit: 'cup' }, 'ml')
}

cupsToMilliliters('½')

A finite amount greater than 0 can still throw, because convert throws when the result overflows to Infinity or underflows to 0, as it does for 1e308 cups. Check that the amount is from 1e-300 to 1e290, the range recipe input allows, before you convert it:

TypeScript
import { convert } from '@recipe-book/core'

function cupsToMilliliters(text: string) {
  const amount = Number(text)
  if (Number.isNaN(amount) || amount < 1e-300 || amount > 1e290) return undefined
  return convert({ amount, unit: 'cup' }, 'ml')
}

cupsToMilliliters('0.5') // { amount: 118.29411825, unit: 'ml' }
cupsToMilliliters('½') // undefined
cupsToMilliliters('1e308') // undefined

Use formatServings or formatYield, not quantity functions

A quantity has no max: its range is amount: { min, max }. A recipe’s servings and yield write a range with max instead, as in { amount: 4, max: 6 }. TypeScript rejects one where a quantity belongs, but with data typed any, the quantity functions, such as convert, throw for servings or a yield with a max:

Text
RecipeBookError: convert: a quantity has no max; use amount: { min, max }, or formatServings or formatYield.
See https://recipe-book.siraj.page/errors/invalid_argument/

Use formatServings to print servings and formatYield to print a yield. resolveRecipe scales both with the ingredients.

Check the measurement system

toSystem accepts only 'metric' and 'us', in lowercase, so it throws for 'US':

TypeScript
import { toSystem, type System } from '@recipe-book/core'

const system = new URLSearchParams('?system=US').get('system') as System

toSystem({ amount: 1.5, unit: 'cup' }, system)

Check the value instead of casting it, and fall back to a system you choose:

TypeScript
import { toSystem, type System } from '@recipe-book/core'

function isSystem(value: unknown): value is System {
  return value === 'metric' || value === 'us'
}

const param = new URLSearchParams('?system=US').get('system')
const system = isSystem(param) ? param : 'metric'

toSystem({ amount: 1.5, unit: 'cup' }, system) // { amount: 354.88235475, unit: 'ml' }

Parse recipe input first

<Recipe.Root>, useRecipeState(), and useChecklistState() take a parsed recipe. Input typed any, such as the result of response.json(), gets past TypeScript even when you haven’t parsed it:

TSX
import * as Recipe from '@recipe-book/react'

export default async function Page() {
  const response = await fetch('https://example.com/recipes/pancakes.json')
  const input = await response.json()

  return (
    <Recipe.Root recipe={input}>
      <Recipe.Image />
    </Recipe.Root>
  )
}
Text
RecipeBookError: <Recipe.Root>: recipe must be a parsed recipe, received an object that parseRecipe hasn't normalized. Call parseRecipe() from @recipe-book/react/core and pass result.data.
See https://recipe-book.siraj.page/errors/invalid_argument/

Other input gets the same message with its kind after received, such as received undefined., received null., received a string., or received an array.

Parse it with parseRecipe and pass result.data:

TSX
import { parseRecipe } from '@recipe-book/react/core'
import * as Recipe from '@recipe-book/react'

export default async function Page() {
  const response = await fetch('https://example.com/recipes/pancakes.json')
  const result = parseRecipe(await response.json())
  if (!result.success) throw result.error

  return (
    <Recipe.Root recipe={result.data}>
      <Recipe.Image />
    </Recipe.Root>
  )
}

To parse in the browser instead, call parseRecipe in a Client Component, as Parse in the browser shows. useRecipeState() and useChecklistState() check their recipe only in development.

Contents