Przeglądaj źródła

Issue 1703: Move exception handling out from Identity


git-svn-id: http://svn.dmdirc.com/trunk@4830 00569f92-eb28-0410-84fd-f71c24880f
tags/0.6.3m1rc1
Chris Smith 15 lat temu
rodzic
commit
7d9c8407d5

+ 28
- 32
src/com/dmdirc/config/Identity.java Wyświetl plik

@@ -543,14 +543,15 @@ public class Identity extends ConfigSource implements Serializable,
543 543
      *
544 544
      * @param settings The settings to populate the identity with
545 545
      * @return A new identity containing the specified properties
546
+     * @throws IOException If the file cannot be created
547
+     * @throws InvalidIdentityFileException If the settings are invalid
546 548
      * @since 0.6.3
547 549
      */
548
-    protected static Identity createIdentity(final Map<String, Map<String, String>> settings) {
550
+    protected static Identity createIdentity(final Map<String, Map<String, String>> settings)
551
+            throws IOException, InvalidIdentityFileException {
549 552
         if (!settings.containsKey(DOMAIN) || !settings.get(DOMAIN).containsKey("name")
550 553
                 || settings.get(DOMAIN).get("name").isEmpty()) {
551
-            Logger.appError(ErrorLevel.LOW, "createIdentity called with invalid identity",
552
-                    new InvalidIdentityFileException("identity.name is not set"));
553
-            return null;
554
+            throw new InvalidIdentityFileException("identity.name is not set");
554 555
         }
555 556
 
556 557
         final String fs = System.getProperty("file.separator");
@@ -570,32 +571,12 @@ public class Identity extends ConfigSource implements Serializable,
570 571
             configFile.addDomain(entry.getKey(), entry.getValue());
571 572
         }
572 573
 
573
-        try {
574
-            configFile.write();
575
-        } catch (IOException ex) {
576
-            Logger.userError(ErrorLevel.MEDIUM,
577
-                    "Unable to write new identity file: " + ex.getMessage());
578
-            return null;
579
-        }
574
+        configFile.write();
580 575
 
581
-        try {
582
-            final Identity identity = new Identity(file, false);
583
-            IdentityManager.addIdentity(identity);
576
+        final Identity identity = new Identity(file, false);
577
+        IdentityManager.addIdentity(identity);
584 578
 
585
-            return identity;
586
-        } catch (MalformedURLException ex) {
587
-            Logger.userError(ErrorLevel.MEDIUM,
588
-                    "Unable to open new identity file: " + ex.getMessage());
589
-            return null;
590
-        } catch (InvalidIdentityFileException ex) {
591
-            Logger.userError(ErrorLevel.MEDIUM,
592
-                    "Unable to open new identity file: " + ex.getMessage());
593
-            return null;
594
-        } catch (IOException ex) {
595
-            Logger.userError(ErrorLevel.MEDIUM,
596
-                    "Unable to open new identity file: " + ex.getMessage());
597
-            return null;
598
-        }
579
+        return identity;
599 580
     }
600 581
 
601 582
     /**
@@ -603,15 +584,23 @@ public class Identity extends ConfigSource implements Serializable,
603 584
      *
604 585
      * @param target The target for the new identity
605 586
      * @return An empty identity for the specified target
587
+     * @throws IOException if the file can't be written
588
+     * @see #createIdentity(java.util.Map)
606 589
      */
