-
Notifications
You must be signed in to change notification settings - Fork 526
Expand file tree
/
Copy pathJavaScriptModeErrorHandler.js
More file actions
127 lines (116 loc) · 3.89 KB
/
Copy pathJavaScriptModeErrorHandler.js
File metadata and controls
127 lines (116 loc) · 3.89 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/** @file Decorator and delegator for error, warning and info events in toolkits
* that use droplet and allow students to write and execute JavaScript. */
import annotationList from './acemode/annotationList';
import logToCloud from './logToCloud';
import {makeEnum} from './utils';
/** @enum {string} */
export const LogLevel = makeEnum('ERROR', 'WARNING');
/**
* Logging, annotation and error reporting mediator for use with toolkits where
* students write JavaScript.
* @param {function():JSInterpreter} getJsInterpreter
* @param {LogTarget} logTarget
* @constructor
*/
export default class JavaScriptModeErrorHandler {
constructor(getJsInterpreter, logTarget) {
this.getJsInterpreter_ = getJsInterpreter;
this.logTarget_ = logTarget;
}
/**
* Report an error. This will log to configured log targets (usually
* window.console and the onscreen console), set up a runtime annotation at the
* current user code line if we're currently running the interpreter, and
* will report the error up to New Relic.
* @param {string} errorString
* @param {number} [lineNumber]
*/
outputError(errorString, lineNumber, libraryName) {
if (errorString.msg) {
errorString = errorString.msg;
}
this.output_(errorString, LogLevel.ERROR, lineNumber, libraryName);
}
/**
* Report a warning. This will log to configured log targets (usually
* window.console and the onscreen console) and set up a runtime annotation at
* the current user code line if we're currently running the interpreter.
* @param {string} errorString
* @param {number} [lineNumber]
*/
outputWarning(errorString, lineNumber) {
if (errorString.msg) {
errorString = errorString.msg;
}
this.output_(errorString, LogLevel.WARNING, lineNumber);
}
/**
* Get a outputWarning() function that binds against the current line number, so we can
* report a later error at this line.
* @returns {function(string)}
*/
getAsyncOutputWarning() {
const lineNumber = this.getNearestUserCodeLine_();
return error => this.outputWarning(error, lineNumber);
}
/**
* @param {string} message
* @param {LogLevel} logLevel
* @param {number} [lineNumber] - if omitted, we'll try to infer a line number
* from the interpreter's current user code line. However for async
* calls we may want to pass this in to set a specific line number.
* @private
*/
output_(message, logLevel, lineNumber, libraryName) {
if (lineNumber === undefined) {
lineNumber = this.getNearestUserCodeLine_();
}
let logText = `${logLevel}: `;
if (lineNumber !== undefined) {
logText += `Line: ${lineNumber}: `;
}
logText += message;
if (!IN_UNIT_TEST) {
logText += '\n';
}
// Send the assembled output to our logging service.
this.logTarget_.log(logText, logLevel);
// Add an annotation directly in the editor for this output
if (lineNumber !== undefined && !libraryName) {
annotationList.addRuntimeAnnotation(logLevel, lineNumber, message);
}
// Send to New Relic if it's an error and meets our sampling rate
if (logLevel === LogLevel.ERROR) {
logToCloud.addPageAction(
logToCloud.PageAction.UserJavascriptError,
{
error: message,
},
1 / 20
);
}
}
/**
* @returns {number|undefined} The nearest user code line according to the
* active interpreter, or undefined if no interpreter can be found.
* @private
*/
getNearestUserCodeLine_() {
if (typeof this.getJsInterpreter_ === 'function') {
const interpreter = this.getJsInterpreter_();
if (interpreter) {
return 1 + interpreter.getNearestUserCodeLine();
}
}
return undefined;
}
}
/**
* An object that can consume strings and log them somewhere.
* @interface LogTarget
*/
/**
* @function
* @name LogTarget#log
* @param {string} logString
*/