|
| 1 | +/* |
| 2 | +Copyright 2017 OpenMarket Ltd |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import q from "q"; |
| 18 | +import request from "browser-request"; |
| 19 | + |
| 20 | +import PlatformPeg from 'matrix-react-sdk/lib/PlatformPeg'; |
| 21 | + |
| 22 | +import rageshake from './rageshake' |
| 23 | + |
| 24 | +/** |
| 25 | + * Send a bug report. |
| 26 | + * @param {string} bugReportEndpoint HTTP url to send the report to |
| 27 | + * @param {string} userText Any additional user input. |
| 28 | + * @param {boolean} sendLogs True to send logs |
| 29 | + * @return {Promise} Resolved when the bug report is sent. |
| 30 | + */ |
| 31 | +export default async function sendBugReport(bugReportEndpoint, userText, sendLogs) { |
| 32 | + if (!bugReportEndpoint) { |
| 33 | + throw new Error("No bug report endpoint has been set."); |
| 34 | + } |
| 35 | + |
| 36 | + let version = "UNKNOWN"; |
| 37 | + try { |
| 38 | + version = await PlatformPeg.get().getAppVersion(); |
| 39 | + } |
| 40 | + catch (err) {} // PlatformPeg already logs this. |
| 41 | + |
| 42 | + let userAgent = "UNKNOWN"; |
| 43 | + if (window.navigator && window.navigator.userAgent) { |
| 44 | + userAgent = window.navigator.userAgent; |
| 45 | + } |
| 46 | + |
| 47 | + console.log("Sending bug report."); |
| 48 | + |
| 49 | + let logs = []; |
| 50 | + if (sendLogs) { |
| 51 | + logs = await rageshake.getLogsForReport(); |
| 52 | + } |
| 53 | + |
| 54 | + await q.Promise((resolve, reject) => { |
| 55 | + request({ |
| 56 | + method: "POST", |
| 57 | + url: bugReportEndpoint, |
| 58 | + body: { |
| 59 | + logs: logs, |
| 60 | + text: ( |
| 61 | + userText || "User did not supply any additional text." |
| 62 | + ), |
| 63 | + app: 'riot-web', |
| 64 | + version: version, |
| 65 | + user_agent: userAgent, |
| 66 | + }, |
| 67 | + json: true, |
| 68 | + timeout: 5 * 60 * 1000, |
| 69 | + }, (err, res) => { |
| 70 | + if (err) { |
| 71 | + reject(err); |
| 72 | + return; |
| 73 | + } |
| 74 | + if (res.status < 200 || res.status >= 400) { |
| 75 | + reject(new Error(`HTTP ${res.status}`)); |
| 76 | + return; |
| 77 | + } |
| 78 | + resolve(); |
| 79 | + }) |
| 80 | + }); |
| 81 | +} |
0 commit comments