react-native-spotify-remote icon indicating copy to clipboard operation
react-native-spotify-remote copied to clipboard

Terminating app due to uncaught exception 'NSInvalidArgumentException'

Open yungbigos opened this issue 5 years ago • 12 comments

Hi there! I ran into a weird issue that I hope somebody can help me with.

So, in my AppDelegate.m, I had to adjust this function slightly to enable a share extension and this library at the same time. This seems straightforward to me, but then again I have little to no experience in Obj-C.

- (BOOL)application:(UIApplication *)application
   openURL:(NSURL *)url
   options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
  
  //  Check if the passed url is a <MY_APP_URL_SCHEME>://
  NSString *urlString = url.absoluteString;
  

  //  If it contains <MY_APP_URL_SCHEME>://spotifyauth handle it with react-native-spotify-remote
  if ([urlString hasPrefix:@"<MY_APP_URL_SCHEME>://spotifyauth"]) {
    NSLog(@"Entered with the following string: %@s", urlString);
    return [[RNSpotifyRemoteAuth sharedInstance] application:application openURL:url options:options];
  }
  // Else, go into the ShareMenuManager
  return [ShareMenuManager application:application openURL:url options:options];
  
}

Now, after the app switches to Spotify for auth and comes back to my app, I receive the following exception in Xcode:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[0]'
terminating with uncaught exception of type NSException

Can anyone spot an obvious mistake? @cjam maybe? I'd appreciate any help!

Cheers

yungbigos avatar Mar 04 '21 16:03 yungbigos

This is the log just before the crash occurs:

Entered with the following string: <MY_APP_URL_SCHEME>://spotifyauth/...

2021-03-04 17:22:03.696248+0100 XXX [3499:353183] -[RNSpotifyRemoteAuth application:openURL:options:] [Line 86] Setting application openURL and options on session manager 

yungbigos avatar Mar 04 '21 16:03 yungbigos

Any ideas? This effectively crashes my every time the auth callback comes back to my app 😥 @cjam @reteps If you have only the slightest idea why this might happen, please let me know. Any feedback is highly appreciated!

yungbigos avatar Mar 09 '21 16:03 yungbigos

"attempt to insert nil object from objects[0]" -> something is nil that should not be nil. Log out everything, and find out what is nil.

reteps avatar Mar 09 '21 17:03 reteps

Hey @yannikw23 , sorry for the delay. I'm not sure what might be causing this, however, I did try to make the URL handling code follow the same kind signature as other Auth/Login flow libraries, so it will return a boolean whether or not it was able to handle the URL:

https://github.com/cjam/react-native-spotify-remote/blob/528e0e93c7648959b28806da5ba7f0078d5911ac/ios/RNSpotifyRemoteAuth.m#L80-L102

I'm not sure where the nil reference might be coming from, other than perhaps where I'm building the error description from the URL components. Hm.. Are there any other logs in XCode? You could try debugging it by putting a break point in your AppDelegate and stepping into the code that is handling the URL. Perhaps you'd be able to inspect some of the variable values. At least it would give more details on the issue.

cjam avatar Mar 10 '21 05:03 cjam

Hi @cjam and @reteps, thanks so much for getting back to me!

I tried to log everything and I cannot seem to find any irregularities...

if(_sessionManager != nil){
        DLog(@"Setting application openURL and options on session manager");
        NSLog(@"options in Lib: %@",options);
        NSURLComponents *urlComponents = [NSURLComponents componentsWithURL:URL resolvingAgainstBaseURL:TRUE];
        NSLog(@"urlComponents in Lib: %@",urlComponents);
        NSURLQueryItem * errorDescription = [[[urlComponents queryItems] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"name == %@", SPTAppRemoteErrorDescriptionKey]] firstObject];
        NSLog(@"errorDescription in Lib: %@",errorDescription);
        NSURLQueryItem * errorType = [[[urlComponents queryItems] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"name == %@",@"error"]] firstObject];
        NSLog(@"errorType in Lib: %@",errorType);
        // If there was an error we should reject our pending Promise
        if(errorDescription){
            [RNSpotifyRemotePromise rejectCompletions:_sessionManagerCallbacks error:[RNSpotifyRemoteError errorWithCode:errorType.value message:errorDescription.value]];
            returnVal = NO;
        }
        returnVal = [_sessionManager application:application openURL:URL options:options];
    }

