Recipe Book0.6.0

5.1Errors

Error codes

Every code a RecipeBookError can carry and what it means, every issue code and its fix, and the warnings React parts log in development.

§ 5.114 min readView as Markdown

Every RecipeBookError has a code, and its message ends with a link to the page for that code.

Code Meaning
invalid_recipe parseRecipe or fromJsonLd found errors in a recipe, which the error’s issues list.
invalid_quantity parseQuantity found errors in quantity text, such as '2 cupz', which the error’s issues list.
invalid_ingredient parseIngredientLine received blank text or a value that isn’t text. Other lines succeed, and one it can’t read is kept as the name, with a warning when it starts with an amount, such as '0 eggs', but not when it starts with a sign, NaN, or Infinity, such as '-2 eggs'.
invalid_argument A function, part, or hook received an argument it can’t use, such as an amount of 0 or recipe input you haven’t parsed.
incompatible_units convert received units it can’t convert between, such as cups and grams, or a count with no fixed size, such as a clove or 2 eggs.
unknown_unit A function received a unit that isn’t a unit ID, such as 'cups'.
missing_parent A part from @recipe-book/react, or a hook such as useRecipe(), ran outside the component it belongs in, such as <Recipe.Root>, or the app loaded two copies of @recipe-book/react. The message names that component.

Issue codes

Problems in recipe input are issues rather than errors, and every issue has one of these codes. Read the issues shows what each one holds. In an ingredient line read with parseIngredientLine, missing_field, invalid_quantity, invalid_amount, and invalid_range are warnings instead, because parseIngredientLine keeps the line as the name, and so is unknown_unit for a likely misspelled unit, which it keeps in the name. Only a blank line is an error from parseIngredientLine. In recipe input, an amount, or either end of a range, that it reads, or works out from a size and its count, outside 1e-300 to 1e290, the range recipe input allows, is an invalid_amount error at the quantity’s or the equivalent’s amount, or at that amount’s min or max. fromJsonLd leaves out a JSON-LD value the recipe can’t hold, with a warning whose message ends “It is ignored.” toJsonLd reports only warnings, which the rows for missing_field, invalid_type, invalid_amount, and invalid_duration describe.

Code Severity Meaning Fix
invalid_type 'error' A value has the wrong type, such as a number where text belongs. A system other than 'metric' or 'us', and a section inside a section, are also this code. A number for yield is also this code, and for a finite number greater than 0 its message points to servings. A text ingredient, such as '2 cups flour', is also this code unless you pass { parseIngredientLine }. toJsonLd reports this code as a warning, at options.servings or options.system, for an option of the wrong type, which it ignores. Use the type the message names. For system, use 'metric' or 'us'. Move a nested section to the top level. Write a number of servings as servings: 4. Pass { parseIngredientLine } to parseRecipe or createRecipeSchema, or write the ingredient as an object.
missing_field 'error' A required field is missing or empty: the recipe’s name, an ingredient’s name, a step’s text, a note’s text, a piece of equipment’s name, a custom time’s label or minutes, the amount of a quantity, servings, or yield object, a yield’s unit, or the quantity of an ingredient with equivalents. toJsonLd reports this code as a warning, at image or recipeYield, when the JSON-LD it writes lacks an image, or has nutrition without the number of servings. Add the field the message names, with a value. For an ingredient with equivalents, add its quantity.
unknown_unit 'error' A quantity’s unit isn’t in the unit registry, such as 'cupz'. Use the suggested unit, or move the word into the ingredient’s name. See unknown_unit.
invalid_quantity 'error' Quantity, servings, or yield text can’t be read, such as 'two cups', '1,500 g', 'tbsp', '4 servings', or '3 or 4 loaves', or nutrient text can’t be read as one quantity in the right kind of unit, such as fat: '250 kcal' or protein: '10-12 g'. Write the amount in digits or fractions before the unit, such as '1 ½ cups'. Leave out thousands separators, and for a number such as '1,500', write the value you mean or pass the decimal option your text uses. Write a range with a dash or “to”, such as '3–4 loaves', and servings as only a number or range. When yield text joins two numbers with “and” or “&”, as in '2 and 1/2 dozen' or '4 & 5 loaves', the message ends with Did you mean "2 1/2 dozen"? or Did you mean "4-5 loaves"?, and suggestion holds the same text. Give each nutrient one quantity in its unit.
invalid_amount 'error' An amount, including a servings or yield amount, isn’t a finite number greater than 0, such as 0, an equipment quantity isn’t a whole number greater than 0, such as 1.5, or a nutrient isn’t a finite number of 0 or more. A quantity, servings, or yield amount must also be from 1e-300 to 1e290, so that scaled and converted amounts stay finite and greater than 0. toJsonLd reports this code as a warning, at options.servings, for a servings option outside a millionth to a million times the recipe’s scaling base, which it ignores. Use a number from 1e-300 to 1e290 for a quantity, servings, or yield, a whole number greater than 0 for equipment, and 0 or more for a nutrient. For an ingredient without a quantity, such as salt to taste, leave out quantity. For the toJsonLd warning, pass servings in the range the message gives.
invalid_range 'error' A range’s minimum is greater than its maximum, such as '3-2 cups', or a servings or yield max below its amount. Put the smaller number first. For a mixed number written as '1-1/2', use a space: '1 1/2'.
invalid_duration 'error' A time in times or times.custom, such as '30 minutes', isn’t a number of minutes or an ISO 8601 duration. fromJsonLd uses it as a warning for a prepTime, cookTime, or totalTime it leaves out, such as a number of minutes: Expected an ISO 8601 duration such as "PT30M", received 30. It is ignored. toJsonLd reports this code as a warning, at times.prep, times.cook, or times.total, for a time longer than Number.MAX_SAFE_INTEGER minutes, or, in a recipe built by hand, a time that is NaN or a value that Number makes NaN, such as 'abc' or {}, which it leaves out of the JSON-LD. In recipe input, use minutes, such as 30, or a duration, such as 'PT30M'. For the fromJsonLd warning, the JSON-LD needs an ISO 8601 duration, such as 'PT30M'. For the toJsonLd warning, correct the time in the recipe.
invalid_url 'error' A link or image URL has a scheme other than http or https, such as 'javascript:alert(1)' or a data: URL, or the URL parser rejects it, such as 'http://[bad'. Relative URLs the URL parser reads, including protocol-relative ones such as '//cdn.example.com/a.jpg', are fine. fromJsonLd uses it as a warning for an image, an equipment url, or an ingredient link it leaves out. The path is the value’s, such as image[0] or tool[0].url, except for an ingredient link, whose path is its line’s, such as recipeIngredient[3], since a path can’t point inside a string. It also reports this code as a warning for an image URL longer than 4,096 UTF-16 code units, which it leaves out. Use an http or https URL or a relative one, or leave the field out.
unknown_field 'warning' An object has a field Recipe Book doesn’t read, such as a typo like serving, a schema.org name like fatContent in nutrition, whose message suggests fat, or another format’s name like a top-level title, whose message suggests name. A field with no close match, such as rating, gets Unknown field. It is ignored., and a schema.org @type at the top level of recipe input gets Unknown field. For schema.org JSON-LD, use fromJsonLd. Rename the field as the message says, or remove it. Pass schema.org JSON-LD to fromJsonLd.
duplicate_id 'warning' Two items in the same list, such as two ingredients, steps, sections, notes, pieces of equipment, or custom times, have the same id. parseRecipe renames the later one, with the message "egg" is already used; renamed to "egg-2". Give each one a unique id, or leave id out to derive it from the name.
empty_list 'warning' The recipe has no ingredients or no steps, or a section has no items. Its message says which, such as Recipe has no ingredients. or Section has no steps. Add the missing ingredients, steps, or items, or remove the empty section.
ignored_value 'warning' When JSON-LD has more than one of a value the recipe holds once, fromJsonLd keeps one and ignores the rest: a second Recipe node, reported once at the root with the count, another number of servings with a different amount, a second yield, a second duration in a time’s list, or a second, different link in an ingredient line. A servings, yield, or duration warning is at its path, such as recipeYield[2], and a link warning at its line’s, such as recipeIngredient[3]. The message names the value it kept, and a link warning also names the link it ignored. Remove the extra values from the JSON-LD, or leave them if the kept value is the right one.

parseQuantity reports five of these codes for quantity text, in an invalid_quantity error: invalid_type, invalid_quantity, invalid_amount, invalid_range, and unknown_unit. parseQuantity and parseIngredientLine accept any amount that reads as a finite number greater than 0, including one below 1e-300 or above 1e290, which recipe input doesn’t allow, so neither reports invalid_amount for it. A decimal below about 2.5e-324 reads as 0, so it gets invalid_amount, as an amount too large to read does.

