fix account selection after first account creation

Account wasn't properly set due to a not handled exception. This is no
longer the case as the first account of the list is now selected by
default when no account is yet saved in user configuration.

Moreover, having an empty AccountModel shouldn't cause any crash as
this case is now handled with exceptions.

Change-Id: Ibb3e386d80e946b2507916b3e44cb28af62bc3c7
Reviewed-by: Kateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com>
diff --git a/src/ChooseAccountVC.mm b/src/ChooseAccountVC.mm
index ec7fff0..dce90d4 100644
--- a/src/ChooseAccountVC.mm
+++ b/src/ChooseAccountVC.mm
@@ -94,17 +94,21 @@
 
 -(const lrc::api::account::Info&) selectedAccount
 {
-    const auto& account = [accountSelectionManager_ savedAccount];
-    if(account.profileInfo.type == lrc::api::profile::Type::INVALID){
-        try {
-            auto accountId = accMdl_->getAccountList().at(0);
-            const auto& fallbackAccount = accMdl_->getAccountInfo(accMdl_->getAccountList().at(0));
+    try {
+        return [accountSelectionManager_ savedAccount];
+    } catch (std::out_of_range& e) { // Is thrown when no account are saved yet
+        auto accountList = accMdl_->getAccountList();
+        if (!accountList.empty()) {
+            const auto& fallbackAccount = accMdl_->getAccountInfo(accountList.at(0));
             return fallbackAccount;
-        } catch (std::out_of_range& e) { // Is thrown if account model has no account. We then return an invalid account
-            return account;
+        } else {
+            NSException* noAccEx = [NSException
+                                    exceptionWithName:@"NoAccountException"
+                                    reason:@"No account in AccountModel"
+                                    userInfo:nil];
+            @throw noAccEx;
         }
     }
-    return account;
 }
 
 -(void) updateMenu {
@@ -144,14 +148,19 @@
 
 -(void) updatePhoto
 {
-    auto& account = [self selectedAccount];
-    if(account.profileInfo.type == lrc::api::profile::Type::INVALID)
-        return;
+    @try {
+        auto& account = [self selectedAccount];
+        if(account.profileInfo.type == lrc::api::profile::Type::INVALID)
+            return;
 
-    QByteArray ba = QByteArray::fromStdString(account.profileInfo.avatar);
+        QByteArray ba = QByteArray::fromStdString(account.profileInfo.avatar);
 
-    QVariant photo = GlobalInstances::pixmapManipulator().personPhoto(ba);
-    [profileImage setImage:QtMac::toNSImage(qvariant_cast<QPixmap>(photo))];
+        QVariant photo = GlobalInstances::pixmapManipulator().personPhoto(ba);
+        [profileImage setImage:QtMac::toNSImage(qvariant_cast<QPixmap>(photo))];
+    }
+    @catch (NSException *ex) {
+        NSLog(@"Caught exception %@: %@", [ex name], [ex reason]);
+    }
 }
 
 -(NSString*) nameForAccount:(const lrc::api::account::Info&) account {
@@ -209,11 +218,16 @@
         return;
     }
     [self.view setHidden:NO];
-    auto& account = [self selectedAccount];
-    if(account.profileInfo.type == lrc::api::profile::Type::INVALID){
-        return;
+    @try {
+        auto& account = [self selectedAccount];
+        if(account.profileInfo.type == lrc::api::profile::Type::INVALID){
+            return;
+        }
+        [accountSelectionButton selectItemWithTitle:[self itemTitleForAccount:account]];
     }
-    [accountSelectionButton selectItemWithTitle:[self itemTitleForAccount:account]];
+    @catch (NSException *ex) {
+        NSLog(@"Caught exception %@: %@", [ex name], [ex reason]);
+    }
 }
 
 #pragma mark - NSPopUpButton item selection