errorDescription and errorType are both (null) and urlComponents seems to be containing the right options:

Entered with the following string: <MY_APP_URL_SCHEME>://spotifyauth/?code=AQ...Dh&spotify_version=8.6.6.1099s
2021-03-10 13:30:13.821880+0100 XXX[14874:1624248] -[RNSpotifyRemoteAuth application:openURL:options:] [Line 86] Setting application openURL and options on session manager
2021-03-10 13:30:13.822241+0100 XXX[14874:1624248] options in Lib: {
    UIApplicationOpenURLOptionsOpenInPlaceKey = 0;
}
2021-03-10 13:30:13.822525+0100 XXX[14874:1624248] urlComponents in Lib: <NSURLComponents 0x281119d30> {scheme = <MY_APP_URL_SCHEME>, user = (null), password = (null), host = spotifyauth, port = (null), path = /, query = code=AQ...Dh&spotify_version=8.6.6.1099, fragment = (null)}
2021-03-10 13:30:13.823454+0100 XXX[14874:1624248] errorDescription in Lib: (null)
2021-03-10 13:30:13.823616+0100 XXX[14874:1624248] errorType in Lib: (null)

2021-03-10 13:30:39.832578+0100 XXX[14874:1625672] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[0]'
*** First throw call stack:
(0x1912f19d8 0x1a5677b54 0x19135bd98 0x1913671f4 0x1911ec8a8 0x1911dfc04 0x1050e0cc8 0x1050de0f0 0x1050e3594 0x1050e3cb8 0x1050dcfc8 0x10554ef8c 0x10554ec78 0x1918e7728 0x1918f81f8 0x107b8bce4 0x107b8d528 0x107b945ac 0x107b95268 0x107ba0a5c 0x1d9a815a4 0x1d9a84874)
libc++abi.dylib: terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[0]'
terminating with uncaught exception of type NSException

Not sure where to set a break point. I tried setting it here in my AppDelegate, but I am not too sure where to go from here: --> return [[RNSpotifyRemoteAuth sharedInstance] application:application openURL:url options:options];

yungbigos avatar Mar 10 '21 12:03 yungbigos

It seems that user and password are (null) in NSURLComponents. Not too sure what to do about that though.

yungbigos avatar Mar 10 '21 12:03 yungbigos

So once you have your breakpoint set, it should get hit when the auth callback is made back from spotify. At this point, you can step into the RNSpotifyRemoteAuth. I would recommend setting some breakpoints here in the URL handling method.

https://github.com/cjam/react-native-spotify-remote/blob/528e0e93c7648959b28806da5ba7f0078d5911ac/ios/RNSpotifyRemoteAuth.m#L84

You can also set a breakpoint within XCode for uncaught exceptions which you could then look at the stack trace to try and figure out where it's coming from.

cjam avatar Mar 16 '21 14:03 cjam

I'll have to check and see what the value of user and password on mine are. Perhaps they're always null.

cjam avatar Mar 16 '21 14:03 cjam

In my case I got the error because I wasn't returning all data (ex. refresh_token, scope) from the swap endpoint in my server. As I was only sending the access_token:

res.status(200).json({ access_token: spotifyRes.data.access_token });

I believe this caused the values in RNSpotifyRemoteConvert.m to be nil (don't quote me though, I have no experience in Objective C): https://github.com/cjam/react-native-spotify-remote/blob/528e0e93c7648959b28806da5ba7f0078d5911ac/ios/RNSpotifyRemoteConvert.m#L206-L212

I fixed this error by forwarding all data back from the swap endpoint:

res.status(200).json(spotifyRes);

rz-0605 avatar Mar 27 '21 21:03 rz-0605

@rz-0605 great. Perhaps I will close this then as it doesn't seem to be an issue with this library.

cjam avatar Apr 04 '21 13:04 cjam

@rz-0605 @cjam Thanks a lot guys! Sorry for my late reply - will test this today!

yungbigos avatar Apr 06 '21 08:04 yungbigos

Quick follow up: Searched for the error in all sort of different places. In the end the issue was indeed in my server. I was not responding to the swap request properly (a merge conflict deleted 2 lines of code 3 months ago). The way it is in the example-server is actually correct - so the error is entirely on my side.

Thanks a lot for pointing me into the right direction!!

yungbigos avatar May 04 '21 21:05 yungbigos