Skip to content

Commit

Permalink
fix: broken request error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
broofa committed Dec 10, 2021
1 parent 17939c9 commit e8dc095
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
17 changes: 8 additions & 9 deletions js/Store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import doesSatisfySemver from 'semver/functions/satisfies';
import isSemverValid from 'semver/functions/valid';
import Module, { moduleKey } from './Module';
import { ModuleInfo } from './types';
import { fetchJSON, LoadActivity, report } from './util';
import { fetchJSON, HttpError, LoadActivity, report } from './util';

class Store {
activity: LoadActivity;
Expand Down Expand Up @@ -104,10 +104,7 @@ class Store {
);
req = this.requestCache[reqPath] = fetchJSON<ModuleInfo>(
`https://registry.npmjs.cf/${reqPath}`
)
// Errors get turned into stub modules, below
.catch(err => err)
.finally(finish);
).finally(finish);
}

let body: ModuleInfo;
Expand All @@ -118,12 +115,14 @@ class Store {
failure = err;
}

if (!body) {
if (failure) {
if (failure instanceof HttpError) {
failure.message = `Error getting module from NPM (status: ${failure.code})`;
}
} else if (!body) {
failure = Error('No info provided by NPM repo');
} else if (typeof body != 'object') {
failure = Error(
'Data provided by NPM repo is not in the expected format'
);
failure = Error('Data not in expected format');
} else if (body.unpublished) {
failure = Error('Module is unpublished');
} else if (body.versions) {
Expand Down
4 changes: 3 additions & 1 deletion js/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,9 @@ export class LoadActivity {
*/
export function fetchJSON<T>(...args: [string, RequestInit?]): Promise<T> {
const p = window.fetch(...args).then(res => {
if (!res.ok) throw new HttpError(res.status);
if (!res.ok) {
return Promise.reject(new HttpError(res.status));
}
return res.json();
});

Expand Down

0 comments on commit e8dc095

Please sign in to comment.