-
Notifications
You must be signed in to change notification settings - Fork 964
Expand file tree
/
Copy patherrorwidget.ts
More file actions
94 lines (88 loc) · 2.71 KB
/
errorwidget.ts
File metadata and controls
94 lines (88 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import {
WidgetModel,
DOMWidgetModel,
DOMWidgetView,
WidgetView,
} from './widget';
import { JUPYTER_WIDGETS_VERSION } from './version';
import { BROKEN_FILE_SVG_ICON } from './utils';
// create a Widget Model that captures an error object
export function createErrorWidgetModel(
error: unknown,
msg?: string
): typeof WidgetModel {
class ErrorWidget extends DOMWidgetModel {
constructor(attributes: any, options: any) {
attributes = {
...attributes,
_view_name: 'ErrorWidgetView',
_view_module: '@jupyter-widgets/base',
_model_module_version: JUPYTER_WIDGETS_VERSION,
_view_module_version: JUPYTER_WIDGETS_VERSION,
msg: msg,
error: error,
};
super(attributes, options);
}
get comm_live(): boolean {
return true;
}
}
return ErrorWidget;
}
export class ErrorWidgetView extends DOMWidgetView {
generateErrorMessage(): { msg?: string; stack: string } {
return {
msg: this.model.get('msg'),
stack: String(this.model.get('error').stack),
};
}
render(): void {
const { msg, stack } = this.generateErrorMessage();
this.el.classList.add('jupyter-widgets');
const content = document.createElement('div');
content.classList.add('jupyter-widgets-error-widget', 'icon-error');
content.innerHTML = BROKEN_FILE_SVG_ICON;
const text = document.createElement('pre');
text.style.textAlign = 'center';
text.innerText = 'Click to show javascript error.';
content.append(text);
this.el.appendChild(content);
let width: number;
let height: number;
this.el.onclick = () => {
if (content.classList.contains('icon-error')) {
height = height || content.clientHeight;
width = width || content.clientWidth;
content.classList.remove('icon-error');
content.innerHTML = `
<pre>[Open Browser Console for more detailed log - Double click to close this message]\n${msg}\n${stack}</pre>
`;
content.style.height = `${height}px`;
content.style.width = `${width}px`;
content.classList.add('text-error');
}
};
this.el.ondblclick = () => {
if (content.classList.contains('text-error')) {
content.classList.remove('text-error');
content.innerHTML = BROKEN_FILE_SVG_ICON;
content.append(text);
content.classList.add('icon-error');
}
};
}
}
export function createErrorWidgetView(
error?: unknown,
msg?: string
): typeof WidgetView {
return class InnerErrorWidgetView extends ErrorWidgetView {
generateErrorMessage(): { msg?: string; stack: string } {
return {
msg,
stack: String(error instanceof Error ? error.stack : error),
};
}
};
}