Skip to content

Commit 6aa3ba6

Browse files
authored
refactor(browser): refactor chrome-paths error (#541)
1 parent 166bbdc commit 6aa3ba6

File tree

2 files changed

+23
-53
lines changed

2 files changed

+23
-53
lines changed

packages/agent-infra/browser/src/browser-finder/chrome-paths.ts

+19-53
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ const chromePaths = {
106106
},
107107
};
108108

109-
function getChromePath(): string {
109+
function getChromePath() {
110110
const chrome = chromePaths.chrome;
111111

112112
if (platform && Object.keys(chrome).includes(platform)) {
@@ -115,10 +115,9 @@ function getChromePath(): string {
115115
return pth;
116116
}
117117
}
118-
throwInvalidPlatformError('Chrome Stable');
119118
}
120119

121-
function getChromeBetaPath(): string {
120+
function getChromeBetaPath() {
122121
const beta = chromePaths.beta;
123122

124123
if (platform && Object.keys(beta).includes(platform)) {
@@ -127,10 +126,9 @@ function getChromeBetaPath(): string {
127126
return pth;
128127
}
129128
}
130-
throwInvalidPlatformError('Chrome Beta');
131129
}
132130

133-
function getChromeDevPath(): string {
131+
function getChromeDevPath() {
134132
const dev = chromePaths.dev;
135133

136134
if (platform && Object.keys(dev).includes(platform)) {
@@ -139,10 +137,9 @@ function getChromeDevPath(): string {
139137
return pth;
140138
}
141139
}
142-
throwInvalidPlatformError('Chrome Dev');
143140
}
144141

145-
function getChromeCanaryPath(): string {
142+
function getChromeCanaryPath() {
146143
const canary = chromePaths.canary;
147144

148145
if (platform && Object.keys(canary).includes(platform)) {
@@ -151,61 +148,30 @@ function getChromeCanaryPath(): string {
151148
return pth;
152149
}
153150
}
154-
throwInvalidPlatformError('Chrome Canary');
155151
}
156152

157153
export function getAnyChromeStable(): string {
158-
try {
159-
return getChromePath();
160-
} catch (e) {
161-
throwIfNotChromePathIssue(e);
154+
const chrome = getChromePath();
155+
if (chrome) {
156+
return chrome;
162157
}
163158

164-
try {
165-
return getChromeBetaPath();
166-
} catch (e) {
167-
throwIfNotChromePathIssue(e);
168-
}
169-
170-
try {
171-
return getChromeDevPath();
172-
} catch (e) {
173-
throwIfNotChromePathIssue(e);
159+
const beta = getChromeBetaPath();
160+
if (beta) {
161+
return beta;
174162
}
175163

176-
try {
177-
return getChromeCanaryPath();
178-
} catch (e) {
179-
throwIfNotChromePathIssue(e);
164+
const dev = getChromeDevPath();
165+
if (dev) {
166+
return dev;
180167
}
181168

182-
throw {
183-
name: 'chrome-paths',
184-
message: `Unable to find any google-chrome-browser.`,
185-
};
186-
}
187-
188-
function throwInvalidPlatformError(
189-
additionalInfo: string = '',
190-
otherDetails?: any,
191-
): never {
192-
throw {
193-
name: 'chrome-paths',
194-
message: `Couldn't find the chrome browser. ${additionalInfo}`,
195-
additionalInfo,
196-
otherDetails,
197-
};
198-
}
199-
200-
function throwIfNotChromePathIssue(obj: any) {
201-
if (
202-
Object.prototype.toString.call(obj) === '[object Object]' &&
203-
obj &&
204-
obj.name &&
205-
obj.name === 'chrome-paths'
206-
) {
207-
return;
169+
const canary = getChromeCanaryPath();
170+
if (canary) {
171+
return canary;
208172
}
209173

210-
throw obj;
174+
const error = new Error('Unable to find any google-chrome-browser.');
175+
error.name = 'ChromePathsError';
176+
throw error;
211177
}

packages/ui-tars/operators/browser-operator/src/browser-operator.ts

+4
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,10 @@ export class DefaultBrowserOperator extends BrowserOperator {
513513
*/
514514
public static hasBrowser(): boolean {
515515
try {
516+
if (this.browserPath) {
517+
return true;
518+
}
519+
516520
if (!this.logger) {
517521
this.logger = new ConsoleLogger('[DefaultBrowserOperator]');
518522
}

0 commit comments

Comments
 (0)