elunez/eladmin · error · BadRequestException

没有这个选项

Error message

没有这个选项

What it means

GeneratorController.generatorCode switches on the path variable 'type' and only accepts 0 (generate), 1 (preview), 2 (download). Any other integer falls to the default branch and throws BadRequestException('没有这个选项'). It is pure input validation on the URL path.

Source

Thrown at eladmin-generator/src/main/java/me/zhengjie/rest/GeneratorController.java:105

        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("生成代码")
    @PostMapping(value = "/{tableName}/{type}")
    public ResponseEntity<Object> generatorCode(@PathVariable String tableName, @PathVariable Integer type, HttpServletRequest request, HttpServletResponse response){
        if(!generatorEnabled && type == 0){
            throw new BadRequestException("此环境不允许生成代码,请选择预览或者下载查看!");
        }
        switch (type){
            // 生成代码
            case 0: generatorService.generator(genConfigService.find(tableName), generatorService.getColumns(tableName));
                    break;
            // 预览
            case 1: return generatorService.preview(genConfigService.find(tableName), generatorService.getColumns(tableName));
            // 打包
            case 2: generatorService.download(genConfigService.find(tableName), generatorService.getColumns(tableName), request, response);
                    break;
            default: throw new BadRequestException("没有这个选项");
        }
        return new ResponseEntity<>(HttpStatus.OK);
    }
}

View on GitHub (pinned to 55fbf70595)

Solutions

  1. Send a valid type: 0 to generate, 1 to preview, 2 to download.
  2. Align frontend and backend versions so the UI only emits supported type values.
  3. If you are adding a new generation mode, extend the switch and keep the default branch as the guard.

Example fix

// before
curl -X POST http://host/api/generator/sys_user/3

// after
curl -X POST http://host/api/generator/sys_user/2   // 0=generate 1=preview 2=download
Defensive patterns

Strategy: validation

Validate before calling

// Client: whitelist the type before calling
int type = params.type;
if (type < 0 || type > 2) {
    throw new Error("type must be 0(生成) 1(预览) 2(下载)");
}
post(`/api/generator/${table}/${type}`);

Type guard

function isValidGeneratorType(t) {
  return [0, 1, 2].includes(t);
}

Try / catch

try {
    return post("/api/generator/" + table + "/" + type);
} catch (BadRequestException e) {
    if ("没有这个选项".equals(e.getMessage())) throw new IllegalArgumentException("Unsupported generator type: " + type);
    throw e;
}

Prevention

When it happens

Trigger: POST /api/generator/{tableName}/3 (or any type other than 0, 1, 2). Happens with hand-crafted requests, outdated frontend sending a type value the backend does not support, or typos in scripts.

Common situations: Version skew between an updated frontend (new action type) and older backend; manual curl/API testing; browser autofill or URL editing producing an unexpected integer.

Related errors


AI-assisted analysis of elunez/eladmin@55fbf70595 (2026-08-14). Data as JSON: /api/errors/7fa79fde3119e961. Report an issue: GitHub.