Skip to content

Commit d721d6e

Browse files
committed
Check status code when fetching library info from PyPI
1 parent b8e7481 commit d721d6e

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "python-dependencies-vscode",
33
"displayName": "Python Dependencies",
44
"description": "Utilities for managing Python dependencies",
5-
"version": "0.0.16",
5+
"version": "0.0.17",
66
"publisher": "patrick91",
77
"engines": {
88
"vscode": "^1.53.0"

src/core/api.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const getCacheKey = (library: string) => `${CACHE_PREFIX}-${library}`;
88

99
export const getInfo = async (
1010
library: string,
11-
): Promise<{ version: string; summary: string }> => {
11+
): Promise<{ version: string; summary: string } | null> => {
1212
const cache = getCache();
1313
const key = getCacheKey(library);
1414

@@ -27,12 +27,16 @@ export const getInfo = async (
2727

2828
const data = await fetchLibrary(library);
2929

30-
info = {
31-
version: data.info.version as string,
32-
summary: data.info.summary as string,
33-
};
30+
if (data) {
31+
info = {
32+
version: data.info.version as string,
33+
summary: data.info.summary as string,
34+
};
3435

35-
await cache.put(key, JSON.stringify({ info, date: new Date() }));
36+
await cache.put(key, JSON.stringify({ info, date: new Date() }));
3637

37-
return info;
38+
return info;
39+
}
40+
41+
return null;
3842
};

src/core/listener.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ const updateVersions = (
1717
deps.map(async (dep) => {
1818
const info = await getInfo(dep.name);
1919

20+
if (!info) {
21+
return;
22+
}
23+
2024
dep.version.installed = installedVersions[dep.name];
2125
dep.version.latest = info.version;
2226
dep.summary = info.summary;

src/core/pypi.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import fetch from "node-fetch";
22

33
export const fetchLibrary = async (library: string) => {
4-
const x = await fetch(`https://pypi.org/pypi/${library}/json`);
4+
const response = await fetch(`https://pypi.org/pypi/${library}/json`);
55

6-
return await x.json();
6+
if (response.status === 200) {
7+
return await response.json();
8+
}
9+
10+
console.log(`unable to fetch library ${library}`);
11+
12+
return null;
713
};

0 commit comments

Comments
 (0)