Improved bug report error handling (#1018)

* Improve rageshake error handling.

* Add a max file size and exclude older files one the limit is hit.

* Zip log files as smaller chunks.
This commit is contained in:
Doug
2023-06-06 09:24:01 +01:00
committed by GitHub
parent 4e926c3334
commit ea4aa943e0
11 changed files with 279 additions and 100 deletions
+33 -7
View File
@@ -24,7 +24,7 @@ class BugReportServiceTests: XCTestCase {
override func setUpWithError() throws {
bugReportService = BugReportServiceMock()
bugReportService.underlyingCrashedLastRun = false
bugReportService.submitBugReportProgressListenerReturnValue = SubmitBugReportResponse(reportUrl: "https://www.example.com/123")
bugReportService.submitBugReportProgressListenerReturnValue = .success(SubmitBugReportResponse(reportUrl: "https://www.example.com/123"))
}
func testInitialStateWithMockService() {
@@ -39,8 +39,8 @@ class BugReportServiceTests: XCTestCase {
includeCrashLog: true,
githubLabels: [],
files: [])
let result = try await bugReportService.submitBugReport(bugReport, progressListener: nil)
XCTAssertFalse(result.reportUrl.isEmpty)
let response = try await bugReportService.submitBugReport(bugReport, progressListener: nil).get()
XCTAssertFalse(response.reportUrl.isEmpty)
}
func testInitialStateWithRealService() throws {
@@ -64,17 +64,43 @@ class BugReportServiceTests: XCTestCase {
includeCrashLog: true,
githubLabels: [],
files: [])
let result = try await service.submitBugReport(bugReport, progressListener: nil)
let response = try await service.submitBugReport(bugReport, progressListener: nil).get()
XCTAssertEqual(result.reportUrl, "https://example.com/123")
XCTAssertEqual(response.reportUrl, "https://example.com/123")
}
func testLogsMaxSize() {
// Given a new set of logs
var logs = BugReportService.Logs(maxFileSize: 1000)
XCTAssertEqual(logs.zippedSize, 0)
XCTAssertEqual(logs.originalSize, 0)
XCTAssertTrue(logs.files.isEmpty)
// When adding new files within the size limit
logs.appendFile(at: .homeDirectory, zippedSize: 250, originalSize: 1000)
logs.appendFile(at: .picturesDirectory, zippedSize: 500, originalSize: 2000)
// Then the logs should be included
XCTAssertEqual(logs.zippedSize, 750)
XCTAssertEqual(logs.originalSize, 3000)
XCTAssertEqual(logs.files, [.homeDirectory, .picturesDirectory])
// When adding a new file larger that will exceed the size limit
logs.appendFile(at: .homeDirectory, zippedSize: 500, originalSize: 2000)
// Then the files shouldn't be included.
XCTAssertEqual(logs.zippedSize, 750)
XCTAssertEqual(logs.originalSize, 3000)
XCTAssertEqual(logs.files, [.homeDirectory, .picturesDirectory])
}
}
private class MockURLProtocol: URLProtocol {
override func startLoading() {
let response = "{\"report_url\":\"https://example.com/123\"}"
if let data = response.data(using: .utf8) {
let urlResponse = URLResponse()
if let data = response.data(using: .utf8),
let url = request.url,
let urlResponse = HTTPURLResponse(url: url, statusCode: 200, httpVersion: nil, headerFields: nil) {
client?.urlProtocol(self, didReceive: urlResponse, cacheStoragePolicy: .allowedInMemoryOnly)
client?.urlProtocol(self, didLoad: data)
client?.urlProtocolDidFinishLoading(self)
@@ -64,7 +64,7 @@ class BugReportViewModelTests: XCTestCase {
let mockService = BugReportServiceMock()
mockService.submitBugReportProgressListenerClosure = { _, _ in
await Task.yield()
return SubmitBugReportResponse(reportUrl: "https://test.test")
return .success(SubmitBugReportResponse(reportUrl: "https://test.test"))
}
let viewModel = BugReportScreenViewModel(bugReportService: mockService,
userID: "@mock.client.com",
@@ -90,7 +90,7 @@ class BugReportViewModelTests: XCTestCase {
func testSendReportWithError() async throws {
let mockService = BugReportServiceMock()
mockService.submitBugReportProgressListenerClosure = { _, _ in
throw TestError.testError
.failure(.uploadFailure(TestError.testError))
}
let viewModel = BugReportScreenViewModel(bugReportService: mockService,
userID: "@mock.client.com",
+55
View File
@@ -161,6 +161,61 @@ class LoggingTests: XCTestCase {
XCTAssertTrue(logFile.lastPathComponent.contains(target))
}
func testLogFileSorting() async throws {
// Given a collection of log files.
XCTAssertTrue(MXLogger.logFiles.isEmpty)
// When creating new logs.
let logsFileDirectory = URL.appGroupContainerDirectory
for i in 1...5 {
let filename = "console.\(i).log"
try "console".write(to: logsFileDirectory.appending(path: filename), atomically: true, encoding: .utf8)
}
for i in 1...5 {
let nseFilename = "console-nse.\(i).log"
try "nse".write(to: logsFileDirectory.appending(path: nseFilename), atomically: true, encoding: .utf8)
}
// Then the logs should be sorted chronologically (newest first) and not alphabetically.
XCTAssertEqual(MXLogger.logFiles.map(\.lastPathComponent),
["console-nse.5.log",
"console-nse.4.log",
"console-nse.3.log",
"console-nse.2.log",
"console-nse.1.log",
"console.5.log",
"console.4.log",
"console.3.log",
"console.2.log",
"console.1.log"])
// When updating the oldest log file.
let currentLogFile = logsFileDirectory.appending(path: "console.1.log")
let fileHandle = try FileHandle(forWritingTo: currentLogFile)
try fileHandle.seekToEnd()
guard let newLineData = "newline".data(using: .utf8) else {
XCTFail("Couldn't create data to write to disk.")
return
}
try fileHandle.write(contentsOf: newLineData)
try fileHandle.close()
// Then that file should now be the first log file.
XCTAssertEqual(MXLogger.logFiles.map(\.lastPathComponent),
["console.1.log",
"console-nse.5.log",
"console-nse.4.log",
"console-nse.3.log",
"console-nse.2.log",
"console-nse.1.log",
"console.5.log",
"console.4.log",
"console.3.log",
"console.2.log"])
}
func testRoomSummaryContentIsRedacted() throws {
// Given a room summary that contains sensitive information
let roomName = "Private Conversation"