|
126 | 126 | // This will use sourcemaps and normalize the stack frames |
127 | 127 | // eslint-disable-next-line no-undef |
128 | 128 | return StackTrace.fromError(err).then(function(stack) { |
129 | | - payload.message = err.toString(); |
| 129 | + var lines = [err.toString()]; |
| 130 | + // Reconstruct to a JS stackframe as expected by Error Reporting parsers. |
130 | 131 | for (var s = firstFrameIndex; s < stack.length; s++) { |
131 | | - payload.message += '\n'; |
132 | | - // Reconstruct the stackframe to a JS stackframe as expected by Error Reporting parsers. |
133 | | - // stack[s].source should not be used because not populated when created from source map. |
134 | | - // |
135 | | - // If functionName or methodName isn't available <anonymous> will be used as the name. |
136 | | - payload.message += [' at ', stack[s].getFunctionName() || '<anonymous>', ' (', stack[s].getFileName(), ':', stack[s].getLineNumber(), ':', stack[s].getColumnNumber(), ')'].join(''); |
| 132 | + // Cannot use stack[s].source as it is not populated from source maps. |
| 133 | + lines.push([ |
| 134 | + ' at ', |
| 135 | + // If a function name is not available '<anonymous>' will be used. |
| 136 | + stack[s].getFunctionName() || '<anonymous>', ' (', |
| 137 | + stack[s].getFileName(), ':', |
| 138 | + stack[s].getLineNumber(), ':', |
| 139 | + stack[s].getColumnNumber(), ')', |
| 140 | + ].join('')); |
137 | 141 | } |
138 | | - return that.sendErrorPayload(payload); |
| 142 | + return lines.join('\n'); |
139 | 143 | }, function(reason) { |
140 | 144 | // Failure to extract stacktrace |
141 | | - payload.message = [ |
| 145 | + return [ |
142 | 146 | 'Error extracting stack trace: ', reason, '\n', |
143 | 147 | err.toString(), '\n', |
144 | 148 | ' (', err.file, ':', err.line, ':', err.column, ')', |
145 | 149 | ].join(''); |
| 150 | + }).then(function(message) { |
| 151 | + payload.message = message; |
146 | 152 | return that.sendErrorPayload(payload); |
147 | 153 | }); |
148 | 154 | }; |
|
156 | 162 | xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8'); |
157 | 163 |
|
158 | 164 | return new Promise(function(resolve, reject) { |
159 | | - xhr.onloadend = resolve; |
160 | | - xhr.onerror = reject; |
| 165 | + xhr.onreadystatechange = function() { |
| 166 | + if (xhr.readyState === 4) { |
| 167 | + var code = xhr.status; |
| 168 | + if (code >= 200 && code < 300) { |
| 169 | + resolve({message: payload.message}); |
| 170 | + } else { |
| 171 | + var condition = code ? code + ' http response' : 'network error'; |
| 172 | + reject(new Error(condition + ' on stackdriver report')); |
| 173 | + } |
| 174 | + } |
| 175 | + }; |
161 | 176 | xhr.send(JSON.stringify(payload)); |
162 | 177 | }); |
163 | 178 | }; |
|
0 commit comments