Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-1975] JSONSerialization.jsonObject Error Not Caught In Do Catch Statement #4559

Closed
swift-ci opened this issue Jul 3, 2016 · 1 comment

Comments

@swift-ci
Copy link
Contributor

swift-ci commented Jul 3, 2016

Previous ID SR-1975
Radar None
Original Reporter agnosticdev (JIRA User)
Type Bug
Status Resolved
Resolution Invalid

Attachment: Download

Environment

OS X El Capitan Version 10.11.5. MacBook Pro. Xcode 8.0 beta (8S128d), Swift 3.0

Additional Detail from JIRA
Votes 0
Component/s Foundation, LLDB for Swift
Labels Bug, RunTimeCrash, Runtime
Assignee None
Priority Medium

md5: 0eab5249a98d19b74d568f408fcff47a

Issue Description:

I ran across an error that I thought might be caught in a do catch statement using the try keyword but the error was never caught and instead threw a fatal runtime error that crashed my program. The error came from using the jsonObject(with:options:) method on the JSONSerialization object when no valid NSData was set to a variable. I was expecting that when NSData was not correctly set and the method call failed that the method would throw -> AnyObject to be caught in the do catch statement like the documentation says, but instead my do catch statement did not catch the thrown error and it crashed my program.

Here is the documentation I am referring to: https://developer.apple.com/reference/foundation/nsjsonserialization/1415493-jsonobject

My issue can obviously be solved by ensuring that there is valid NSData/data before using it in the method call, but my real bug to report here is the fact that I was under the impression that based upon the documentation that the jsonObject(with:options:) method call would throw a catchable AnyObject error.

Here is a code snippet illustration the bug:

    func getWeatherInfo(fromCurrentURL URLString: String){
        
        // URLString = "https://www.badurl.url"
        guard let locationURL = NSURL(string: currentURL) else {
            print("Unable to form location URL")
            return
        }
        // To ensure weather data is set correctly use an if let statement to 
        // validate that NSData is set before using it
        //if let weatherData = NSData(contentsOf: locationURL as URL) {
            do {
                // Here we are optimistically assuming that weather 
                // data is present and assigning it to the variable
                let weatherData = NSData(contentsOf: locationURL as URL)
                // Because weatherData is optimistically set above and we 
                // guarantee that is is present in the jsonObject
                // call using the as! Data statement then the 
                // try statement throws a fatal runtime error and it is
                // not caught in the do catch block.  Why is it not
                // caught in the do catch block?
                let json = try JSONSerialization.jsonObject(with: weatherData as! Data, options: JSONSerialization.ReadingOptions.mutableContainers)
                
                if let currentDict = json as? NSDictionary {
                    if let currentArray = currentDict["currently"] as? NSDictionary {
                        // Data is parsed here
                    }else  {
                        print("Unable to deserialze")
                    }
                }
            }catch let error as NSError {
                print("NSError CAUGHT ERROR \(error) -- Never executed")
            } catch {
                print("Catch Any Other Errors -- Never executed")
            }
        //}
    }
@belkadan
Copy link

belkadan commented Jul 5, 2016

You're not even getting to the JSON deserialization. Your NSData instance is nil, and the as! cast is unwrapping it without checking. That's a programmer error rather than a recoverable run-time error.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 6, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants