Skip to content

Commit 8a7cebc

Browse files
authored
Merge pull request swiftlang#2442 from benrimmington/debug-description-error-key
Replace literal "NSDebugDescription" error keys
2 parents 0a2a701 + c671cbc commit 8a7cebc

File tree

5 files changed

+26
-25
lines changed

5 files changed

+26
-25
lines changed

CoreFoundation/PlugIn.subproj/CFBundle.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <CoreFoundation/CFSet.h>
1515
#include <CoreFoundation/CFURLAccess.h>
1616
#include <CoreFoundation/CFError.h>
17+
#include <CoreFoundation/CFError_Private.h>
1718
#include <string.h>
1819
#include <CoreFoundation/CFPriv.h>
1920
#include "CFInternal.h"
@@ -1122,7 +1123,7 @@ CF_PRIVATE CFErrorRef _CFBundleCreateErrorDebug(CFAllocatorRef allocator, CFBund
11221123
numKeys++;
11231124
}
11241125
if (debugString) {
1125-
userInfoKeys[numKeys] = CFSTR("NSDebugDescription");
1126+
userInfoKeys[numKeys] = kCFErrorDebugDescriptionKey;
11261127
userInfoValues[numKeys] = debugString;
11271128
numKeys++;
11281129
}

Foundation/JSONSerialization.swift

+18-18
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ open class JSONSerialization : NSObject {
183183
return value
184184
}
185185
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: [
186-
"NSDebugDescription" : "JSON text did not start with array or object and option to allow fragments not set."
186+
NSDebugDescriptionErrorKey : "JSON text did not start with array or object and option to allow fragments not set."
187187
])
188188
}
189189

@@ -353,7 +353,7 @@ private struct JSONWriter {
353353
let num = __SwiftValue.store(obj) as! NSNumber
354354
writer(num.description)
355355
default:
356-
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: ["NSDebugDescription" : "Invalid object cannot be serialized"])
356+
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: [NSDebugDescriptionErrorKey : "Invalid object cannot be serialized"])
357357
}
358358
}
359359

@@ -390,7 +390,7 @@ private struct JSONWriter {
390390

391391
private func serializeFloat<T: FloatingPoint & LosslessStringConvertible>(_ num: T) throws {
392392
guard num.isFinite else {
393-
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: ["NSDebugDescription" : "Invalid number value (\(num)) in JSON write"])
393+
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: [NSDebugDescriptionErrorKey : "Invalid number value (\(num)) in JSON write"])
394394
}
395395
var str = num.description
396396
if str.hasSuffix(".0") {
@@ -460,7 +460,7 @@ private struct JSONWriter {
460460
if let key = key as? String {
461461
try serializeString(key)
462462
} else {
463-
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: ["NSDebugDescription" : "NSDictionary key must be NSString"])
463+
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: [NSDebugDescriptionErrorKey : "NSDictionary key must be NSString"])
464464
}
465465
pretty ? writer(" : ") : writer(":")
466466
try serializeJSON(value)
@@ -470,7 +470,7 @@ private struct JSONWriter {
470470
let elems = try dict.sorted(by: { a, b in
471471
guard let a = a.key as? String,
472472
let b = b.key as? String else {
473-
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: ["NSDebugDescription" : "NSDictionary key must be NSString"])
473+
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: [NSDebugDescriptionErrorKey : "NSDictionary key must be NSString"])
474474
}
475475
let options: NSString.CompareOptions = [.numeric, .caseInsensitive, .forcedOrdering]
476476
let range: Range<String.Index> = a.startIndex..<a.endIndex
@@ -593,7 +593,7 @@ private struct JSONReader {
593593

594594
guard let chunk = String(data: Data(bytes: buffer.baseAddress!.advanced(by: begin), count: byteLength), encoding: encoding) else {
595595
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: [
596-
"NSDebugDescription" : "Unable to convert data to a string using the detected encoding. The data may be corrupt."
596+
NSDebugDescriptionErrorKey : "Unable to convert data to a string using the detected encoding. The data may be corrupt."
597597
])
598598
}
599599
return chunk
@@ -627,7 +627,7 @@ private struct JSONReader {
627627
switch self.source.takeASCII(input) {
628628
case nil:
629629
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: [
630-
"NSDebugDescription" : "Unexpected end of file during JSON parse."
630+
NSDebugDescriptionErrorKey : "Unexpected end of file during JSON parse."
631631
])
632632
case let (taken, index)? where taken == ascii:
633633
return index
@@ -686,22 +686,22 @@ private struct JSONReader {
686686
}
687687
else {
688688
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: [
689-
"NSDebugDescription" : "Invalid escape sequence at position \(source.distanceFromStart(currentIndex))"
689+
NSDebugDescriptionErrorKey : "Invalid escape sequence at position \(source.distanceFromStart(currentIndex))"
690690
])
691691
}
692692
default:
693693
currentIndex = index
694694
}
695695
}
696696
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: [
697-
"NSDebugDescription" : "Unexpected end of file during string parse."
697+
NSDebugDescriptionErrorKey : "Unexpected end of file during string parse."
698698
])
699699
}
700700

701701
func parseEscapeSequence(_ input: Index) throws -> (String, Index)? {
702702
guard let (byte, index) = source.takeASCII(input) else {
703703
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: [
704-
"NSDebugDescription" : "Early end of unicode escape sequence around character"
704+
NSDebugDescriptionErrorKey : "Early end of unicode escape sequence around character"
705705
])
706706
}
707707
let output: String
@@ -741,7 +741,7 @@ private struct JSONReader {
741741
// Trail surrogate must come after lead surrogate
742742
throw CocoaError.error(.propertyListReadCorrupt,
743743
userInfo: [
744-
"NSDebugDescription" : """
744+
NSDebugDescriptionErrorKey : """
745745
Unable to convert unicode escape sequence (no high-surrogate code point) \
746746
to UTF8-encoded character at position \(source.distanceFromStart(input))
747747
"""
@@ -752,7 +752,7 @@ private struct JSONReader {
752752
UTF16.isTrailSurrogate(trailCodeUnit) else {
753753
throw CocoaError.error(.propertyListReadCorrupt,
754754
userInfo: [
755-
"NSDebugDescription" : """
755+
NSDebugDescriptionErrorKey : """
756756
Unable to convert unicode escape sequence (no low-surrogate code point) \
757757
to UTF8-encoded character at position \(source.distanceFromStart(input))
758758
"""
@@ -852,7 +852,7 @@ private struct JSONReader {
852852
guard nextASCII() else { return true }
853853
} else {
854854
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue,
855-
userInfo: ["NSDebugDescription" : "Numbers must start with a 1-9 at character \(input)." ])
855+
userInfo: [NSDebugDescriptionErrorKey : "Numbers must start with a 1-9 at character \(input)." ])
856856
}
857857

858858
if ascii == JSONReader.DECIMAL_SEPARATOR {
@@ -861,7 +861,7 @@ private struct JSONReader {
861861
guard nextASCII() else { return true }
862862
} else if JSONReader.allDigits.contains(ascii) {
863863
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue,
864-
userInfo: ["NSDebugDescription" : "Leading zeros not allowed at character \(input)." ])
864+
userInfo: [NSDebugDescriptionErrorKey : "Leading zeros not allowed at character \(input)." ])
865865
}
866866

867867
digitCount = string.count - (isInteger ? 0 : 1) - (isNegative ? 1 : 0)
@@ -982,17 +982,17 @@ private struct JSONReader {
982982
func parseObjectMember(_ input: Index, options opt: JSONSerialization.ReadingOptions) throws -> (String, Any, Index)? {
983983
guard let (name, index) = try parseString(input) else {
984984
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: [
985-
"NSDebugDescription" : "Missing object key at location \(source.distanceFromStart(input))"
985+
NSDebugDescriptionErrorKey : "Missing object key at location \(source.distanceFromStart(input))"
986986
])
987987
}
988988
guard let separatorIndex = try consumeStructure(Structure.NameSeparator, input: index) else {
989989
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: [
990-
"NSDebugDescription" : "Invalid separator at location \(source.distanceFromStart(index))"
990+
NSDebugDescriptionErrorKey : "Invalid separator at location \(source.distanceFromStart(index))"
991991
])
992992
}
993993
guard let (value, finalIndex) = try parseValue(separatorIndex, options: opt) else {
994994
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: [
995-
"NSDebugDescription" : "Invalid value at location \(source.distanceFromStart(separatorIndex))"
995+
NSDebugDescriptionErrorKey : "Invalid value at location \(source.distanceFromStart(separatorIndex))"
996996
])
997997
}
998998

