Skip to content

Commit b7cb0df

Browse files
committed
fix package fetching error handling
1 parent 3b342d7 commit b7cb0df

4 files changed

Lines changed: 36 additions & 24 deletions

File tree

.changeset/three-fireants-serve.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ryanatkn/orc': patch
3+
---
4+
5+
fix package fetching error handling

src/lib/fetch_packages.ts

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,17 @@ import type {Logger} from '@grogarden/util/log.js';
55
import {wait} from '@grogarden/util/async.js';
66
import {parse_package_meta, type PackageMeta} from '@fuz.dev/fuz_library/package_meta.js';
77
import {request} from '@octokit/request';
8-
import {GITHUB_TOKEN_SECRET} from '$env/static/private';
98

109
// TODO rethink with `Package` and `FetchedPackage2`
11-
export interface FetchedPackage {
10+
export interface MaybeFetchedPackage {
1211
url: Url;
1312
package_json: PackageJson | null; // TODO forward error
14-
pulls: GithubIssue[];
13+
pulls: GithubIssue[] | null;
1514
}
1615

1716
type GithubIssue = any; // TODO
1817

19-
// TODO obviously bad names
20-
export interface FetchedPackage2 extends PackageMeta {
18+
export interface FetchedPackage extends PackageMeta {
2119
pulls: GithubIssue[] | null;
2220
}
2321

@@ -28,28 +26,33 @@ export interface UnfetchablePackage {
2826
}
2927

3028
// TODO rethink these names
31-
export type FetchedPackageMeta = FetchedPackage2 | UnfetchablePackage;
29+
export type FetchedPackageMeta = FetchedPackage | UnfetchablePackage;
3230

3331
/* eslint-disable no-await-in-loop */
3432

3533
export const fetch_packages = async (
3634
urls: Url[],
35+
token?: string,
3736
log?: Logger,
3837
delay = 50,
39-
token = GITHUB_TOKEN_SECRET,
40-
): Promise<FetchedPackage[]> => {
38+
): Promise<MaybeFetchedPackage[]> => {
4139
console.log(`urls`, urls);
42-
const packages: FetchedPackage[] = [];
40+
const packages: MaybeFetchedPackage[] = [];
4341
for (const url of urls) {
44-
const package_json = await load_package_json(url, log);
45-
if (!package_json) throw Error('failed to load package_json: ' + url);
46-
await wait(delay);
47-
const pkg = parse_package_meta(url, package_json);
48-
if (!pkg) throw Error('failed to parse package_json: ' + url);
49-
const pulls = await fetch_github_issues(url, pkg, log, token);
50-
if (!pulls) throw Error('failed to fetch issues: ' + url);
51-
await wait(delay);
52-
packages.push({url, package_json, pulls});
42+
try {
43+
const package_json = await load_package_json(url, log);
44+
if (!package_json) throw Error('failed to load package_json: ' + url);
45+
await wait(delay);
46+
const pkg = parse_package_meta(url, package_json);
47+
if (!pkg) throw Error('failed to parse package_json: ' + url);
48+
const pulls = await fetch_github_issues(url, pkg, log, token);
49+
if (!pulls) throw Error('failed to fetch issues: ' + url);
50+
await wait(delay);
51+
packages.push({url, package_json, pulls});
52+
} catch (err) {
53+
packages.push({url, package_json: null, pulls: null});
54+
log?.error(err);
55+
}
5356
}
5457
return packages;
5558
};

src/lib/packages.json.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
declare module '$lib/packages.json' {
2-
import type {FetchedPackage} from '$lib/fetch_packages.js';
3-
const data: FetchedPackage[];
2+
import type {MaybeFetchedPackage} from '$lib/fetch_packages.js';
3+
const data: MaybeFetchedPackage[];
44
export default data;
55
}

src/lib/packages.task.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import {format_file} from '@grogarden/gro/format_file.js';
66
import {exists} from '@grogarden/gro/exists.js';
77
import {join} from 'node:path';
88
import {paths} from '@grogarden/gro/paths.js';
9+
import {GITHUB_TOKEN_SECRET} from '$env/static/private';
910

10-
import {fetch_packages, type FetchedPackage} from '$lib/fetch_packages.js';
11+
import {fetch_packages, type MaybeFetchedPackage} from '$lib/fetch_packages.js';
1112
import {load_orc_config} from '$lib/config.js';
1213

1314
// TODO etags - cache?
@@ -40,13 +41,16 @@ export const task: Task<Args> = {
4041
const orc_config = await load_orc_config(dir);
4142
const {packages} = orc_config;
4243

43-
const fetched_packages = await fetch_packages(packages, log);
44+
const fetched_packages = await fetch_packages(packages, GITHUB_TOKEN_SECRET, log);
4445

4546
const local_package_json = await load_package_json(dir);
4647

47-
const final_packages: FetchedPackage[] = local_package_json?.homepage
48+
const final_packages: MaybeFetchedPackage[] = local_package_json?.homepage
4849
? [
49-
{url: local_package_json.homepage, package_json: local_package_json} as FetchedPackage,
50+
{
51+
url: local_package_json.homepage,
52+
package_json: local_package_json,
53+
} as MaybeFetchedPackage,
5054
].concat(fetched_packages)
5155
: fetched_packages;
5256

0 commit comments

Comments
 (0)