@@ -225,7 +225,16 @@ + (void)initialize
225225
226226- (nullable instancetype )initWithPayload : (NSString *)payload
227227{
228- return ([payload hasPrefix: @" MT:" ]) ? [self initWithQRCode: payload] : [self initWithManualPairingCode: payload];
228+ return [self initWithPayload: payload error: nil ];
229+ }
230+
231+ - (nullable instancetype )initWithPayload : (NSString *)payload error : (NSError * __autoreleasing *)error
232+ {
233+ // Dispatch by the "MT:" prefix and preserve the fine-grained underlying
234+ // error code (unlike +setupPayloadWithOnboardingPayload:error:, which
235+ // flattens to MTRErrorCodeInvalidArgument).
236+ return ([payload hasPrefix: @" MT:" ]) ? [self initWithQRCode: payload error: error]
237+ : [self initWithManualPairingCode: payload error: error];
229238}
230239
231240- (CHIP_ERROR)initializeFromQRCode : (NSString *)qrCode validate : (BOOL )validate
@@ -269,17 +278,32 @@ - (CHIP_ERROR)initializeFromQRCode:(NSString *)qrCode validate:(BOOL)validate
269278}
270279
271280- (nullable instancetype )initWithQRCode : (NSString *)qrCodePayload
281+ {
282+ return [self initWithQRCode: qrCodePayload error: nil ];
283+ }
284+
285+ - (nullable instancetype )initWithQRCode : (NSString *)qrCodePayload
286+ error : (NSError * __autoreleasing *)error
272287{
273288 self = [super init ];
274289 CHIP_ERROR err = [self initializeFromQRCode: qrCodePayload validate: YES ];
275290 if (err != CHIP_NO_ERROR) {
291+ if (error) {
292+ *error = [MTRError errorForCHIPErrorCode: err];
293+ }
276294 return nil ;
277295 }
278296
279297 return self;
280298}
281299
282300- (nullable instancetype )initWithManualPairingCode : (NSString *)manualCode
301+ {
302+ return [self initWithManualPairingCode: manualCode error: nil ];
303+ }
304+
305+ - (nullable instancetype )initWithManualPairingCode : (NSString *)manualCode
306+ error : (NSError * __autoreleasing *)error
283307{
284308 self = [super init ];
285309
@@ -288,10 +312,16 @@ - (nullable instancetype)initWithManualPairingCode:(NSString *)manualCode
288312 CHIP_ERROR err = parser.populatePayload (_payload);
289313 if (err != CHIP_NO_ERROR) {
290314 MTR_LOG_ERROR (" Failed to parse Manual Pairing Code: %" CHIP_ERROR_FORMAT, err.Format ());
315+ if (error) {
316+ *error = [MTRError errorForCHIPErrorCode: err];
317+ }
291318 return nil ;
292319 }
293320 if (!_payload.isValidManualCode (chip::PayloadContents::ValidationMode::kConsume )) {
294321 MTR_LOG_ERROR (" Invalid Manual Pairing Code" );
322+ if (error) {
323+ *error = [MTRError errorForCHIPErrorCode: CHIP_ERROR_INVALID_ARGUMENT];
324+ }
295325 return nil ;
296326 }
297327
@@ -678,7 +708,13 @@ - (instancetype)initWithCoder:(NSCoder *)coder
678708 // did not encode it. When present, it carries almost the entire state of the object.
679709 NSString * qrCode = [coder decodeObjectOfClass: NSString .class forKey :MTRSetupPayloadCodingKeyQRCode];
680710 if (qrCode != nil ) {
681- [self initializeFromQRCode: qrCode validate: NO ];
711+ // Intentionally fault-tolerant: don't -failWithError: on parse failure
712+ // (older archives may hold payloads we no longer parse cleanly). Just
713+ // log a breadcrumb and fall through to the fields decoded below.
714+ CHIP_ERROR err = [self initializeFromQRCode: qrCode validate: NO ];
715+ if (err != CHIP_NO_ERROR) {
716+ MTR_LOG_ERROR (" initWithCoder: failed to restore QR code from archive: %" CHIP_ERROR_FORMAT, err.Format ());
717+ }
682718 } else {
683719 self.version = [coder decodeObjectOfClass: NSNumber .class forKey :MTRSetupPayloadCodingKeyVersion];
684720 self.vendorID = [coder decodeObjectOfClass: NSNumber .class forKey :MTRSetupPayloadCodingKeyVendorID];
@@ -781,8 +817,13 @@ + (instancetype)new
781817+ (MTRSetupPayload * _Nullable)setupPayloadWithOnboardingPayload : (NSString *)onboardingPayload
782818 error : (NSError * __autoreleasing *)error
783819{
784- MTRSetupPayload * payload = [[MTRSetupPayload alloc ] initWithPayload: onboardingPayload];
785- if (!payload && error) {
820+ // Deprecated surface: flatten every parse failure to
821+ // MTRErrorCodeInvalidArgument, preserving the legacy contract for callers
822+ // that switch on it. Callers wanting the fine-grained code should migrate
823+ // to -initWithPayload:error:. (We pass nil for the underlying error so its
824+ // localized description can't contradict the flattened code we report.)
825+ MTRSetupPayload * payload = [[MTRSetupPayload alloc ] initWithPayload: onboardingPayload error: nil ];
826+ if (payload == nil && error != nil ) {
786827 *error = [MTRError errorWithCode: MTRErrorCodeInvalidArgument];
787828 }
788829 return payload;
0 commit comments