util.ts (2078B)
1 /** 2 * @license 3 * Copyright 2023 Google Inc. 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7 import type * as Bidi from 'chromium-bidi/lib/cjs/protocol/protocol.js'; 8 9 import {ProtocolError, TimeoutError} from '../common/Errors.js'; 10 import {PuppeteerURL} from '../common/util.js'; 11 12 import {BidiDeserializer} from './Deserializer.js'; 13 14 /** 15 * @internal 16 */ 17 export function createEvaluationError( 18 details: Bidi.Script.ExceptionDetails, 19 ): unknown { 20 if (details.exception.type !== 'error') { 21 return BidiDeserializer.deserialize(details.exception); 22 } 23 const [name = '', ...parts] = details.text.split(': '); 24 const message = parts.join(': '); 25 const error = new Error(message); 26 error.name = name; 27 28 // The first line is this function which we ignore. 29 const stackLines = []; 30 if (details.stackTrace && stackLines.length < Error.stackTraceLimit) { 31 for (const frame of details.stackTrace.callFrames.reverse()) { 32 if ( 33 PuppeteerURL.isPuppeteerURL(frame.url) && 34 frame.url !== PuppeteerURL.INTERNAL_URL 35 ) { 36 const url = PuppeteerURL.parse(frame.url); 37 stackLines.unshift( 38 ` at ${frame.functionName || url.functionName} (${ 39 url.functionName 40 } at ${url.siteString}, <anonymous>:${frame.lineNumber}:${ 41 frame.columnNumber 42 })`, 43 ); 44 } else { 45 stackLines.push( 46 ` at ${frame.functionName || '<anonymous>'} (${frame.url}:${ 47 frame.lineNumber 48 }:${frame.columnNumber})`, 49 ); 50 } 51 if (stackLines.length >= Error.stackTraceLimit) { 52 break; 53 } 54 } 55 } 56 57 error.stack = [details.text, ...stackLines].join('\n'); 58 return error; 59 } 60 61 /** 62 * @internal 63 */ 64 export function rewriteNavigationError( 65 message: string, 66 ms: number, 67 ): (error: unknown) => never { 68 return error => { 69 if (error instanceof ProtocolError) { 70 error.message += ` at ${message}`; 71 } else if (error instanceof TimeoutError) { 72 error.message = `Navigation timeout of ${ms} ms exceeded`; 73 } 74 throw error; 75 }; 76 }