{"record":{"id":"c00997d861e2ee52","repo":"apache/dolphinscheduler","slug":"query-environment-by-code-error","errorCode":"QUERY_ENVIRONMENT_BY_CODE_ERROR","errorMessage":"Status.QUERY_ENVIRONMENT_BY_CODE_ERROR","messagePattern":"Status\\.QUERY_ENVIRONMENT_BY_CODE_ERROR","errorType":"error_code","errorClass":"ServiceException","httpStatus":null,"severity":"error","filePath":"dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/EnvironmentServiceImpl.java","lineNumber":239,"sourceCode":"\n        return environmentList.stream().map(environment -> {\n            EnvironmentDto dto = new EnvironmentDto();\n            BeanUtils.copyProperties(environment, dto);\n            dto.setWorkerGroups(relationMap.getOrDefault(environment.getCode(), new ArrayList<>()));\n            return dto;\n        }).collect(Collectors.toList());\n    }\n\n    /**\n     * query environment\n     *\n     * @param code environment code\n     */\n    @Override\n    public EnvironmentDto queryEnvironmentByCode(Long code) {\n        Environment env = environmentMapper.queryByEnvironmentCode(code);\n        if (env == null) {\n            throw new ServiceException(Status.QUERY_ENVIRONMENT_BY_CODE_ERROR, code);\n        }\n        List<String> workerGroups = relationMapper.queryByEnvironmentCode(env.getCode()).stream()\n                .map(EnvironmentWorkerGroupRelation::getWorkerGroup)\n                .collect(Collectors.toList());\n\n        EnvironmentDto dto = new EnvironmentDto();\n        BeanUtils.copyProperties(env, dto);\n        dto.setWorkerGroups(workerGroups);\n        return dto;\n    }\n\n    /**\n     * query environment\n     *\n     * @param name environment name\n     */\n    @Override\n    public EnvironmentDto queryEnvironmentByName(String name) {","sourceCodeStart":221,"sourceCodeEnd":257,"githubUrl":"https://github.com/apache/dolphinscheduler/blob/02eac45a1b6676e639fcbfb4be2243de5771b05d/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/EnvironmentServiceImpl.java#L221-L257","documentation":"Thrown by EnvironmentServiceImpl.queryEnvironmentByCode when no environment row matches the given code (environmentMapper.queryByEnvironmentCode returns null). Environment codes are snowflake-style generated Longs; querying a code that was never created — or was deleted — produces this error.","triggerScenarios":"GET /dolphinscheduler/environments/{code} with a code that does not exist; using a stale code cached from a deleted environment; passing a task's environment code that references an environment removed by another user.","commonSituations":"Workflows referencing environments deleted after the task was defined; stale configuration in deployment scripts; typo or truncation of the numeric code.","solutions":["List all environments (GET /dolphinscheduler/environments/list) and use the correct code.","Recreate the missing environment and update task definitions to reference the new code.","Fix the calling code to fetch the code dynamically rather than hardcoding it."],"exampleFix":"// before\nEnvironmentDto env = environmentService.queryEnvironmentByCode(staleCode);\n// after\nList<EnvironmentDto> all = environmentService.queryEnvironmentList();\nEnvironmentDto env = all.stream()\n        .filter(e -> e.getName().equals(\"default\"))\n        .findFirst()\n        .orElseThrow(() -> new IllegalStateException(\"Environment 'default' not found\"));","handlingStrategy":"try-catch","validationCode":"public boolean environmentCodeExists(long code) {\n    try {\n        environmentService.queryEnvironmentByCode(code);\n        return true;\n    } catch (ServiceException e) {\n        return false;\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    EnvironmentDto env = environmentService.queryEnvironmentByCode(code);\n} catch (ServiceException e) {\n    // QUERY_ENVIRONMENT_BY_CODE_ERROR: refresh from environment list or recreate\n    List<EnvironmentDto> all = environmentService.queryEnvironmentList();\n}","preventionTips":["Never hardcode environment codes; resolve them by name from the list API at runtime","Handle deleted environments in workflows by re-resolving codes before use","Sync environment codes between clusters during migration tooling"],"tags":["dolphinscheduler","not-found","environment","lookup"],"backgroundTag":"record-not-found","analyzedSha":"02eac45a1b6676e639fcbfb4be2243de5771b05d","analyzedAt":"2026-09-06T17:43:00.555Z","contentChangedAt":"2026-09-06T17:43:00.555Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}