{"record":{"id":"732173682ba3a425","repo":"iflytek/astron-agent","slug":"8519-database-template-generate-failed","errorCode":"8519","errorMessage":"database.template.generate.failed","messagePattern":"database\\.template\\.generate\\.failed","errorType":"error_code","errorClass":"BusinessException","httpStatus":null,"severity":"error","filePath":"console/backend/toolkit/src/main/java/com/iflytek/astron/console/toolkit/service/database/DatabaseService.java","lineNumber":832,"sourceCode":"\n            List<List<String>> head = new ArrayList<>();\n            for (DbTableField field : fields) {\n                // The header is the field name\n                if (Arrays.asList(SYSTEM_FIELDS).contains(field.getName())) {\n                    continue;\n                }\n                head.add(Collections.singletonList(field.getName()));\n            }\n\n            // Generate a file stream using EasyExcel, writing only the header row\n            EasyExcel.write(response.getOutputStream())\n                    .head(head)\n                    .sheet(\"模版\")\n                    .doWrite(new ArrayList<>());\n\n        } catch (Exception ex) {\n            log.error(\"Template generation failed, tbId={}\", tbId, ex);\n            throw new BusinessException(ResponseEnum.DATABASE_TEMPLATE_GENERATE_FAILED);\n        }\n\n    }\n\n    public Page<JSONObject> selectTableData(DbTableSelectDataDto dto) {\n        dataPermissionCheckTool.checkTbBelong(dto.getTbId());\n        try {\n            Page<JSONObject> page = new Page<>(dto.getPageNum(), dto.getPageSize());\n            page.setSize(Math.min(page.getSize(), MAX_PAGE_SIZE));\n\n            DbTable dbTable = dbTableMapper.selectById(dto.getTbId());\n            DbInfo dbInfo = dbInfoMapper.selectById(dbTable.getDbId());\n\n            String table = dialect.quoteIdent(dbTable.getName());\n            long limit = page.getSize();\n            long offset = (page.getCurrent() - 1) * page.getSize();\n            if (limit < 0 || offset < 0)\n                throw new IllegalArgumentException(\"Bad paging\");","sourceCodeStart":814,"sourceCodeEnd":850,"githubUrl":"https://github.com/iflytek/astron-agent/blob/5e758547a83371a5a4b29dadf4ac03e8dd527635/console/backend/toolkit/src/main/java/com/iflytek/astron/console/toolkit/service/database/DatabaseService.java#L814-L850","documentation":"Thrown when DatabaseService fails to generate an import/export Excel template for a table using EasyExcel. Code 8519, message 'database.template.generate.failed'. The catch-all wraps any exception during workbook writing (head building, sheet creation, response streaming).","triggerScenarios":"Calling the template-download endpoint for a tbId when EasyExcel .doWrite fails: HttpServletResponse already committed, broken client connection mid-write, too many/odd field names for headers, or memory pressure building the head list.","commonSituations":"User cancels download causing broken pipe; response output stream closed by an earlier filter; huge number of columns causing OOM; EasyExcel version incompatibility with POI.","solutions":["Check log 'Template generation failed, tbId=...' for the root-cause stack trace.","Ensure no filter/interceptor wrote to or committed the response before the template is streamed.","Retry the download; if broken pipe, it is a client-side cancellation, not a server bug.","If OOM, reduce column count or raise heap; verify EasyExcel/POI versions match."],"exampleFix":"// before\nresponse.setContentType(\"application/octet-stream\"); // wrong/absent headers can break stream\nEasyExcel.write(out).head(head).sheet(\"模版\").doWrite(new ArrayList<>());\n// after\nresponse.setContentType(\"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\");\nresponse.setHeader(\"Content-Disposition\", \"attachment;filename=template.xlsx\");\nEasyExcel.write(response.getOutputStream()).head(head).sheet(\"模版\").doWrite(new ArrayList<>());","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    templateApi.downloadTemplate(tbId);\n} catch (BusinessException ex) {\n    if (\"database.template.generate.failed\".equals(ex.getMessage())) {\n        log.error(\"template generation failed; retry download\", ex);\n    }\n    throw ex;\n}","preventionTips":["Don't cancel the download mid-stream; broken pipes surface as this error","Ensure no filter writes to the response before the template stream","Keep EasyExcel/POI versions compatible"],"tags":["excel","easyexcel","export"],"backgroundTag":"file-write-failed","analyzedSha":"5e758547a83371a5a4b29dadf4ac03e8dd527635","analyzedAt":"2026-09-12T08:03:51.356Z","contentChangedAt":"2026-09-12T08:03:51.356Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}