{"record":{"id":"82caf107a2678976","repo":"dianping/cat","slug":"reduceright-of-empty-array-with-no-initial-value","errorCode":null,"errorMessage":"reduceRight of empty array with no initial value","messagePattern":"reduceRight of empty array with no initial value","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"cat-home/src/main/webapp/assets/js/editor/worker-xquery.js","lineNumber":49147,"sourceCode":"                result = fun.call(void 0, result, self[i], i, object);\n            }\n        }\n\n        return result;\n    };\n}\nif (!Array.prototype.reduceRight) {\n    Array.prototype.reduceRight = function reduceRight(fun /*, initial*/) {\n        var object = toObject(this),\n            self = splitString && _toString(this) == \"[object String]\" ?\n                this.split(\"\") :\n                object,\n            length = self.length >>> 0;\n        if (_toString(fun) != \"[object Function]\") {\n            throw new TypeError(fun + \" is not a function\");\n        }\n        if (!length && arguments.length == 1) {\n            throw new TypeError(\"reduceRight of empty array with no initial value\");\n        }\n\n        var result, i = length - 1;\n        if (arguments.length >= 2) {\n            result = arguments[1];\n        } else {\n            do {\n                if (i in self) {\n                    result = self[i--];\n                    break;\n                }\n                if (--i < 0) {\n                    throw new TypeError(\"reduceRight of empty array with no initial value\");\n                }\n            } while (true);\n        }\n\n        do {","sourceCodeStart":49129,"sourceCodeEnd":49165,"githubUrl":"https://github.com/dianping/cat/blob/e815e74d4c2dd74edac831241f1253fcc7d25381/cat-home/src/main/webapp/assets/js/editor/worker-xquery.js#L49129-L49165","documentation":"ES5-shim Array.prototype.reduceRight throws 'reduceRight of empty array with no initial value' when length is 0 and no initialValue argument was given — the right-to-left twin of reduce's rule that an accumulator must come from somewhere.","triggerScenarios":"[].reduceRight(fn); — empty array after a filter/map chain, a queue drained to zero, or data not yet arrived at reduce time.","commonSituations":"Reducing an empty filtered set; race where reduce runs before data loads; empty tail recursion helper arrays.","solutions":["Always supply the initial value: arr.reduceRight(fn, init).","Short-circuit empty inputs with a default result before calling reduceRight.","Initialize the accumulator consistently so first-iteration behavior is deterministic."],"exampleFix":"// before\nvar composed = fns.reduceRight(compose2);\n\n// after\nvar composed = fns.reduceRight(compose2, function(x) { return x; });","handlingStrategy":"validation","validationCode":"if (arr.length === 0 && arguments.length < 2) {\n  return identity; // or default — avoids the empty-array TypeError\n}\nreturn arr.reduceRight(fn, initialValue);","typeGuard":null,"tryCatchPattern":"try { return arr.reduceRight(fn); }\ncatch (e) {\n  if (e instanceof TypeError && /no initial value/.test(e.message)) {\n    return fallbackValue;\n  }\n  throw e;\n}","preventionTips":["Always pass an explicit initial value to reduceRight.","Short-circuit empty collections with a default result before folding."],"tags":["javascript","array","es5-shim","empty-array","typeerror"],"backgroundTag":null,"analyzedSha":"e815e74d4c2dd74edac831241f1253fcc7d25381","analyzedAt":"2026-08-14T14:22:34.512Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}