yikart/AiToEarn · error · NotFoundException
任务不存在
Error message
任务不存在
What it means
NestJS 404 thrown in the user-facing apply-for-task endpoint (task.controller.ts) when taskService.findOne(id) returns no task. The application cannot proceed because the target task does not exist.
Source
Thrown at project/aitoearn-electron/server/src/modules/task/task.controller.ts:82
@Public()
@Get('info/:id')
@ApiOperation({ summary: '获取任务详情' })
@ApiResult({ type: Task })
async findOne(@Param('id') id: string) {
return await this.taskService.findOne(id);
}
@ApiOperation({ summary: '申请任务: 保持一定时间' })
@ApiResult({ type: Boolean })
@Post('apply/:id')
async applyForTask(
@Param('id') id: string,
@GetToken() token: TokenInfo,
@Body() body: ApplyTaskDto,
) {
const task = await this.taskService.findOne(id);
if (!task) throw new NotFoundException('任务不存在');
if (!!body.taskMaterialId) {
const taskMaterial = await this.taskService.getTaskMaterialById(
body.taskMaterialId,
);
if (!taskMaterial) throw new NotFoundException('任务素材不存在');
}
const res = await this.userTaskService.userApplyTask(token.id, task, body);
if (!!body.taskMaterialId)
this.taskService.upTaskMaterialUsedCount(body.taskMaterialId);
return res;
}
@ApiOperation({ summary: '提交任务 id是用户任务ID' })
@ApiResult({ type: Boolean })View on GitHub (pinned to d3aa8bea5b)
Solutions
- Re-fetch the task list and use a current task id.
- Validate the id is a valid ObjectId before applying.
- Handle 404 in the client by showing 'task no longer available' instead of retrying.
Example fix
// before
const task = await this.taskService.findOne(id);
if (!task) throw new NotFoundException('任务不存在');
// after
import { isValidObjectId } from 'mongoose';
if (!isValidObjectId(id)) throw new BadRequestException('Invalid task id');
const task = await this.taskService.findOne(id);
if (!task) throw new NotFoundException(`Task ${id} not found or unavailable`); Defensive patterns
Strategy: try-catch
Validate before calling
import { isValidObjectId } from 'mongoose';
if (!isValidObjectId(id)) throw new Error('Invalid task id');
const task = await api.getTask(id);
if (!task) throw new Error('Task unavailable'); Type guard
function hasTask(t: unknown): t is { id: string } {
return !!t && typeof t === 'object' && 'id' in t;
} Try / catch
try {
await api.applyForTask(id, body);
} catch (e) {
if (e.response?.status === 404) {
showUserMessage('该任务已下线或不存在');
return;
}
throw e;
} Prevention
- Fetch task ids only from live task-list endpoints, not shared links or caches.
- Show a friendly 'task unavailable' state for 404 in the client.
- Invalidate cached task lists after task unpublication.
When it happens
Trigger: POST-style apply call with a task id that was unpublished/deleted, never existed, or is an invalid ObjectId.
Common situations: User opening an old share link to a removed task; task list cache pointing at a delisted task; app pointing at a different environment's task ids.
Understand the failure class
Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.
Related errors
AI-assisted analysis of yikart/AiToEarn@d3aa8bea5b (2026-08-31).
Data as JSON: /api/errors/fffd9fa0182ebf45.
Report an issue: GitHub.