You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shindig.apache.org by jo...@apache.org on 2010/02/18 12:29:20 UTC

svn commit: r911336 - /shindig/trunk/java/common/src/main/java/org/apache/shindig/common/uri/Uri.java

Author: johnh
Date: Thu Feb 18 11:29:20 2010
New Revision: 911336

URL: http://svn.apache.org/viewvc?rev=911336&view=rev
Log:
This CL introduces a trivial wrapper UriException class, which wraps
IllegalArgumentException (itself a RuntimeException), and changes the Uri class
to throw it exclusively rather than IllegalArgumentException or RuntimeException
directly.

This allows calling code to wrap relevant methods in semantically-bounded
try/catch blocks without our having to have Uri's methods throw a checked
exception, which in turn would necessitate large numbers of downstream changes.

In this way, we can slowly phase in use of UriException, to the point that
eventually we may find it relatively easy to change this exception to a checked
version of itself with minimal trouble.


Modified:
    shindig/trunk/java/common/src/main/java/org/apache/shindig/common/uri/Uri.java

Modified: shindig/trunk/java/common/src/main/java/org/apache/shindig/common/uri/Uri.java
URL: http://svn.apache.org/viewvc/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/uri/Uri.java?rev=911336&r1=911335&r2=911336&view=diff
==============================================================================
--- shindig/trunk/java/common/src/main/java/org/apache/shindig/common/uri/Uri.java (original)
+++ shindig/trunk/java/common/src/main/java/org/apache/shindig/common/uri/Uri.java Thu Feb 18 11:29:20 2010
@@ -97,7 +97,17 @@
    * @return A new Uri, parsed into components.
    */
   public static Uri parse(String text) {
-    return parser.parse(text);
+    try {
+      return parser.parse(text);
+    } catch (IllegalArgumentException e) {
+      // This occurs all the time. Wrap the exception in a Uri-specific
+      // exception, yet one that remains a RuntimeException, so that
+      // callers may catch a specific exception rather than a blanket
+      // Exception, as a compromise between throwing a checked exception
+      // here (forcing wide-scale refactoring across the code base) and
+      // forcing users to simply catch abstract Exceptions here and there.
+      throw new UriException(e);
+    }
   }
 
   /**
@@ -105,7 +115,7 @@
    */
   public static Uri fromJavaUri(URI uri) {
     if (uri.isOpaque()) {
-      throw new IllegalArgumentException("No support for opaque Uris " + uri.toString());
+      throw new UriException("No support for opaque Uris " + uri.toString());
     }
     return new UriBuilder()
         .setScheme(uri.getScheme())
@@ -124,7 +134,7 @@
       return new URI(toString());
     } catch (URISyntaxException e) {
       // Shouldn't ever happen.
-      throw new IllegalArgumentException(e);
+      throw new UriException(e);
     }
   }
 
@@ -185,7 +195,7 @@
     if (StringUtils.isEmpty(uri.authority) &&
         StringUtils.isEmpty(uri.path) &&
         StringUtils.isEmpty(uri.query)) {
-      throw new IllegalArgumentException("Invalid scheme-specific part");
+      throw new UriException("Invalid scheme-specific part");
     }
   }
 
@@ -387,4 +397,14 @@
     if (!(obj instanceof Uri)) {return false;}
     return Objects.equal(text, ((Uri)obj).text);
   }
+  
+  public static class UriException extends IllegalArgumentException {
+    private UriException(Exception e) {
+      super(e);
+    }
+    
+    private UriException(String msg) {
+      super(msg);
+    }
+  }
 }