fromJsonLd reports invalid_type as a warning for a list item or an image URL nested more than 32 levels deep, counting from the top of its input, or an extra field with a value that deep, and leaves it out.

Paste recipe input into the playground to see its issues, each linked to its fix. It reads text ingredients with parseIngredientLine, so it doesn’t report them as invalid_type.

React development warnings

In development, parts and hooks from @recipe-book/react log these warnings with console.warn and keep rendering. In production, they log nothing. Each quote is the warning for the example beside it, so yours names your own part, prop, recipe, or value.

Warning Logged when
Recipe Book: <Recipe.SystemOption> has asChild but no single element child, received text "Metric", so it renders a <button>. Pass one element, such as <button>.
See https://recipe-book.siraj.page/errors/#warning-as-child
A part with asChild gets anything but one element child, here <Recipe.SystemOption value="metric" asChild>Metric</Recipe.SystemOption>. Only the first of each part to do this warns. Render your own elements explains the rule.
Recipe Book: <Recipe.JsonLd> for "Toast" has 1 warning. The script still renders. https://recipe-book.siraj.page/errors/#issue-codes gives the fix for each warning's code. toJsonLd returns warnings for the recipe of a <Recipe.JsonLd>, here a recipe called “Toast” without an image. It also returns one at options.servings or options.system for a servings or system prop it ignores, such as servings={0}. The warnings follow in the same call.
Recipe Book: Two <Recipe.Root>s for "Buttermilk pancakes" render steps with the idPrefix "buttermilk-pancakes-", so their steps can render the same id. Give each Root its own idPrefix, such as `featured-${recipe.id}-`.
See https://recipe-book.siraj.page/errors/#warning-duplicate-id-prefix
Two mounted <Recipe.Root>s have the same idPrefix, and each has a <Recipe.Step> that renders an id from it, here two Roots with the sample recipe and its steps. Two recipes with the same id, such as two with the same name and no id, have the same default idPrefix, and so does one recipe rendered twice. A Root whose steps render no id from it, such as a recipe card or a Root with only <Recipe.JsonLd>, doesn’t count. The Roots check in the browser after they mount, so the server never logs this, and neither does StrictMode’s second mount. It warns once for each idPrefix until the page reloads, so a duplicate added again later, such as after a Fast Refresh, stays silent.
Recipe Book: `servings` was passed without `onServingsChange`, so it is read-only. Pass `onServingsChange` to update it, or `defaultServings` instead of `servings` for uncontrolled state.
See https://recipe-book.siraj.page/errors/#warning-read-only
<Recipe.Root> or useRecipeState gets servings without onServingsChange, here servings={4}, or system without onSystemChange, where the warning suggests defaultSystem instead. Controlled and uncontrolled state explains both. A <Recipe.Checklist> or useChecklistState warns the same way for checkedIngredients without onCheckedIngredientsChange and checkedSteps without onCheckedStepsChange. A <Recipe.CookMode> does too for checked without onCheckedChange, and suggests defaultChecked.
Recipe Book: `servings` changed from undefined to a value, which switches it from uncontrolled to controlled. Decide between controlled and uncontrolled for the component's lifetime: pass `servings` on every render (never undefined), or pass only `defaultServings`.
See https://recipe-book.siraj.page/errors/#warning-controlled-switch
servings, system, checkedIngredients, checkedSteps, or the checked of a <Recipe.CookMode> changes from undefined to a value, here to servings={4}, or back.
Recipe Book: `servings` must be a number from 0.000004 to 4000000 for this recipe, received -3. Showing 4 instead.
See https://recipe-book.siraj.page/errors/#warning-invalid-servings
A controlled servings isn’t a number in the servings window, here servings={-3} for the sample recipe, which has 4 servings. The Root shows its defaultServings instead, or the recipe’s scaling base without one.
Recipe Book: `defaultServings` must be a number from 0.000004 to 4000000 for this recipe, received 0. Using 4 instead.
See https://recipe-book.siraj.page/errors/#warning-invalid-default-servings
defaultServings isn’t a number in the servings window, here defaultServings={0} for the sample recipe. The Root starts at the recipe’s scaling base instead.
Recipe Book: setServings() expects a number from 0.000004 to 4000000 for this recipe, but received 0. The value was ignored.
See https://recipe-book.siraj.page/errors/#warning-invalid-set-servings
setServings gets anything but a number in the servings window, here setServings(0).
Recipe Book: setServings() expects a number from 4e-9 to 4000 for this recipe, but a setServings from the recipe this one replaced received 3000000. Showing 0.004 instead.
See https://recipe-book.siraj.page/errors/#warning-stale-set-servings
A setServings from before a recipe with another scaling base replaced the recipe, whatever its id, accepted a number the new recipe’s servings window doesn’t include, here setServings(3e6) after the sample recipe gave way to a copy with 0.004 servings. Each setServings checks the window of the recipe it came from. The Root shows its defaultServings instead, or the recipe’s scaling base without one.
Recipe Book: `system` must be "metric" or "us", received "imperial". Showing "us" instead.
See https://recipe-book.siraj.page/errors/#warning-invalid-system
A controlled system is neither 'metric' nor 'us', here system="imperial" for the sample recipe, whose default system is US. The Root shows its default system instead.
Recipe Book: `defaultSystem` must be "metric" or "us", received "imperial". Using "us" instead.
See https://recipe-book.siraj.page/errors/#warning-invalid-default-system
defaultSystem is neither 'metric' nor 'us', here defaultSystem="imperial" for the sample recipe, whose default system is US. The Root starts in that default system, as if defaultSystem were missing. On a <Recipe.Provider>, the warning names the provider, and each Root or useRecipeState() call below it starts as if the provider had no defaultSystem.
Recipe Book: `defaultSystem` on <Recipe.Provider> must be "metric" or "us", received "imperial". Each Root or useRecipeState() call below it starts as if the provider had no `defaultSystem`.
See https://recipe-book.siraj.page/errors/#warning-invalid-default-system
defaultSystem on a <Recipe.Provider> is neither 'metric' nor 'us', here <Recipe.Provider defaultSystem="imperial">.
Recipe Book: setSystem() expects "metric" or "us", but received "imperial". The value was ignored.
See https://recipe-book.siraj.page/errors/#warning-invalid-set-system
setSystem gets anything but 'metric' or 'us', here setSystem('imperial').
Recipe Book: `defaultCheckedSteps` must be an array of strings, received "step-1". Checking none instead.
See https://recipe-book.siraj.page/errors/#warning-invalid-default-checked
defaultCheckedIngredients or defaultCheckedSteps of a <Recipe.Checklist> or useChecklistState isn’t an array of strings, here defaultCheckedSteps="step-1". Those ingredients or steps start with none ticked.
Recipe Book: `checkedSteps` must be an array of strings, received an array holding 1. Checking none instead.
See https://recipe-book.siraj.page/errors/#warning-invalid-checked
A controlled checkedIngredients or checkedSteps isn’t an array of strings, here checkedSteps={[1]}. Those ingredients or steps show none ticked.
Recipe Book: setCheckedSteps() expects an array of strings, but received "step-1". The value was ignored.
See https://recipe-book.siraj.page/errors/#warning-invalid-set-checked
setCheckedIngredients or setCheckedSteps gets anything but an array of strings, here setCheckedSteps('step-1').
Recipe Book: <Recipe.CookMode> can't keep the screen on, so it turned off: NotAllowedError: Failed to execute 'request' on 'WakeLock': Access to Screen Wake Lock features is disallowed by permissions policy. In a cross-origin iframe, the iframe needs allow="screen-wake-lock".
See https://recipe-book.siraj.page/errors/#warning-wake-lock
A <Recipe.CookMode> is on and the browser refuses to keep the screen on while the page is visible, here in a cross-origin iframe without allow="screen-wake-lock". It can refuse as soon as the switch is on, or when the page is visible again and the switch asks again. The middle of the warning is the browser’s own error, which differs between browsers. In that iframe, Firefox’s ends for the requesting document. A browser that has denied the wake lock permission refuses anywhere, with an error such as Safari’s Permission was denied. Safari gives the same error for the page’s first wake lock if the switch asks with no click shortly before, such as one that starts on in the page’s server HTML.

The servings and system warnings, and those for checkedIngredients and checkedSteps, name values of other types the same way, such as received null, received true, received an object, or received a function.

The asChild warning names your part and the element it renders instead:

It says what the part received instead of one element: text, a Fragment, an array of children, a function child, no children, or a child that is not an element.

Contents