fix: use error-like (#8504)
This commit is contained in:
parent
bfd4e68f25
commit
4d359906a4
2
.gitignore
vendored
2
.gitignore
vendored
@ -14,7 +14,7 @@ test-ts-types/**/dist/
|
|||||||
package-lock.json
|
package-lock.json
|
||||||
yarn.lock
|
yarn.lock
|
||||||
/node6
|
/node6
|
||||||
/lib
|
lib
|
||||||
test/coverage.json
|
test/coverage.json
|
||||||
temp/
|
temp/
|
||||||
new-docs/
|
new-docs/
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { debugError } from '../common/helper.js';
|
import { debugError, isErrorLike } from '../common/helper.js';
|
||||||
import { isNode } from '../environment.js';
|
import { isNode } from '../environment.js';
|
||||||
import { assert } from './assert.js';
|
import { assert } from './assert.js';
|
||||||
import {
|
import {
|
||||||
@ -138,7 +138,7 @@ async function getWSEndpoint(browserURL: string): Promise<string> {
|
|||||||
const data = await result.json();
|
const data = await result.json();
|
||||||
return data.webSocketDebuggerUrl;
|
return data.webSocketDebuggerUrl;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof Error) {
|
if (isErrorLike(error)) {
|
||||||
error.message =
|
error.message =
|
||||||
`Failed to fetch browser webSocket URL from ${endpointURL}: ` +
|
`Failed to fetch browser webSocket URL from ${endpointURL}: ` +
|
||||||
error.message;
|
error.message;
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
import { EventEmitter } from './EventEmitter.js';
|
import { EventEmitter } from './EventEmitter.js';
|
||||||
import { assert } from './assert.js';
|
import { assert } from './assert.js';
|
||||||
import { helper, debugError } from './helper.js';
|
import { helper, debugError, isErrorLike } from './helper.js';
|
||||||
import { ExecutionContext, EVALUATION_SCRIPT_URL } from './ExecutionContext.js';
|
import { ExecutionContext, EVALUATION_SCRIPT_URL } from './ExecutionContext.js';
|
||||||
import {
|
import {
|
||||||
LifecycleWatcher,
|
LifecycleWatcher,
|
||||||
@ -163,7 +163,7 @@ export class FrameManager extends EventEmitter {
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
// The target might have been closed before the initialization finished.
|
// The target might have been closed before the initialization finished.
|
||||||
if (
|
if (
|
||||||
error instanceof Error &&
|
isErrorLike(error) &&
|
||||||
(error.message.includes('Target closed') ||
|
(error.message.includes('Target closed') ||
|
||||||
error.message.includes('Session closed'))
|
error.message.includes('Session closed'))
|
||||||
) {
|
) {
|
||||||
@ -226,7 +226,7 @@ export class FrameManager extends EventEmitter {
|
|||||||
? new Error(`${response.errorText} at ${url}`)
|
? new Error(`${response.errorText} at ${url}`)
|
||||||
: null;
|
: null;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof Error) {
|
if (isErrorLike(error)) {
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
throw error;
|
throw error;
|
||||||
|
@ -43,7 +43,7 @@ import {
|
|||||||
FrameManager,
|
FrameManager,
|
||||||
FrameManagerEmittedEvents,
|
FrameManagerEmittedEvents,
|
||||||
} from './FrameManager.js';
|
} from './FrameManager.js';
|
||||||
import { debugError, helper } from './helper.js';
|
import { debugError, helper, isErrorLike } from './helper.js';
|
||||||
import { HTTPRequest } from './HTTPRequest.js';
|
import { HTTPRequest } from './HTTPRequest.js';
|
||||||
import { HTTPResponse } from './HTTPResponse.js';
|
import { HTTPResponse } from './HTTPResponse.js';
|
||||||
import { Keyboard, Mouse, MouseButton, Touchscreen } from './Input.js';
|
import { Keyboard, Mouse, MouseButton, Touchscreen } from './Input.js';
|
||||||
@ -1578,7 +1578,7 @@ export class Page extends EventEmitter {
|
|||||||
const result = await pageBinding(...args);
|
const result = await pageBinding(...args);
|
||||||
expression = helper.pageBindingDeliverResultString(name, seq, result);
|
expression = helper.pageBindingDeliverResultString(name, seq, result);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof Error)
|
if (isErrorLike(error))
|
||||||
expression = helper.pageBindingDeliverErrorString(
|
expression = helper.pageBindingDeliverErrorString(
|
||||||
name,
|
name,
|
||||||
seq,
|
seq,
|
||||||
@ -2360,8 +2360,9 @@ export class Page extends EventEmitter {
|
|||||||
timezoneId: timezoneId || '',
|
timezoneId: timezoneId || '',
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof Error && error.message.includes('Invalid timezone'))
|
if (isErrorLike(error) && error.message.includes('Invalid timezone')) {
|
||||||
throw new Error(`Invalid timezone ID: ${timezoneId}`);
|
throw new Error(`Invalid timezone ID: ${timezoneId}`);
|
||||||
|
}
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
import { assert } from './assert.js';
|
import { assert } from './assert.js';
|
||||||
import { helper } from './helper.js';
|
import { helper, isErrorLike } from './helper.js';
|
||||||
import { CDPSession } from './Connection.js';
|
import { CDPSession } from './Connection.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -122,7 +122,7 @@ export class Tracing {
|
|||||||
const buffer = await helper.getReadableAsBuffer(readable, this._path);
|
const buffer = await helper.getReadableAsBuffer(readable, this._path);
|
||||||
resolve(buffer ?? undefined);
|
resolve(buffer ?? undefined);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof Error) {
|
if (isErrorLike(error)) {
|
||||||
reject(error);
|
reject(error);
|
||||||
} else {
|
} else {
|
||||||
reject(new Error(`Unknown error: ${error}`));
|
reject(new Error(`Unknown error: ${error}`));
|
||||||
|
@ -165,7 +165,9 @@ async function waitForEvent<T>(
|
|||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
if (result instanceof Error) throw result;
|
if (isErrorLike(result)) {
|
||||||
|
throw result;
|
||||||
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -378,6 +380,22 @@ async function getReadableFromProtocolStream(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface ErrorLike extends Error {
|
||||||
|
name: string;
|
||||||
|
message: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isErrorLike(obj: unknown): obj is ErrorLike {
|
||||||
|
return obj instanceof Object && 'name' in obj && 'message' in obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isErrnoException(obj: unknown): obj is NodeJS.ErrnoException {
|
||||||
|
return (
|
||||||
|
isErrorLike(obj) &&
|
||||||
|
('errno' in obj || 'code' in obj || 'path' in obj || 'syscall' in obj)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
export const helper = {
|
export const helper = {
|
||||||
evaluationString,
|
evaluationString,
|
||||||
pageBindingInitString,
|
pageBindingInitString,
|
||||||
|
@ -28,6 +28,8 @@ import {
|
|||||||
helper,
|
helper,
|
||||||
debugError,
|
debugError,
|
||||||
PuppeteerEventListener,
|
PuppeteerEventListener,
|
||||||
|
isErrorLike,
|
||||||
|
isErrnoException,
|
||||||
} from '../common/helper.js';
|
} from '../common/helper.js';
|
||||||
import { LaunchOptions } from './LaunchOptions.js';
|
import { LaunchOptions } from './LaunchOptions.js';
|
||||||
import { Connection } from '../common/Connection.js';
|
import { Connection } from '../common/Connection.js';
|
||||||
@ -216,7 +218,7 @@ export class BrowserRunner {
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`${PROCESS_ERROR_EXPLANATION}\nError cause: ${
|
`${PROCESS_ERROR_EXPLANATION}\nError cause: ${
|
||||||
error instanceof Error ? error.stack : error
|
isErrorLike(error) ? error.stack : error
|
||||||
}`
|
}`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -330,9 +332,8 @@ function pidExists(pid: number): boolean {
|
|||||||
try {
|
try {
|
||||||
return process.kill(pid, 0);
|
return process.kill(pid, 0);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof Error) {
|
if (isErrnoException(error)) {
|
||||||
const err = error as NodeJS.ErrnoException;
|
if (error.code && error.code === 'ESRCH') {
|
||||||
if (err.code && err.code === 'ESRCH') {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,24 +14,24 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { TestServer } from '../utils/testserver/index.js';
|
import Protocol from 'devtools-protocol';
|
||||||
import * as path from 'path';
|
import expect from 'expect';
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import * as os from 'os';
|
import * as os from 'os';
|
||||||
|
import * as path from 'path';
|
||||||
|
import rimraf from 'rimraf';
|
||||||
import sinon from 'sinon';
|
import sinon from 'sinon';
|
||||||
import puppeteer from '../lib/cjs/puppeteer/puppeteer.js';
|
|
||||||
import {
|
import {
|
||||||
Browser,
|
Browser,
|
||||||
BrowserContext,
|
BrowserContext,
|
||||||
} from '../lib/cjs/puppeteer/common/Browser.js';
|
} from '../lib/cjs/puppeteer/common/Browser.js';
|
||||||
|
import { isErrorLike } from '../lib/cjs/puppeteer/common/helper.js';
|
||||||
import { Page } from '../lib/cjs/puppeteer/common/Page.js';
|
import { Page } from '../lib/cjs/puppeteer/common/Page.js';
|
||||||
import { PuppeteerNode } from '../lib/cjs/puppeteer/node/Puppeteer.js';
|
import { PuppeteerNode } from '../lib/cjs/puppeteer/node/Puppeteer.js';
|
||||||
import utils from './utils.js';
|
import puppeteer from '../lib/cjs/puppeteer/puppeteer.js';
|
||||||
import rimraf from 'rimraf';
|
import { TestServer } from '../utils/testserver/index.js';
|
||||||
import expect from 'expect';
|
|
||||||
|
|
||||||
import { trackCoverage } from './coverage-utils.js';
|
import { trackCoverage } from './coverage-utils.js';
|
||||||
import Protocol from 'devtools-protocol';
|
import utils from './utils.js';
|
||||||
|
|
||||||
const setupServer = async () => {
|
const setupServer = async () => {
|
||||||
const assetsPath = path.join(__dirname, 'assets');
|
const assetsPath = path.join(__dirname, 'assets');
|
||||||
@ -73,7 +73,7 @@ let extraLaunchOptions = {};
|
|||||||
try {
|
try {
|
||||||
extraLaunchOptions = JSON.parse(process.env['EXTRA_LAUNCH_OPTIONS'] || '{}');
|
extraLaunchOptions = JSON.parse(process.env['EXTRA_LAUNCH_OPTIONS'] || '{}');
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof Error) {
|
if (isErrorLike(error)) {
|
||||||
console.warn(
|
console.warn(
|
||||||
`Error parsing EXTRA_LAUNCH_OPTIONS: ${error.message}. Skipping.`
|
`Error parsing EXTRA_LAUNCH_OPTIONS: ${error.message}. Skipping.`
|
||||||
);
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user