{"record":{"id":"42f2fb32e41f2365","repo":"TheAlgorithms/JavaScript","slug":"grid-must-be-a-non-empty-array","errorCode":null,"errorMessage":"Grid must be a non-empty array","messagePattern":"Grid must be a non-empty array","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Backtracking/RatInAMaze.js","lineNumber":26,"sourceCode":" * Reference for this problem: https://www.geeksforgeeks.org/rat-in-a-maze-backtracking-2/\n *\n * Based on the original implementation contributed by Chiranjeev Thapliyal (https://github.com/chiranjeev-thapliyal).\n */\n\n/**\n * Checks if the given grid is valid.\n *\n * A grid needs to satisfy these conditions:\n * - must not be empty\n * - must be a square\n * - must not contain values other than {@code 0} and {@code 1}\n *\n * @param grid The grid to check.\n * @throws TypeError When the given grid is invalid.\n */\nfunction validateGrid(grid) {\n  if (!Array.isArray(grid) || grid.length === 0)\n    throw new TypeError('Grid must be a non-empty array')\n\n  const allRowsHaveCorrectLength = grid.every(\n    (row) => row.length === grid.length\n  )\n  if (!allRowsHaveCorrectLength) throw new TypeError('Grid must be a square')\n\n  const allCellsHaveValidValues = grid.every((row) => {\n    return row.every((cell) => cell === 0 || cell === 1)\n  })\n  if (!allCellsHaveValidValues)\n    throw new TypeError('Grid must only contain 0s and 1s')\n}\n\nfunction isSafe(grid, x, y) {\n  const n = grid.length\n  return x >= 0 && x < n && y >= 0 && y < n && grid[y][x] === 1\n}\n","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Backtracking/RatInAMaze.js#L8-L44","documentation":"Thrown by validateGrid() in the Rat-in-a-Maze solver before any backtracking begins. The solver indexes grid[y][x], so it requires an actual Array with at least one row; null, undefined, objects, primitives, or an empty array are all rejected up front.","triggerScenarios":"Calling solve()/validateGrid with null, undefined, [], {}, a number, a string, a Set, or any non-Array value. Also triggered when a JSON parse returns null/empty and that result is passed straight through.","commonSituations":"Loading the maze from a JSON config that failed to parse, defaulting an unset option to [] (empty), or passing a matrix-library wrapper / typed array instead of a plain Array of Arrays.","solutions":["Pass a non-empty square 2D array of 0/1 values, e.g. [[1,0,1],[0,1,0],[0,0,1]].","If the grid comes from JSON or a fetch, verify Array.isArray(grid) && grid.length>0 before calling the solver.","Ensure the variable is not shadowed or accidentally set to undefined by a failed map/assignment."],"exampleFix":"// before\nconst path = solveRatInAMaze(parsed ?? [])\n// after\nconst path = Array.isArray(parsed) && parsed.length\n  ? solveRatInAMaze(parsed)\n  : null","handlingStrategy":"validation","validationCode":"function isValidMazeGrid(g) {\n  if (!Array.isArray(g) || g.length === 0) return false;\n  const n = g.length;\n  return g.every(r => Array.isArray(r) && r.length === n && r.every(c => c === 0 || c === 1));\n}\n// if (isValidMazeGrid(grid)) solve(grid);","typeGuard":"/** @param {unknown} g @returns {g is number[][]} */\nfunction isSquareBinaryGrid(g) {\n  if (!Array.isArray(g) || g.length === 0) return false;\n  const n = g.length;\n  return g.every(r => Array.isArray(r) && r.length === n &&\n    r.every(c => c === 0 || c === 1));\n}","tryCatchPattern":"try { solve(grid); }\ncatch (e) {\n  if (e instanceof TypeError && /non-empty array/.test(e.message)) {\n    // handle bad/missing grid\n  } else throw e;\n}","preventionTips":["Validate grid shape and cell values at the data-loading boundary, not at solve time.","Parse maze text into numbers explicitly: row.split('').map(Number).","Keep visited/state separate from the source grid to avoid needing values beyond 0/1."],"tags":["backtracking","maze","grid","input-validation"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}