607
-    public static Identity buildIdentity(final ConfigTarget target) {
590
+    public static Identity buildIdentity(final ConfigTarget target)
591
+            throws IOException {
608 592
         final Map<String, Map<String, String>> settings
609 593
                 = new HashMap<String, Map<String, String>>();
610 594
         settings.put(DOMAIN, new HashMap<String, String>(2));
611 595
         settings.get(DOMAIN).put("name", target.getData());
612 596
         settings.get(DOMAIN).put(target.getTypeName(), target.getData());
613 597
 
614
-        return createIdentity(settings);
598
+        try {
599
+            return createIdentity(settings);
600
+        } catch (InvalidIdentityFileException ex) {
601
+            Logger.appError(ErrorLevel.MEDIUM, "Unable to create identity", ex);
602
+            return null;
603
+        }
615 604
     }
616 605
 
617 606
     /**
@@ -620,8 +609,10 @@ public class Identity extends ConfigSource implements Serializable,
620 609
      *
621 610
      * @param name The name of the profile to create
622 611
      * @return A new profile with the specified name
612
+     * @throws IOException If the file can't be written
613
+     * @see #createIdentity(java.util.Map)
623 614
      */
624
-    public static Identity buildProfile(final String name) {
615
+    public static Identity buildProfile(final String name) throws IOException {
625 616
         final Map<String, Map<String, String>> settings
626 617
                 = new HashMap<String, Map<String, String>>();
627 618
         settings.put(DOMAIN, new HashMap<String, String>(1));
@@ -633,7 +624,12 @@ public class Identity extends ConfigSource implements Serializable,
633 624
         settings.get("profile").put("nickname", nick);
634 625
         settings.get("profile").put("realname", nick);
635 626
 
636
-        return createIdentity(settings);
627
+        try {
628
+            return createIdentity(settings);
629
+        } catch (InvalidIdentityFileException ex) {
630
+            Logger.appError(ErrorLevel.MEDIUM, "Unable to create identity", ex);
631
+            return null;
632
+        }
637 633
     }
638 634
 
639 635
 }

+ 27
- 7
src/com/dmdirc/config/IdentityManager.java Wyświetl plik

@@ -76,7 +76,11 @@ public final class IdentityManager {
76 76
         loadConfig();
77 77
         
78 78
         if (getProfiles().size() == 0) {
79
-            Identity.buildProfile("Default Profile");
79
+            try {
80
+                Identity.buildProfile("Default Profile");
81
+            } catch (IOException ex) {
82
+                Logger.userError(ErrorLevel.FATAL, "Unable to write default profile", ex);
83
+            }
80 84
         }
81 85
         
82 86
         // Set up the identity used for the addons defaults
@@ -387,6 +391,7 @@ public final class IdentityManager {
387 391
     /**
388 392
      * Retrieves the config for the specified channel@network. The config is
389 393
      * created if it doesn't exist.
394
+     *
390 395
      * @param network The name of the network
391 396
      * @param channel The name of the channel
392 397
      * @return A config source for the channel
@@ -395,8 +400,7 @@ public final class IdentityManager {
395 400
         "The specified network is non-null and not empty",
396 401
         "The specified channel is non-null and not empty"
397 402
     })
398
-    public static Identity getChannelConfig(final String network,
399
-            final String channel) {
403
+    public static Identity getChannelConfig(final String network, final String channel) {
400 404
         if (network == null || network.isEmpty()) {
401 405
             throw new IllegalArgumentException("getChannelConfig called "
402 406
                     + "with null or empty network\n\nNetwork: " + network);
@@ -421,13 +425,19 @@ public final class IdentityManager {
421 425
         // We need to create one
422 426
         final ConfigTarget target = new ConfigTarget();
423 427
         target.setChannel(myTarget);
424
-        
425
-        return Identity.buildIdentity(target);
428
+
429
+        try {
430
+            return Identity.buildIdentity(target);
431
+        } catch (IOException ex) {
432
+            Logger.userError(ErrorLevel.HIGH, "Unable to create channel identity", ex);
433
+            return null;
434
+        }
426 435
     }
427 436
     
428 437
     /**
429 438
      * Retrieves the config for the specified network. The config is
430 439
      * created if it doesn't exist.
440
+     *
431 441
      * @param network The name of the network
432 442
      * @return A config source for the network
433 443
      */
@@ -453,7 +463,12 @@ public final class IdentityManager {
453 463
         final ConfigTarget target = new ConfigTarget();
454 464
         target.setNetwork(myTarget);
455 465
         
456
-        return Identity.buildIdentity(target);
466
+        try {
467
+            return Identity.buildIdentity(target);
468
+        } catch (IOException ex) {
469
+            Logger.userError(ErrorLevel.HIGH, "Unable to create network identity", ex);
470
+            return null;
471
+        }
457 472
     }
458 473
     
459 474
     /**
@@ -485,7 +500,12 @@ public final class IdentityManager {
485 500
         final ConfigTarget target = new ConfigTarget();
486 501
         target.setServer(myTarget);
487 502
         
488
-        return Identity.buildIdentity(target);
503
+        try {
504
+            return Identity.buildIdentity(target);
505
+        } catch (IOException ex) {
506
+            Logger.userError(ErrorLevel.HIGH, "Unable to create network identity", ex);
507
+            return null;
508
+        }
489 509
     }    
490 510
     
491 511
 }

+ 6
- 1
src/com/dmdirc/ui/swing/dialogs/profiles/Profile.java Wyświetl plik

@@ -25,6 +25,7 @@ package com.dmdirc.ui.swing.dialogs.profiles;
25 25
 import com.dmdirc.config.Identity;
26 26
 import com.dmdirc.config.IdentityManager;
27 27
 
28
+import java.io.IOException;
28 29
 import java.util.Arrays;
29 30
 import java.util.List;
30 31
 
@@ -291,7 +292,11 @@ public class Profile {
291 292
             }
292 293
 
293 294
             if (profile == null) {
294
-                profile = Identity.buildProfile(name);
295
+                try {
296
+                    profile = Identity.buildProfile(name);
297
+                } catch (IOException ex) {
298
+                    // TODO: ??
299
+                }
295 300
             }
296 301
 
297 302
             profile.setOption("identity", "name", name);

Ładowanie…
Anuluj
Zapisz