@@ -1023,7 +1023,7 @@ private struct JSONReader {
10231023
}
10241024
}
10251025
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: [
1026-
"NSDebugDescription" : "Badly formed array at location \(source.distanceFromStart(index))"
1026+
NSDebugDescriptionErrorKey : "Badly formed array at location \(source.distanceFromStart(index))"
10271027
])
10281028
}
10291029
}

Foundation/NSCoder.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ open class NSCoder : NSObject {
732732
}
733733

734734
open func failWithError(_ error: Error) {
735-
if let debugDescription = (error as? NSError)?.userInfo["NSDebugDescription"] {
735+
if let debugDescription = (error as? NSError)?.userInfo[NSDebugDescriptionErrorKey] {
736736
NSLog("*** NSKeyedUnarchiver.init: \(debugDescription)")
737737
} else {
738738
NSLog("*** NSKeyedUnarchiver.init: decoding error")

Foundation/NSKeyedUnarchiver.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ open class NSKeyedUnarchiver : NSCoder {
407407

408408
private func _decodingError(_ code: CocoaError.Code, withDescription description: String) -> NSError {
409409
return NSError(domain: NSCocoaErrorDomain,
410-
code: code.rawValue, userInfo: [ "NSDebugDescription" : description ])
410+
code: code.rawValue, userInfo: [ NSDebugDescriptionErrorKey : description ])
411411
}
412412

413413
private func _replacementObject(_ decodedObject: Any?) -> Any? {

Foundation/NSString.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -1376,15 +1376,15 @@ extension NSString {
13761376
let bytePtr = readResult.bytes.bindMemory(to: UInt8.self, capacity: readResult.length)
13771377
guard let cf = CFStringCreateWithBytes(kCFAllocatorDefault, bytePtr, readResult.length, CFStringConvertNSStringEncodingToEncoding(numericCast(enc)), true) else {
13781378
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.fileReadInapplicableStringEncoding.rawValue, userInfo: [
1379-
"NSDebugDescription" : "Unable to create a string using the specified encoding."
1379+
NSDebugDescriptionErrorKey : "Unable to create a string using the specified encoding."
13801380
])
13811381
}
13821382
var str: String?
13831383
if String._conditionallyBridgeFromObjectiveC(cf._nsObject, result: &str) {
13841384
self.init(str!)
13851385
} else {
13861386
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.fileReadInapplicableStringEncoding.rawValue, userInfo: [
1387-
"NSDebugDescription" : "Unable to bridge CFString to String."
1387+
NSDebugDescriptionErrorKey : "Unable to bridge CFString to String."
13881388
])
13891389
}
13901390
}
@@ -1431,15 +1431,15 @@ extension NSString {
14311431
guard let cf = CFStringCreateWithBytes(kCFAllocatorDefault, bytePtr + offset, readResult.length - offset,
14321432
CFStringConvertNSStringEncodingToEncoding(numericCast(encoding)), true) else {
14331433
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.fileReadInapplicableStringEncoding.rawValue, userInfo: [
1434-
"NSDebugDescription" : "Unable to create a string using the specified encoding."
1434+
NSDebugDescriptionErrorKey : "Unable to create a string using the specified encoding."
14351435
])
14361436
}
14371437
var str: String?
14381438
if String._conditionallyBridgeFromObjectiveC(cf._nsObject, result: &str) {
14391439
self.init(str!)
14401440
} else {
14411441
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.fileReadInapplicableStringEncoding.rawValue, userInfo: [
1442-
"NSDebugDescription" : "Unable to bridge CFString to String."
1442+
NSDebugDescriptionErrorKey : "Unable to bridge CFString to String."
14431443
])
14441444
}
14451445
enc?.pointee = encoding

0 commit comments

Comments
 (0)