Skip to content

Commit 4804597

Browse files
committed
[PlaygroundLogger] Introduced a temporary loggingError function.
This is intended as a placeholder for all of the places which should generate proper errors once error handling/generation is implemented. This makes it easier to see what's actually unimplemented, what actually should call fatalError, and what actually should report proper errors to the user.
1 parent f4e6a96 commit 4804597

File tree

10 files changed

+47
-22
lines changed

10 files changed

+47
-22
lines changed

PlaygroundLogger/PlaygroundLogger.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
5E5D77802040BD7D00EBC3A9 /* LogPolicyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E5D777F2040BD7D00EBC3A9 /* LogPolicyTests.swift */; };
7878
5E5D77812040BD7D00EBC3A9 /* LogPolicyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E5D777F2040BD7D00EBC3A9 /* LogPolicyTests.swift */; };
7979
5E5D77822040BD7D00EBC3A9 /* LogPolicyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E5D777F2040BD7D00EBC3A9 /* LogPolicyTests.swift */; };
80+
5E5D77842040F5E900EBC3A9 /* LoggingError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E5D77832040F5E900EBC3A9 /* LoggingError.swift */; };
8081
5E5F600B20409D4E007EF0A8 /* LogPolicy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E5F600A20409D4E007EF0A8 /* LogPolicy.swift */; };
8182
5E5FE50B202D13C800E28C3C /* PGLConcurrentMap.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E5FE50A202D13C800E28C3C /* PGLConcurrentMap.swift */; };
8283
5ECE8F911FFCD2A70034D9BC /* LegacyPlaygroundLoggerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5ECE8F901FFCD2A70034D9BC /* LegacyPlaygroundLoggerTests.swift */; };
@@ -243,6 +244,7 @@
243244
5E4AF1AC1FDDBDE400B7C9D9 /* Unimplemented.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Unimplemented.swift; sourceTree = "<group>"; };
244245
5E4AF1AE1FDDC12A00B7C9D9 /* LegacyEntrypoints.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LegacyEntrypoints.swift; sourceTree = "<group>"; };
245246
5E5D777F2040BD7D00EBC3A9 /* LogPolicyTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LogPolicyTests.swift; sourceTree = "<group>"; };
247+
5E5D77832040F5E900EBC3A9 /* LoggingError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoggingError.swift; sourceTree = "<group>"; };
246248
5E5F600A20409D4E007EF0A8 /* LogPolicy.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LogPolicy.swift; sourceTree = "<group>"; };
247249
5E5FE50A202D13C800E28C3C /* PGLConcurrentMap.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PGLConcurrentMap.swift; sourceTree = "<group>"; };
248250
5ECE8F901FFCD2A70034D9BC /* LegacyPlaygroundLoggerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LegacyPlaygroundLoggerTests.swift; sourceTree = "<group>"; };
@@ -564,6 +566,7 @@
564566
isa = PBXGroup;
565567
children = (
566568
5E4AF1AC1FDDBDE400B7C9D9 /* Unimplemented.swift */,
569+
5E5D77832040F5E900EBC3A9 /* LoggingError.swift */,
567570
5E184716202BB80200F01AD1 /* PGLConcurrentMap.h */,
568571
5E184717202BB80200F01AD1 /* PGLConcurrentMap_MRR.m */,
569572
5E5FE50A202D13C800E28C3C /* PGLConcurrentMap.swift */,
@@ -890,6 +893,7 @@
890893
5E27566E1FC51F7E00B69C83 /* CGGeometry+CustomOpaqueLoggable.swift in Sources */,
891894
5E2756201FC485AA00B69C83 /* TaggedOpaqueRepresentation.swift in Sources */,
892895
5E2646431FB64B2A002DC6B6 /* LoggerEntrypoints.swift in Sources */,
896+
5E5D77842040F5E900EBC3A9 /* LoggingError.swift in Sources */,
893897
5E2646521FB65026002DC6B6 /* LoggerInitialization.swift in Sources */,
894898
5E2755D51FB6807F00B69C83 /* LogPacket+Encoding.swift in Sources */,
895899
5E2756461FC4DC0C00B69C83 /* ImageOpaqueRepresentation.swift in Sources */,

PlaygroundLogger/PlaygroundLogger/CustomLoggable/CoreImage/CIColor+CustomOpaqueLoggable.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ import CoreImage
1515
extension CIColor: CustomOpaqueLoggable {
1616
var opaqueRepresentation: LogEntry.OpaqueRepresentation {
1717
guard let color = CGColor(colorSpace: self.colorSpace, components: self.components) else {
18-
// TODO: don't crash in this case
19-
fatalError("Unable to convert CIColor to CGColor")
18+
loggingError("Unable to convert CIColor to CGColor")
2019
}
2120

2221
return color

PlaygroundLogger/PlaygroundLogger/CustomLoggable/SpriteKit/SpriteKitOpaqueLoggable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fileprivate protocol SpriteKitOpaqueLoggable: class, OpaqueImageRepresentable, C
2323
extension SpriteKitOpaqueLoggable {
2424
func encodeImage(into encoder: LogEncoder, withFormat format: LogEncoder.Format) {
2525
guard let copyImageDataMethod = (self as AnyObject)._copyImageData, let imageData = copyImageDataMethod() else {
26-
unimplemented("Handle case where we don't have any image data")
26+
loggingError("SpriteKit did not return any image data")
2727
}
2828

2929
encoder.encode(number: UInt64(imageData.count))

PlaygroundLogger/PlaygroundLogger/LegacySupport/PlaygroundQuickLook+OpaqueRepresentationSupport.swift

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,51 +36,51 @@ extension PlaygroundQuickLook {
3636
case let .image(image):
3737
#if os(macOS)
3838
guard let image = image as? NSImage else {
39-
fatalError("Must be an NSImage")
39+
loggingError("Must be an NSImage")
4040
}
4141

4242
return ImageOpaqueRepresentation(kind: .image, backedBy: image)
4343
#elseif os(iOS) || os(tvOS)
4444
guard let image = image as? UIImage else {
45-
fatalError("Must be an IOImage")
45+
loggingError("Must be an IOImage")
4646
}
4747

4848
return ImageOpaqueRepresentation(kind: .image, backedBy: image)
4949

5050
#endif
5151
case .sound:
52-
fatalError("Sounds not yet supported")
52+
loggingError("Sounds not supported")
5353
case let .color(color):
5454
#if os(macOS)
5555
guard let color = color as? NSColor else {
56-
fatalError("Must be an NSColor")
56+
loggingError("Must be an NSColor")
5757
}
5858

5959
return color.cgColor
6060
#elseif os(iOS) || os(tvOS)
6161
guard let color = color as? UIColor else {
62-
fatalError("Must be a UIColor")
62+
loggingError("Must be a UIColor")
6363
}
6464

6565
return color.cgColor
6666
#endif
6767
case let .bezierPath(bezierPath):
6868
#if os(macOS)
6969
guard let bezierPath = bezierPath as? NSBezierPath else {
70-
fatalError("Must be an NSBezierPath")
70+
loggingError("Must be an NSBezierPath")
7171
}
7272

7373
return bezierPath
7474
#elseif os(iOS) || os(tvOS)
7575
guard let bezierPath = bezierPath as? UIBezierPath else {
76-
fatalError("Must be a UIBezierPath")
76+
loggingError("Must be a UIBezierPath")
7777
}
7878

7979
return bezierPath
8080
#endif
8181
case let .attributedString(attributedString):
8282
guard let attributedString = attributedString as? NSAttributedString else {
83-
fatalError("Must be an NSAttributedString")
83+
loggingError("Must be an NSAttributedString")
8484
}
8585

8686
return attributedString
@@ -103,7 +103,7 @@ extension PlaygroundQuickLook {
103103
return ImageOpaqueRepresentation(kind: .view, backedBy: image)
104104
}
105105
else {
106-
fatalError("Must be an NSView or NSImage")
106+
loggingError("Must be an NSView or NSImage")
107107
}
108108
#elseif os(iOS) || os(tvOS)
109109
if let view = viewOrImage as? UIView {
@@ -113,27 +113,27 @@ extension PlaygroundQuickLook {
113113
return ImageOpaqueRepresentation(kind: .view, backedBy: image)
114114
}
115115
else {
116-
fatalError("Must be a UIView or UIImage")
116+
loggingError("Must be a UIView or UIImage")
117117
}
118118
#endif
119119
case let .sprite(image):
120120
// TODO: figure out if this is even a little bit right. (The previous implementation just logged a string for sprites?)
121121
#if os(macOS)
122122
guard let image = image as? NSImage else {
123-
fatalError("Must be an NSImage")
123+
loggingError("Must be an NSImage")
124124
}
125125

126126
return ImageOpaqueRepresentation(kind: .sprite, backedBy: image)
127127
#elseif os(iOS) || os(tvOS)
128128
guard let image = image as? UIImage else {
129-
fatalError("Must be a UIImage")
129+
loggingError("Must be a UIImage")
130130
}
131131

132132
return ImageOpaqueRepresentation(kind: .sprite, backedBy: image)
133133
#endif
134134
case let .url(urlString):
135135
guard let url = URL(string: urlString) else {
136-
fatalError("Must be a valid URL string!")
136+
loggingError("Must be a valid URL string!")
137137
}
138138

139139
return url

PlaygroundLogger/PlaygroundLogger/OpaqueRepresentations/AppKit/NSBitmapImageRep+OpaqueImageRepresentable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
extension NSBitmapImageRep: OpaqueImageRepresentable {
1717
func encodeImage(into encoder: LogEncoder, withFormat format: LogEncoder.Format) {
1818
guard let pngData = self.representation(using: .png, properties: [:]) else {
19-
unimplemented("Need to handle error when we couldn't generate PNG data")
19+
loggingError("Failed to generate PNG data")
2020
}
2121

2222
encoder.encode(number: UInt64(pngData.count))

PlaygroundLogger/PlaygroundLogger/OpaqueRepresentations/AppKit/NSImage+OpaqueImageRepresentable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
func encodeImage(into encoder: LogEncoder, withFormat format: LogEncoder.Format) {
3636
guard let bitmapRep = self.bestBitmapRepresentation else {
37-
unimplemented("Need to figure out how to handle when we can't get the best bitmap rep!")
37+
loggingError("Failed to convert image to a bitmap representation")
3838
}
3939

4040
bitmapRep.encodeImage(into: encoder, withFormat: format)

PlaygroundLogger/PlaygroundLogger/OpaqueRepresentations/AppKit/NSView+OpaqueImageRepresentable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
extension NSView: OpaqueImageRepresentable {
1717
func encodeImage(into encoder: LogEncoder, withFormat format: LogEncoder.Format) {
1818
guard let bitmapRep = self.bitmapImageRepForCachingDisplay(in: self.bounds) else {
19-
unimplemented("Need to handle cases where we can't get a bitmap rep!")
19+
loggingError("Failed to get a bitmap representation of a view")
2020
}
2121

2222
self.cacheDisplay(in: self.bounds, to: bitmapRep)

PlaygroundLogger/PlaygroundLogger/OpaqueRepresentations/CoreGraphics/CGColor+KeyedArchiveOpaqueRepresentation.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ extension CGColor: KeyedArchiveOpaqueRepresentation {
2323

2424
func encodeOpaqueRepresentation(with encoder: NSCoder, usingFormat format: LogEncoder.Format) {
2525
guard let colorSpace = self.colorSpace else {
26-
unimplemented("Need to handle colors without a color space somehow")
26+
loggingError("Unable to log colors without a colorspace")
2727
}
2828

2929
guard colorSpace.model != .pattern else {
3030
unimplemented("Need to handle pattern colors")
3131
}
3232

3333
guard let colorSpaceName = colorSpace.name, let components = self.components else {
34-
unimplemented("Need to handle colors with an unnamed color space or with missing components")
34+
loggingError("Unable to log non-pattern colors with unnamed colorspaces or which are missing components")
3535
}
3636

3737
encoder.encode(colorSpaceName as NSString, forKey: colorSpaceKey)

PlaygroundLogger/PlaygroundLogger/OpaqueRepresentations/UIKit/UIImage+OpaqueImageRepresentable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
extension UIImage: OpaqueImageRepresentable {
1717
func encodeImage(into encoder: LogEncoder, withFormat format: LogEncoder.Format) {
1818
guard let pngData = UIImagePNGRepresentation(self) else {
19-
unimplemented("Need to handle when we can't convert a UIImage to a PNG")
19+
loggingError("Failed to convert an image to a PNG")
2020
}
2121

2222
encoder.encode(number: UInt64(pngData.count))
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===--- LoggingError.swift -----------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2017 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
/// A temporary function intended as a placeholder for real error handling, so that realistic error situations can be marked differently than a plain fatalError.
14+
func loggingError(_ message: String? = nil) -> Never {
15+
if let message = message {
16+
fatalError("Error encountered while logging: \(message)")
17+
}
18+
else {
19+
fatalError("Error encountered while logging")
20+
}
21+
}
22+

0 commit comments

Comments
 (0)