{"record":{"id":"c1829def3a30dd21","repo":"SonarSource/sonarqube","slug":"s-key-s-not-found","errorCode":null,"errorMessage":"%s key '%s' not found","messagePattern":"(.+?) key '(.+?)' not found","errorType":"http","errorClass":"NotFoundException","httpStatus":404,"severity":"error","filePath":"server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ComponentFinder.java","lineNumber":214,"sourceCode":"  private ComponentDto getByKey(DbSession dbSession, String key, String label) {\n    return checkComponent(dbSession, dbClient.componentDao().selectByKey(dbSession, key), \"%s key '%s' not found\", label, key);\n  }\n\n  public ComponentDto getByUuidFromMainBranch(DbSession dbSession, String uuid) {\n    return getByUuidFromMainBranch(dbSession, uuid, LABEL_COMPONENT);\n  }\n\n  private ComponentDto getByUuidFromMainBranch(DbSession dbSession, String uuid, String label) {\n    return checkComponent(dbSession, dbClient.componentDao().selectByUuid(dbSession, uuid), \"%s id '%s' not found\", label, uuid);\n  }\n\n  private ComponentDto checkComponent(DbSession session, Optional<ComponentDto> componentDto, String message, Object... messageArguments) {\n    if (componentDto.isPresent() && componentDto.get().isEnabled()) {\n      if (dbClient.branchDao().selectByUuid(session, componentDto.get().branchUuid()).map(BranchDto::isMain).orElse(true)) {\n        return componentDto.get();\n      }\n    }\n    throw new NotFoundException(format(message, messageArguments));\n  }\n\n  public ComponentDto getRootComponentByUuidOrKey(DbSession dbSession, @Nullable String projectUuid, @Nullable String projectKey) {\n    ComponentDto project;\n    if (projectUuid != null) {\n      project = getByUuidFromMainBranch(dbSession, projectUuid, LABEL_PROJECT);\n    } else {\n      project = getByKey(dbSession, projectKey, LABEL_PROJECT);\n    }\n    checkIsProject(project);\n\n    return project;\n  }\n\n  private ComponentDto checkIsProject(ComponentDto component) {\n    Set<String> rootQualifiers = getRootQualifiers(componentTypes);\n\n    checkRequest(component.scope().equals(ComponentScopes.PROJECT) && rootQualifiers.contains(component.qualifier()),","sourceCodeStart":196,"sourceCodeEnd":232,"githubUrl":"https://github.com/SonarSource/sonarqube/blob/184c821202192afc1c599fc912d0889b69fffa53/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ComponentFinder.java#L196-L232","documentation":"checkComponent validates an Optional<ComponentDto> and throws NotFoundException with the caller-supplied message pattern ('%s key '%s' not found', e.g. 'Project key 'x' not found') when the component is missing, disabled, or does not live on a main branch. getByKey and getByUuidFromMainBranch route through it.","triggerScenarios":"Lookup by key/uuid where the component row is absent, the component is disabled (deleted flag), or its branch is not the main branch (e.g. UUID of a non-main branch component passed to getByUuidFromMainBranch).","commonSituations":"Using a branch component's UUID in APIs that expect main-branch components; referencing a deleted/disabled project; stale cached UUIDs after re-analysis.","solutions":["Use the project key instead of a stored UUID, or re-resolve the UUID from api/components/search","Confirm the UUID belongs to the main branch component (branchUuid maps to a main BranchDto)","Re-enable or recreate the component if it was disabled/deleted","Refresh any cached component references after project key changes or deletions"],"exampleFix":"// before\nclient.components.show({ component: staleUuid })\n// after\nconst search = await client.components.searchProjects({ q: 'my-project' });\nconst key = search.components[0].key;\nawait client.components.show({ component: key });","handlingStrategy":"validation","validationCode":"// re-resolve the component key instead of a stored main-branch uuid\nconst res = await api.components.searchProjects({ q: name });\nconst c = res.components.find(p => p.key === name);\nif (!c || c.enabled === false) throw new Error(`component '${name}' missing or disabled`);","typeGuard":null,"tryCatchPattern":"try {\n  const comp = await api.components.show({ component: idOrKey });\n} catch (e) {\n  if (e.status === 404 && /not found/.test(e.message)) {\n    // treat as: deleted, disabled, or non-main-branch uuid — re-resolve by key\n    return resolveComponentByKey(name);\n  }\n  throw e;\n}","preventionTips":["Prefer component keys over cached UUIDs in scripts","Never use branch-component UUIDs in main-branch APIs","Purge caches of component references after deletions/re-analyses","Check 'enabled' flag when listing components"],"tags":["java","components","not-found"],"backgroundTag":"entity-not-found","analyzedSha":"184c821202192afc1c599fc912d0889b69fffa53","analyzedAt":"2026-09-09T12:23:51.573Z","contentChangedAt":"2026-09-09T12:23:51.573Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}