Skip to content

Commit af4e58c

Browse files
committed
feat: log exceptions in webviews to main process
1 parent 2d15a10 commit af4e58c

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

app/main.dev.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,35 @@ configureStore(MAIN, isDevelopment) // eslint-disable-next-line promise/always-r
114114
const newView = (
115115
name: BrowserViewName,
116116
options: WebPreferences,
117-
initialUrl: string
117+
initialUrl: string,
118+
{ logConsoleErrors = true }: { logConsoleErrors?: boolean } = {}
118119
): WebContentsView => {
119120
if (mainWindow === undefined || mainWindow === null) {
120121
throw Error('Main window not defined!');
121122
}
122123

123124
const view = new WebContentsView({ webPreferences: options });
125+
if (logConsoleErrors) {
126+
view.webContents.on(
127+
'console-message',
128+
({ level, message, line, sourceId }: any) => {
129+
if (level === 'error') {
130+
console.error(
131+
`Forwarded from browser view (${name}): ${message}`,
132+
{
133+
browserView: name,
134+
line,
135+
sourceId
136+
}
137+
);
138+
}
139+
}
140+
);
141+
}
142+
143+
if (name === 'configuration' || name === 'print') {
144+
view.webContents.toggleDevTools();
145+
}
124146
mainWindow.contentView.addChildView(view);
125147
electronObjects.views[name] = view;
126148
store.dispatch(addView(name, initialUrl));
@@ -150,7 +172,8 @@ configureStore(MAIN, isDevelopment) // eslint-disable-next-line promise/always-r
150172
sandbox: true,
151173
contextIsolation: true
152174
},
153-
`file://${__dirname}/app.html#${ROUTES.PLACEHOLDER}`
175+
`file://${__dirname}/app.html#${ROUTES.PLACEHOLDER}`,
176+
{ logConsoleErrors: false }
154177
);
155178
puppetView.webContents.setUserAgent(getRandomUserAgent());
156179
newView(

0 commit comments

Comments
 (0)