Skip to content

Commit

Permalink
Report errors from backend when a request fails
Browse files Browse the repository at this point in the history
  • Loading branch information
confused-Techie committed Aug 29, 2024
1 parent 7a0c3fa commit 24e1221
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 80 deletions.
19 changes: 7 additions & 12 deletions src/develop.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,16 @@ cmd-shift-o to run the package out of the newly cloned repository.\
};
return request.get(requestSettings, (error, response, body) => {
body ??= {};
if (error != null) {
return void reject(`Request for package information failed: ${error.message}`);
if (error != null || response.statusCode !== 200) {
return void reject(`Request for package information failed: ${request.getErrorMessage(body, error)}`);
}

if (response.statusCode === 200) {
const repositoryUrl = body.repository.url;
if (repositoryUrl) {
return void resolve(repositoryUrl);
}

const repositoryUrl = body.repository.url;
if (repositoryUrl) {
return void resolve(repositoryUrl);
} else {
return void reject(`No repository URL found for package: ${packageName}`);
}

const message = request.getErrorMessage(body, error);
return void reject(`Request for package information failed: ${message}`);
});
});
}
Expand Down Expand Up @@ -116,7 +111,7 @@ cmd-shift-o to run the package out of the newly cloned repository.\

try {
const repoUrl = await this.getRepositoryUrl(packageName);

await this.cloneRepository(repoUrl, packageDirectory, options);
await this.installDependencies(packageDirectory, options);
await this.linkPackage(packageDirectory, options);
Expand Down
12 changes: 3 additions & 9 deletions src/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,16 +217,10 @@ Run ppm -v after installing Git to see what version has been detected.\
};
return new Promise((resolve, reject) => {
request.get(requestSettings, (error, response, body) => {
let message;
body ??= {};
if (error != null) {
message = `Request for package information failed: ${error.message}`;
if (error.status) { message += ` (${error.status})`; }
return void reject(message);
}
if (response.statusCode !== 200) {
message = request.getErrorMessage(body, error);
return void reject(`Request for package information failed: ${message}`);
if (error != null || response.statusCode !== 200) {
const message = request.getErrorMessage(body, error);
return void reject(`Request for package information failed: '${message}'`);
}
if (!body.releases.latest) {
return void reject(`No releases available for ${packageName}`);
Expand Down
20 changes: 7 additions & 13 deletions src/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,10 @@ have published it.\
return new Promise((resolve, reject) => {
request.get(requestSettings, (error, response, body) => {
body ??= {};
if (error != null) {
return void reject(error);
if (error != null || response.statusCode !== 200) {
return void reject(request.getErrorMessage(body, error));
}
resolve(response.statusCode === 200);
resolve(true);
});
});
}
Expand Down Expand Up @@ -200,13 +200,10 @@ have published it.\
return new Promise((resolve, reject) => {
request.post(requestSettings, (error, response, body) => {
body ??= {};
if (error != null) {
return void reject(error);
}
if (response.statusCode !== 201) {
if (error != null || response.statusCode !== 201) {
const message = request.getErrorMessage(body, error);
this.logFailure();
return void reject(`Registering package in ${repository} repository failed: ${message}`);
return void reject(`Registering package in ${repository} repository failed: '${message}'`);
}

this.logSuccess();
Expand Down Expand Up @@ -241,12 +238,9 @@ have published it.\
return new Promise((resolve, reject) => {
request.post(requestSettings, (error, response, body) => {
body ??= {};
if (error != null) {
return void reject(error);
}
if (response.statusCode !== 201) {
if (error != null || response.statusCode !== 201) {
const message = request.getErrorMessage(body, error);
return void reject(`Creating new version failed: ${message}`);
return void reject(`Creating new version failed: '${message}'`);
}

resolve();
Expand Down
12 changes: 9 additions & 3 deletions src/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function loadNpm() {
userconfig: config.getUserConfigPath(),
globalconfig: config.getGlobalConfigPath()
};
return new Promise((resolve, reject) =>
return new Promise((resolve, reject) =>
void npm.load(npmOptions, (error, value) => void(error != null ? reject(error) : resolve(value)))
);
};
Expand Down Expand Up @@ -66,7 +66,7 @@ module.exports = {
.ok((res) => OK_STATUS_CODES.includes(res.status));
return void callback(null, res, res.body);
} catch (error) {
return void callback(error, null, null);
return void callback(error, null, null);
}
});
},
Expand Down Expand Up @@ -141,7 +141,13 @@ module.exports = {
if (err?.status === 503) {
return `${err.response.req.host} is temporarily unavailable, please try again later.`;
} else {
return err?.response?.body ?? err?.response?.error ?? err ?? body.message ?? body.error ?? body;
let msg = err?.response?.body?.message ?? err?.response?.body ?? err?.response?.error ?? err ?? body.message ?? body.error ?? body;
if (typeof msg === "object") {
// If we found a message that's still an object, lets make sure it'll
// still show up for users
msg = JSON.stringify(msg);
}
return msg;
}
},

Expand Down
19 changes: 8 additions & 11 deletions src/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,15 @@ Search for packages/themes.\
return new Promise((resolve, reject) => {
request.get(requestSettings, function(error, response, body) {
body ??= {};
if (error != null) {
return void reject(error);
if (error != null || response.statusCode !== 200) {
const message = request.getErrorMessage(body, error);
return void reject(`Searching packages failed: '${message}'`);
}
if (response.statusCode === 200) {
let packages = body.filter(pack => (pack.releases != null ? pack.releases.latest : undefined) != null);
packages = packages.map(({readme, metadata, downloads, stargazers_count}) => _.extend({}, metadata, {readme, downloads, stargazers_count}));
packages = packages.filter(({name, version}) => !isDeprecatedPackage(name, version));
return void resolve(packages);
}

const message = request.getErrorMessage(body, error);
reject(`Searching packages failed: ${message}`);

let packages = body.filter(pack => (pack.releases != null ? pack.releases.latest : undefined) != null);
packages = packages.map(({readme, metadata, downloads, stargazers_count}) => _.extend({}, metadata, {readme, downloads, stargazers_count}));
packages = packages.filter(({name, version}) => !isDeprecatedPackage(name, version));
return void resolve(packages);
});
});
}
Expand Down
8 changes: 2 additions & 6 deletions src/star.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,14 @@ Run \`ppm stars\` to see all your starred packages.\
return new Promise((resolve, reject) => {
request.post(requestSettings, (error, response, body) => {
body ??= {};
if (error != null) {
this.logFailure();
return void reject(error);
}
if ((response.statusCode === 404) && ignoreUnpublishedPackages) {
process.stdout.write('skipped (not published)\n'.yellow);
return void reject();
}
if (response.statusCode !== 200) {
if (error != null || response.statusCode !== 200) {
this.logFailure();
const message = request.getErrorMessage(body, error);
return void reject(`Starring package failed: ${message}`);
return void reject(`Starring package failed: '${message}'`);
}

this.logSuccess();
Expand Down
10 changes: 3 additions & 7 deletions src/unpublish.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,10 @@ name is specified.\
return new Promise((resolve, reject) =>{
request.del(options, (error, response, body) => {
body ??= {};
if (error != null) {
if (error != null || response.statusCode !== 204) {
this.logFailure();
return void reject(error);
}
if (response.statusCode !== 204) {
this.logFailure();
const message = body.message ?? body.error ?? body;
return void reject(`Unpublishing failed: ${message}`);
const message = request.getErrorMessage(body, error);
return void reject(`Unpublishing failed: '${message}'`);
}

this.logSuccess();
Expand Down
8 changes: 2 additions & 6 deletions src/unstar.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,10 @@ Run \`ppm stars\` to see all your starred packages.\
return new Promise((resolve, reject) => {
request.del(requestSettings, (error, response, body) => {
body ??= {};
if (error != null) {
this.logFailure();
return void reject(error);
}
if (response.statusCode !== 204) {
if (error != null || response.statusCode !== 204) {
this.logFailure();
const message = request.getErrorMessage(body, error);
return void reject(`Unstarring package failed: ${message}`);
return void reject(`Unstarring package failed: '${message}'`);
}

this.logSuccess();
Expand Down
11 changes: 4 additions & 7 deletions src/upgrade.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,12 @@ available updates.\
return new Promise((resolve, reject) => {
request.get(requestSettings, (error, response, body) => {
body ??= {};
if (error != null) {
return void reject(`Request for package information failed: ${error.message}`);
}
if (response.statusCode === 404) {
if (response.statusCode === 404 && error == null) {
return void resolve();
}
if (response.statusCode !== 200) {
const message = body.message ?? body.error ?? body;
return void reject(`Request for package information failed: ${message}`);
if (error != null || response.statusCode !== 200) {
const message = request.getErrorMessage(body, error);
return void reject(`Request for package information failed: '${message}'`);
}

const atomVersion = this.installedAtomVersion;
Expand Down
9 changes: 3 additions & 6 deletions src/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,9 @@ View information about a package/theme.\
return new Promise((resolve, reject) => {
request.get(requestSettings, (error, response, body) => {
body ??= {};
if (error != null) {
return void reject(error);
}
if (response.statusCode !== 200) {
const message = body.message ?? body.error ?? body;
return void reject(`Requesting package failed: ${message}`);
if (error != null || response.statusCode !== 200) {
const message = request.getErrorMessage(body, error);
return void reject(`Requesting package failed: '${message}'`);
}

this.getLatestCompatibleVersion(body, options).then(version => {
Expand Down

0 comments on commit 24e1221

Please sign in to comment.