Eneko Alonso
Eneko Alonso

Reputation: 19722

How to catch NSKeyedUnarchiver "NSInvalidUnarchiveOperationException" in Swift?

I am using Codable with NSKeyedArchiver and NSKeyedUnarchiver in Swift to endoce/decode objects.

Things work great when the encoded object matches the expected format. However, if the data is invalid or the format does not match, I am getting the following assertion in my unit tests with XCTest:

failed: caught "NSInvalidUnarchiveOperationException", "The data couldn’t be read because it isn’t in the correct format."

I'm using NSKeyedUnarchiver.decodeTopLevelDecodable method, which is supposed to throw an exception when decoding fails:

If the archive is not a valid property list, this method throws the DecodingError.dataCorrupted(_:) error. If a value within the archive fails to decode, this method throws the corresponding error.

I'm having trouble catching this exception, as neither try?, do/catch nor XCTAssertThrowsError seem to work.

Full error message when test fails:

failed: caught "NSInvalidUnarchiveOperationException", "The data couldn’t be read because it isn’t in the correct format."
(
    0   CoreFoundation                      0x00007fff315f12db __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x00007fff587a1c76 objc_exception_throw + 48
    2   Foundation                          0x00007fff3376b077 -[NSCoder(Exceptions) __failWithExceptionName:errorCode:format:] + 0
    3   Foundation                          0x00007fff3376b371 -[NSCoder(Exceptions) __failWithExternalError:] + 161
    4   libswiftFoundation.dylib            0x0000000109872453 _T0So17NSKeyedUnarchiverC10FoundationE23decodeTopLevelDecodablexSgxm_SS6forKeytKs0G0RzlF + 403
    5   MBOUtilityKit                       0x0000000104cba964 _T013MBOUtilityKit17KeyedLocalStorageV3getxSS3key_tKs9DecodableRzs9EncodableRzlF + 772
    6   MBOUtilityKitTests macOS            0x0000000104c5144c _T024MBOUtilityKitTests_macOS017KeyedLocalStorageC0C17testDecodeFailureyyKF + 556
    7   MBOUtilityKitTests macOS            0x0000000104c51835 _T024MBOUtilityKitTests_macOS017KeyedLocalStorageC0C17testDecodeFailureyyKFTo + 69
    8   CoreFoundation                      0x00007fff31568bec __invoking___ + 140
    9   CoreFoundation                      0x00007fff31568ac0 -[NSInvocation invoke] + 320
    10  XCTest                              0x000000010036c90d __24-[XCTestCase invokeTest]_block_invoke_2.187 + 65
    11  XCTest                              0x00000001003d6207 -[XCTMemoryChecker _assertInvalidObjectsDeallocatedAfterScope:] + 51
    12  XCTest                              0x00000001003755ef -[XCTestCase assertInvalidObjectsDeallocatedAfterScope:] + 116
    13  XCTest                              0x000000010036c89c __24-[XCTestCase invokeTest]_block_invoke.181 + 210
    14  XCTest                              0x00000001003c8772 +[XCTestCase(Failures) performFailableBlock:shouldInterruptTest:] + 36
    15  XCTest                              0x00000001003c86bc -[XCTestCase(Failures) _performTurningExceptionsIntoFailuresInterruptAfterHandling:block:] + 54
    16  XCTest                              0x000000010036c4db __24-[XCTestCase invokeTest]_block_invoke + 854
    17  XCTest                              0x00000001003cd659 -[XCUITestContext performInScope:] + 237
    18  XCTest                              0x000000010036c170 -[XCTestCase invokeTest] + 175
    19  XCTest                              0x000000010036dea6 __26-[XCTestCase performTest:]_block_invoke_2 + 42
    20  XCTest                              0x00000001003c8772 +[XCTestCase(Failures) performFailableBlock:shouldInterruptTest:] + 36
    21  XCTest                              0x00000001003c86bc -[XCTestCase(Failures) _performTurningExceptionsIntoFailuresInterruptAfterHandling:block:] + 54
    22  XCTest                              0x000000010036dd53 __26-[XCTestCase performTest:]_block_invoke.326 + 90
    23  XCTest                              0x00000001003d24a1 +[XCTContext runInContextForTestCase:block:] + 225
    24  XCTest                              0x000000010036d454 -[XCTestCase performTest:] + 673
    25  XCTest                              0x00000001003b0555 -[XCTest runTest] + 57
    26  XCTest                              0x0000000100368b30 __27-[XCTestSuite performTest:]_block_invoke + 365
    27  XCTest                              0x000000010036830c -[XCTestSuite _performProtectedSectionForTest:testSection:] + 55
    28  XCTest                              0x00000001003685cd -[XCTestSuite performTest:] + 296
    29  XCTest                              0x00000001003b0555 -[XCTest runTest] + 57
    30  XCTest                              0x0000000100368b30 __27-[XCTestSuite performTest:]_block_invoke + 365
    31  XCTest                              0x000000010036830c -[XCTestSuite _performProtectedSectionForTest:testSection:] + 55
    32  XCTest                              0x00000001003685cd -[XCTestSuite performTest:] + 296
    33  XCTest                              0x00000001003b0555 -[XCTest runTest] + 57
    34  XCTest                              0x0000000100368b30 __27-[XCTestSuite performTest:]_block_invoke + 365
    35  XCTest                              0x000000010036830c -[XCTestSuite _performProtectedSectionForTest:testSection:] + 55
    36  XCTest                              0x00000001003685cd -[XCTestSuite performTest:] + 296
    37  XCTest                              0x00000001003b0555 -[XCTest runTest] + 57
    38  XCTest                              0x00000001003e6308 __44-[XCTTestRunSession runTestsAndReturnError:]_block_invoke + 40
    39  XCTest                              0x000000010038aaa4 -[XCTestObservationCenter _observeTestExecutionForBlock:] + 600
    40  XCTest                              0x00000001003e610e -[XCTTestRunSession runTestsAndReturnError:] + 369
    41  XCTest                              0x000000010034f865 -[XCTestDriver runTestsAndReturnError:] + 440
    42  XCTest                              0x00000001003d10f3 _XCTestMain + 1228
    43  xctest                              0x0000000100002155 main + 557
    44  libdyld.dylib                       0x00007fff593bb015 start + 1
    45  ???                                 0x0000000000000005 0x0 + 5
)

Upvotes: 1

Views: 1453

Answers (3)

Konstantin Oznobihin
Konstantin Oznobihin

Reputation: 5327

You need to set unarchiver failure policy like this:

let unarchiver = NSKeyedUnarchiver(forReadingWith: data)
unarchiver.decodingFailurePolicy = .setErrorAndReturn

This will make NSKeyedUnarchiver return NSError which will be correctly translated into swift exception that you will be able to catch.

Upvotes: 1

al_mota
al_mota

Reputation: 471

Since iOS 9 you can use: unarchiveTopLevelObjectWithData(_:), which throws exceptions.

Example:

try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data)

https://developer.apple.com/documentation/foundation/nskeyedunarchiver/2919664-unarchivetoplevelobjectwithdata

Upvotes: 4

ekscrypto
ekscrypto

Reputation: 3816

The objective-C bridge for that class wasn't setup properly to throw the error in Swift, so you will have to create an Objective-C wrapper that throws or catch the error.

Here's my super-simple 'SafeUnarchiver" from which you can inspire yourself:

SafeUnarchiver.h:

#import <Foundation/Foundation.h>

@interface SafeUnarchiver : NSObject
+(NSObject* _Nullable)unarchive:(NSData* _Nonnull)data;
@end

SafeUnarchiver.m:

#import "SafeUnarchiver.h"

@implementation SafeUnarchiver
+(NSObject *)unarchive:(NSData *)data {
    @try {
        id object = [NSKeyedUnarchiver unarchiveObjectWithData:data];
        return object;
    } @catch (NSException *exception) {
        NSLog(@"ERROR attempting to unarchive object: %@", exception);
    }
    return nil;
}
@end

Don't forget to file a Radar ticket with Apple so they can eventually fix this exception not being caught properly.

As per other Objective-C code you want to access from Swift, you'll have to add the header to your -Bridging-Header.h

Upvotes: 2

Related Questions