Skip to content

UnhandledPromiseRejection in downloadArtifact #2031

Open
@yury-s

Description

@yury-s

Describe the bug
While running await client.downloadArtifact(...) I'm getting the following exception:

::debug::Artifact destination folder does not exist, creating: /Users/<user>/sandbox/octokit/all-blob-reports/blob-report-ubuntu-latest-node18-1
node:internal/process/promises:392
      new UnhandledPromiseRejection(reason);
      ^

UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "InvalidTokenError: Invalid token specified: Cannot read properties of undefined (reading 'replace')".
    at throwUnhandledRejectionsMode (node:internal/process/promises:392:7)
    at processPromiseRejections (node:internal/process/promises:475:17)
    at process.processTicksAndRejections (node:internal/process/task_queues:106:32) {
  code: 'ERR_UNHANDLED_REJECTION'
}

To Reproduce
Run the following script locally with your token:

import { DefaultArtifactClient, FindOptions } from '@actions/artifact';
import * as core from '@actions/core';
import * as fs from 'fs';
import * as path from 'path';


process.env.ACTIONS_STEP_DEBUG = 'true';
process.env.GITHUB_TOKEN =  'xxx';
process.env.ACTIONS_RUNTIME_TOKEN = process.env.GITHUB_TOKEN;
process.env.ACTIONS_RUNTIME_URL='http://localhost/fake';
process.env.ACTIONS_RESULTS_URL='http://localhost/fake_results';

function getGithubToken() {
  const token = process.env.GITHUB_TOKEN || core.getInput('github-token');
  if (!token) {
    core.setFailed('Missing "github-token" input');
    throw new Error('Missing "github-token" input');
  }
  return token;
}

async function downloadArtifacts(runId: number, dir: string) {

  const client = new DefaultArtifactClient();
  const findBy: FindOptions['findBy'] = {
    workflowRunId: runId,
    repositoryOwner: 'microsoft',
    repositoryName: 'playwright',
    token: getGithubToken(),
  };

  const allArtifacts = await client.listArtifacts({
    latest: true,
    findBy
  });
  const artifacts = allArtifacts.artifacts.filter(a => a.name.startsWith('blob-report-ubuntu-latest-'));
  console.log(artifacts.map(a => a.name));

  const prefix = path.resolve(process.cwd(), dir);
  await fs.promises.mkdir(prefix, { recursive: true });
  for (const artifact of artifacts) {
    const filePath = path.join(prefix, `${artifact.name}`);
    console.log(`* Downloading ${artifact.name}.zip to ${filePath}`);
    const result = await client.downloadArtifact(artifact.id,
      {
        ...findBy,
        path: filePath,
      }
    );
  }
}

downloadArtifacts(14498356788, 'all-blob-reports');

Expected behavior
All artifacts are downloaded.

Desktop (please complete the following information):

  • OS: maOS 15.4.1
  • Node.js v22.11.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions