Skip to content

Commit 4e815ea

Browse files
committed
the changes
1 parent 576e144 commit 4e815ea

File tree

3 files changed

+69
-27
lines changed

3 files changed

+69
-27
lines changed

src/components/crash-message/crash-message.jsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {FormattedMessage} from 'react-intl';
55

66
import styles from './crash-message.css';
77
import reloadIcon from './reload.svg';
8+
import { downloadLogs } from '../../lib/pm-log-capture.js';
89

910
const CrashMessage = props => (
1011
<div className={styles.crashWrapper}>
@@ -25,7 +26,7 @@ const CrashMessage = props => (
2526
defaultMessage={'We are so sorry, but it looks like the page has crashed.' +
2627
' Please refresh your page to try' +
2728
' again.' +
28-
' If the problem persists, please report this error to our Discord.'}
29+
' If the problem persists, please report the downloadable error below to our Discord.'}
2930
description="Message to inform the user that page has crashed."
3031
id="tw.gui.crashMessage.description"
3132
/>
@@ -57,6 +58,12 @@ const CrashMessage = props => (
5758
id="gui.crashMessage.reload"
5859
/>
5960
</button>
61+
<button
62+
className={styles.reloadButton}
63+
onClick={downloadLogs}
64+
>
65+
{'Download Error'}
66+
</button>
6067
</Box>
6168
</div>
6269
);

src/components/menu-bar/menu-bar.jsx

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ import sharedMessages from '../../lib/shared-messages';
8585
import SeeInsideButton from './tw-see-inside.jsx';
8686
import { notScratchDesktop } from '../../lib/isScratchDesktop.js';
8787

88-
//import { consoleLogs } from '../../lib/pm-log-capture.js';
88+
import { downloadLogs } from '../../lib/pm-log-capture.js';
8989

9090
const ariaMessages = defineMessages({
9191
language: {
@@ -207,6 +207,7 @@ class MenuBar extends React.Component {
207207
'handleClickPackager',
208208
'handleClickRestorePoints',
209209
'handleClickSeeCommunity',
210+
'handleClickDownloadLogs',
210211
'handleClickShare',
211212
'handleKeyPress',
212213
'handleLanguageMouseUp',
@@ -422,21 +423,7 @@ class MenuBar extends React.Component {
422423
this.props.onRequestCloseAbout();
423424
};
424425
}
425-
/*
426-
- hidden until this is actually helpful for developers
427-
- unhide when a solution is found for not blocking error tracking/using 3rd parties
428-
handleClickDownloadLogs() {
429-
const str = JSON.stringify(consoleLogs);
430-
const a = document.createElement('a');
431-
a.style.display = 'none';
432-
document.body.append(a);
433-
const url = window.URL.createObjectURL(new Blob([str]));
434-
a.href = url;
435-
a.download = 'pm-log-trace.json';
436-
a.click();
437-
window.URL.revokeObjectURL(url);
438-
a.remove();
439-
}*/
426+
handleClickDownloadLogs() { downloadLogs(); }
440427
render() {
441428
const saveNowMessage = (
442429
<FormattedMessage
@@ -860,6 +847,9 @@ class MenuBar extends React.Component {
860847
id="pm.menuBar.moreSettings"
861848
/>
862849
</MenuItem>
850+
<MenuItem onClick={this.handleClickDownloadLogs}>
851+
Download Logs
852+
</MenuItem>
863853
</MenuSection>
864854
</MenuBarMenu>
865855
</div>

src/lib/pm-log-capture.js

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -142,18 +142,63 @@ const parseStack = (stack, url, line, column) => {
142142
if (stack.split('\n', 2)[0].includes('@')) return _parseFirefoxStack(stack);
143143
return _parseChromeStack(stack);
144144
};
145+
const downloadLogs = async () => {
146+
const str = JSON.stringify(consoleLogs);
147+
let blob = new Blob([str]);
148+
let filename = 'pm-error-download.json';
149+
// if we can, include the project
150+
if (vm) {
151+
filename = 'pm-error-download.pmp';
152+
const archive = vm._saveProjectZip();
153+
archive.file('logs.json', blob);
154+
blob = await archive.generateAsync({
155+
type: 'blob',
156+
mimeType: 'application/x.scratch.sb3',
157+
compression: 'DEFLATE'
158+
});
159+
}
160+
const a = document.createElement('a');
161+
a.style.display = 'none';
162+
document.body.append(a);
163+
const url = window.URL.createObjectURL(blob);
164+
a.href = url;
165+
a.download = filename;
166+
a.click();
167+
window.URL.revokeObjectURL(url);
168+
a.remove();
169+
};
170+
window.downloadLogs = downloadLogs;
145171

146172
window.addEventListener('error', e =>
147173
push('error', e.message, parseStack(e.error.stack, e.filename, e.lineno, e.colno)));
148174
window.addEventListener('unhandledrejection', e => push('promiseError', e.reason, []));
149-
for (const name of ['log', 'warn', 'error', 'debug', 'info']) {
150-
const item = window.console[name];
151-
window.console[name] = (...args) => {
152-
let stack = [];
153-
if (browserHasStack) stack = parseStack(new Error().stack);
154-
push(name, args, stack);
155-
item(...args);
156-
};
175+
class StackTrace extends Error {
176+
constructor() {
177+
super('');
178+
if (this.stack.split('\n', 2)[0].includes('@'))
179+
this.stack = this.stack
180+
.split('\n')
181+
.slice(2, 3)
182+
.join('\n');
183+
else {
184+
// chrome is weird ngl
185+
const lines = this.stack
186+
.split('\n')
187+
.slice(0, 3);
188+
lines.splice(1, 2);
189+
this.stack = lines.join('\n');
190+
}
191+
}
157192
}
158-
159-
export { consoleLogs, parseStack, push };
193+
if (!String(window.location.href).startsWith(`http://localhost:`)) {
194+
for (const name of ['log', 'warn', 'error', 'debug', 'info']) {
195+
const item = window.console[name];
196+
window.console[name] = (...args) => {
197+
let stack = [];
198+
if (browserHasStack) stack = parseStack(new Error().stack);
199+
push(name, args, stack);
200+
item.call(console, ...args, new StackTrace());
201+
};
202+
}
203+
}
204+
export { consoleLogs, parseStack, push, downloadLogs };

0 commit comments

Comments
 (0)