Skip to content

Commit ca75e78

Browse files
committed
[PlaygroundLogger] Updated the places where errors are caught to appropriately propagate error reasons.
This means that users will see some maybe-actionable information in the event of an error instead of a generic error message.
1 parent 2e7510a commit ca75e78

File tree

4 files changed

+84
-8
lines changed

4 files changed

+84
-8
lines changed

PlaygroundLogger/PlaygroundLogger/LegacySupport/LegacyEntrypoints.swift

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,23 @@ public func legacyLog<T>(instance: T, name: String, id: Int, startLine: Int, end
4242
do {
4343
data = try packet.encode()
4444
}
45+
catch LoggingError.failedToGenerateOpaqueRepresentation {
46+
fatalError("Failures to generate opaque representations should not occur during encoding")
47+
}
48+
catch let LoggingError.encodingFailure(reason) {
49+
let errorPacket = LogPacket(errorWithReason: reason, startLine: startLine, endLine: endLine, startColumn: startColumn, endColumn: endColumn, threadID: packet.threadID)
50+
51+
// Encoding an error packet should not fail under any circumstances.
52+
data = try! errorPacket.encode()
53+
}
54+
catch let LoggingError.otherFailure(reason) {
55+
let errorPacket = LogPacket(errorWithReason: reason, startLine: startLine, endLine: endLine, startColumn: startColumn, endColumn: endColumn, threadID: packet.threadID)
56+
57+
// Encoding an error packet should not fail under any circumstances.
58+
data = try! errorPacket.encode()
59+
}
4560
catch {
46-
let errorPacket = LogPacket(errorWithReason: "Error occurred while encoding log packet", startLine: startLine, endLine: endLine, startColumn: startColumn, endColumn: endColumn, threadID: packet.threadID)
61+
let errorPacket = LogPacket(errorWithReason: "Unknown failure encoding log packet", startLine: startLine, endLine: endLine, startColumn: startColumn, endColumn: endColumn, threadID: packet.threadID)
4762

4863
// Encoding an error packet should not fail under any circumstances.
4964
data = try! errorPacket.encode()
@@ -84,8 +99,23 @@ func legacyLogPostPrint(startLine: Int, endLine: Int, startColumn: Int, endColum
8499
do {
85100
data = try packet.encode()
86101
}
102+
catch LoggingError.failedToGenerateOpaqueRepresentation {
103+
fatalError("Failures to generate opaque representations should not occur during encoding")
104+
}
105+
catch let LoggingError.encodingFailure(reason) {
106+
let errorPacket = LogPacket(errorWithReason: reason, startLine: startLine, endLine: endLine, startColumn: startColumn, endColumn: endColumn, threadID: packet.threadID)
107+
108+
// Encoding an error packet should not fail under any circumstances.
109+
data = try! errorPacket.encode()
110+
}
111+
catch let LoggingError.otherFailure(reason) {
112+
let errorPacket = LogPacket(errorWithReason: reason, startLine: startLine, endLine: endLine, startColumn: startColumn, endColumn: endColumn, threadID: packet.threadID)
113+
114+
// Encoding an error packet should not fail under any circumstances.
115+
data = try! errorPacket.encode()
116+
}
87117
catch {
88-
let errorPacket = LogPacket(errorWithReason: "Error occurred while encoding log packet", startLine: startLine, endLine: endLine, startColumn: startColumn, endColumn: endColumn, threadID: packet.threadID)
118+
let errorPacket = LogPacket(errorWithReason: "Unknown failure encoding log packet", startLine: startLine, endLine: endLine, startColumn: startColumn, endColumn: endColumn, threadID: packet.threadID)
89119

90120
// Encoding an error packet should not fail under any circumstances.
91121
data = try! errorPacket.encode()

PlaygroundLogger/PlaygroundLogger/LogEntry+Reflection.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,17 @@ extension Mirror {
164164
do {
165165
return try LogEntry(describing: child.value, name: child.label ?? emptyNameString, typeName: nil, summary: nil, policy: policy, currentDepth: childDepth)
166166
}
167+
catch let LoggingError.failedToGenerateOpaqueRepresentation(reason) {
168+
return LogEntry.error(reason: reason)
169+
}
170+
catch LoggingError.encodingFailure {
171+
fatalError("Encoding failures should not be encountered while generating LogEntry values")
172+
}
173+
catch let LoggingError.otherFailure(reason) {
174+
return LogEntry.error(reason: reason)
175+
}
167176
catch {
168-
// TODO: provide a better error string
169-
return .error(reason: "Error generating log entry")
177+
return LogEntry.error(reason: "Unknown error encountered when generating log entry")
170178
}
171179
}
172180

PlaygroundLogger/PlaygroundLogger/LogPacket.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,17 @@ extension LogPacket {
4040
do {
4141
logEntry = try LogEntry(describing: result, name: name, policy: policy)
4242
}
43+
catch let LoggingError.failedToGenerateOpaqueRepresentation(reason) {
44+
logEntry = .error(reason: reason)
45+
}
46+
catch LoggingError.encodingFailure {
47+
fatalError("Encoding failures should not be encountered while generating LogEntry values")
48+
}
49+
catch let LoggingError.otherFailure(reason) {
50+
logEntry = .error(reason: reason)
51+
}
4352
catch {
44-
// TODO: provide a better error string
45-
logEntry = .error(reason: "Error generating log entry")
53+
logEntry = .error(reason: "Unknown error encountered when generating log entry")
4654
}
4755

4856
self = .init(logEntry: logEntry, startLine: startLine, endLine: endLine, startColumn: startColumn, endColumn: endColumn, threadID: threadID)

PlaygroundLogger/PlaygroundLogger/LoggerEntrypoints.swift

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,23 @@ func logResult(_ result: Any,
2525
do {
2626
data = try packet.encode()
2727
}
28+
catch LoggingError.failedToGenerateOpaqueRepresentation {
29+
fatalError("Failures to generate opaque representations should not occur during encoding")
30+
}
31+
catch let LoggingError.encodingFailure(reason) {
32+
let errorPacket = LogPacket(errorWithReason: reason, startLine: startLine, endLine: endLine, startColumn: startColumn, endColumn: endColumn, threadID: packet.threadID)
33+
34+
// Encoding an error packet should not fail under any circumstances.
35+
data = try! errorPacket.encode()
36+
}
37+
catch let LoggingError.otherFailure(reason) {
38+
let errorPacket = LogPacket(errorWithReason: reason, startLine: startLine, endLine: endLine, startColumn: startColumn, endColumn: endColumn, threadID: packet.threadID)
39+
40+
// Encoding an error packet should not fail under any circumstances.
41+
data = try! errorPacket.encode()
42+
}
2843
catch {
29-
let errorPacket = LogPacket(errorWithReason: "Error occurred while encoding log packet", startLine: startLine, endLine: endLine, startColumn: startColumn, endColumn: endColumn, threadID: packet.threadID)
44+
let errorPacket = LogPacket(errorWithReason: "Unknown failure encoding log packet", startLine: startLine, endLine: endLine, startColumn: startColumn, endColumn: endColumn, threadID: packet.threadID)
3045

3146
// Encoding an error packet should not fail under any circumstances.
3247
data = try! errorPacket.encode()
@@ -81,8 +96,23 @@ func logPostPrint(startLine: Int,
8196
do {
8297
data = try packet.encode()
8398
}
99+
catch LoggingError.failedToGenerateOpaqueRepresentation {
100+
fatalError("Failures to generate opaque representations should not occur during encoding")
101+
}
102+
catch let LoggingError.encodingFailure(reason) {
103+
let errorPacket = LogPacket(errorWithReason: reason, startLine: startLine, endLine: endLine, startColumn: startColumn, endColumn: endColumn, threadID: packet.threadID)
104+
105+
// Encoding an error packet should not fail under any circumstances.
106+
data = try! errorPacket.encode()
107+
}
108+
catch let LoggingError.otherFailure(reason) {
109+
let errorPacket = LogPacket(errorWithReason: reason, startLine: startLine, endLine: endLine, startColumn: startColumn, endColumn: endColumn, threadID: packet.threadID)
110+
111+
// Encoding an error packet should not fail under any circumstances.
112+
data = try! errorPacket.encode()
113+
}
84114
catch {
85-
let errorPacket = LogPacket(errorWithReason: "Error occurred while encoding log packet", startLine: startLine, endLine: endLine, startColumn: startColumn, endColumn: endColumn, threadID: packet.threadID)
115+
let errorPacket = LogPacket(errorWithReason: "Unknown failure encoding log packet", startLine: startLine, endLine: endLine, startColumn: startColumn, endColumn: endColumn, threadID: packet.threadID)
86116

87117
// Encoding an error packet should not fail under any circumstances.
88118
data = try! errorPacket.encode()

0 commit comments

Comments
 (0)