Skip to content

Commit

Permalink
Attempt to handle URL open() errors more gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
dhleong committed Apr 15, 2021
1 parent a0340a7 commit cd96fcd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
9 changes: 5 additions & 4 deletions src/credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
ICredentialStorage,
} from "./credentials/model";
import { RejectingCredentialRequester } from "./credentials/rejecting-requester";
import { DeviceStatus, IDiscoveredDevice } from "./discovery/model";
import { /* DeviceStatus, */ IDiscoveredDevice } from "./discovery/model";

export class CredentialsError extends Error {
}
Expand All @@ -19,9 +19,10 @@ export class CredentialManager {
const existing = await this.storage.read(device.id);
if (existing) return existing;

if (device.status !== DeviceStatus.AWAKE) {
throw new CredentialsError(`Device ${device.name} must be awake for initial registration`);
}
// if (device.status !== DeviceStatus.AWAKE) {
// throw new CredentialsError(`Device ${device.name} must
// be awake for initial registration`);
// }

const fromRequest = await this.requester.requestForDevice(device);
await this.storage.write(device.id, fromRequest);
Expand Down
10 changes: 9 additions & 1 deletion src/credentials/oauth/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class CliOauthStrategy implements OauthStrategy {
await this.io.prompt("Hit ENTER to continue");

try {
await open(url, { wait: true });
await this.openSafely(url);
} catch (e) {
this.io.logInfo("Unable to open the browser for you. This is fine; please manually open the following URL:");
this.io.logInfo(` ${url}`);
Expand All @@ -34,4 +34,12 @@ export class CliOauthStrategy implements OauthStrategy {
private printLoginInstructions() {
this.io.logInfo("When the page shows \"redirect\", copy the URL from your browser's address bar and paste it here.");
}

private async openSafely(url: string) {
const proc = await open(url);
return new Promise<void>((resolve, reject) => {
proc.on("error", reject);
proc.on("exit", resolve);
});
}
}

0 comments on commit cd96fcd

Please sign in to comment.