jackwener/OpenCLI · error · AuthRequiredError

未获取到课程列表

Error message

未获取到课程列表

What it means

The assignments command first establishes a Chaoxing session and calls getCourses; if the course list comes back empty it throws AuthRequiredError('mooc2-ans.chaoxing.com','未获取到课程列表') because an authenticated account always has at least one course — an empty list means the session is not really logged in.

Source

Thrown at clis/chaoxing/assignments.js:31

        {
            name: 'status',
            type: 'string',
            default: 'all',
            choices: ['all', 'pending', 'submitted', 'graded'],
            help: '按状态过滤',
        },
        { name: 'limit', type: 'int', default: 20, help: '最大返回数量' },
        { name: 'timeout', type: 'int', required: false, default: 90, help: 'Max seconds for the overall command (default: 90)' },
    ],
    columns: ['rank', 'course', 'title', 'deadline', 'status', 'score'],
    func: async (page, kwargs) => {
        const { course: courseFilter, status: statusFilter = 'all', limit = 20 } = kwargs;
        // 1. Establish session
        await initSession(page);
        // 2. Get courses
        const courses = await getCourses(page);
        if (!courses.length)
            throw new AuthRequiredError('mooc2-ans.chaoxing.com', '未获取到课程列表');
        const filtered = courseFilter
            ? courses.filter(c => c.title.includes(courseFilter))
            : courses;
        if (courseFilter && !filtered.length) {
            throw new Error(`未找到匹配「${courseFilter}」的课程`);
        }
        // 3. Per-course: enter → click 作业 tab → navigate to iframe → parse
        const allRows = [];
        for (const c of filtered) {
            try {
                await enterCourse(page, c);
                const iframeUrl = await getTabIframeUrl(page, '作业');
                if (!iframeUrl)
                    continue;
                await page.goto(iframeUrl);
                await page.wait(2);
                const rows = await parseAssignmentsFromDom(page, c.title);
                allRows.push(...rows);

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Re-authenticate: run the chaoxing login command to refresh cookies, then retry assignments
  2. Verify cookies include UID/_uid/cx_p_token for i.chaoxing.com before running
  3. Confirm the account actually has enrolled courses at mooc2-ans.chaoxing.com
  4. Check whether getCourses was redirected to the login page (initSession may have silently failed)

Example fix

// before
const courses = await getCourses(page);
if (!courses.length) throw new AuthRequiredError('mooc2-ans.chaoxing.com', '未获取到课程列表');
// after
let courses = await getCourses(page);
if (!courses.length) { await login(page); courses = await getCourses(page); }
if (!courses.length) throw new AuthRequiredError('mooc2-ans.chaoxing.com', '未获取到课程列表');
Defensive patterns

Strategy: try-catch

Validate before calling

const cookies = await page.getCookies({ url: 'https://i.chaoxing.com' });
if (!cookies.some(c => /^(UID|_uid|cx_p_token)$/i.test(c.name) && c.value)) await chaoxingLogin(page);

Type guard

null

Try / catch

try { const rows = await assignments(page, kwargs); }
catch (e) {
  if (e instanceof AuthRequiredError && e.message.includes('未获取到课程列表')) { await login(page); return assignments(page, kwargs); }
  throw e;
}

Prevention

When it happens

Trigger: Running assignments without a valid Chaoxing login (expired cookies, failed initSession, captcha not solved) so getCourses returns zero rows.

Common situations: Stale saved cookies after password change; login page redirect not detected; new account with no enrolled courses; school SSO session expiring.

Related errors


AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29). Data as JSON: /api/errors/40dfb2f0ad263517. Report an issue: GitHub.