Repository: CodeEditApp/CodeEdit
File: CodeEdit/Features/WindowCommands/FileCommands.swift (Swift)

Code before:

        let fileManager = FileManager.default
        let workspaceName = windowController.workspace?.workspaceFileManager?.folderUrl.lastPathComponent

        var hashedWorkspaceName = "no-workspace".md5()
        if let workspaceName {
            hashedWorkspaceName = workspaceName.md5()
        }

        let tempStorageDir = CodeEditApp.applicationSupportURL
            .appendingPathComponent("workspaces/\(hashedWorkspaceName)", isDirectory: true)

        do {
            try createDirectoryIfNotExists(atPath: tempStorageDir.path)

            let probableNumber = try predictNextTempFileNumber(in: tempStorageDir)

            let filename = "untitled-\(probableNumber)"
            let tempFileURL = tempStorageDir.appending(path: filename)
            fileManager.createFile(atPath: tempFileURL.path(percentEncoded: false), contents: nil)
            print("Saving new file at: \(tempFileURL.path)")

            let file = CEWorkspaceFile(url: tempFileURL)
            windowController.workspace?.editorManager.openTab(item: file)
        } catch {
            print(error)
            let alert = NSAlert()
            alert.messageText = "Error"
            alert.informativeText = "An error occurred. Please try again later."
            alert.alertStyle = .critical
            alert.addButton(withTitle: "OK")
            alert.runModal()
        }
    }

    private func getCurrentCEWindowController() -> CodeEditWindowController? {
        return NSApp.keyWindow?.windowController as? CodeEditWindowController
    }

    /// Creates a directory at a given path if it's not present
    /// - Parameter path: The path where the directory should be created
    private func createDirectoryIfNotExists(atPath path: String) throws {
        let fileManager = FileManager.default

        var isDir: ObjCBool = true
        let directoryExists = fileManager.fileExists(atPath: path, isDirectory: &isDir)

        if !directoryExists {
            try fileManager.createDirectory(atPath: path, withIntermediateDirectories: true)
        }
    }

Review comment (suggestion):
```suggestion
            alert.informativeText = "An error occurred: \(error.localizedDescription)"
```

Code after:
        var hashedWorkspaceName = (workspaceName ?? "no-workspace").md5()

        let tempStorageDir = CodeEditApp.applicationSupportURL
            .appendingPathComponent("workspaces/\(hashedWorkspaceName)", isDirectory: true)

        do {
            try createDirectoryIfNotExists(atPath: tempStorageDir.path)

            let probableNumber = try predictNextTempFileNumber(in: tempStorageDir)

            let filename = "untitled-\(probableNumber)"
            let tempFileURL = tempStorageDir.appending(path: filename)
            fileManager.createFile(atPath: tempFileURL.path(percentEncoded: false), contents: nil)
            print("Saving new file at: \(tempFileURL.path)")

            let file = CEWorkspaceFile(url: tempFileURL)
            windowController.workspace?.editorManager.openTab(item: file)
        } catch {
            print(error)
            let alert = NSAlert()
            alert.messageText = "Error"
            alert.informativeText = "An error occurred: \(error.localizedDescription)."
            alert.alertStyle = .critical
            alert.addButton(withTitle: "OK")
            alert.runModal()
        }
    }

    private func getCurrentCEWindowController() -> CodeEditWindowController? {
        return NSApp.keyWindow?.windowController as? CodeEditWindowController
    }

    /// Creates a directory at a given path if it's not present
    /// - Parameter path: The path where the directory should be created
    private func createDirectoryIfNotExists(atPath path: String) throws {
        let fileManager = FileManager.default

        var isDir: ObjCBool = true
        let directoryExists = fileManager.fileExists(atPath: path, isDirectory: &isDir)

        if !directoryExists {
            try fileManager.createDirectory(atPath: path, withIntermediateDirectories: true)
        }
    }

    private func predictNextTempFileNumber(in tempStorageDir: URL) throws -> Int {
        let fileManager = FileManager.default

        let fileNumbers = try fileManager.contentsOfDirectory(atPath: tempStorageDir.path)
            .compactMap { return $0.split(separator: "-").last }
            .compactMap { Int($0) }