Add bad input to Uri in exception messages
authorRajat Goel <rajatgoel2010@fb.com>
Wed, 2 Oct 2013 03:07:23 +0000 (20:07 -0700)
committerPeter Griess <pgriess@fb.com>
Tue, 15 Oct 2013 01:44:30 +0000 (18:44 -0700)
Summary:
It helps if you can directly see the offending URI from the error
message (specially when you dont want to handle errors and let process crash).

@override-unit-failures

Test Plan: unit-tests

Reviewed By: andrei.alexandrescu@fb.com

FB internal diff: D993025

folly/Uri.cpp
folly/test/UriTest.cpp

index 9b1151a8f114e3f37de5266bfa1553da7ce1aaf0..9d4ad2acf37535f7ba5487bdeea051b45bf73ec2 100644 (file)
@@ -47,7 +47,7 @@ Uri::Uri(StringPiece str) : port_(0) {
 
   boost::cmatch match;
   if (UNLIKELY(!boost::regex_match(str.begin(), str.end(), match, uriRegex))) {
-    throw std::invalid_argument("invalid URI");
+    throw std::invalid_argument(to<std::string>("invalid URI ", str));
   }
 
   scheme_ = submatch(match, 1);
@@ -74,7 +74,9 @@ Uri::Uri(StringPiece str) : port_(0) {
                             authority.second,
                             authorityMatch,
                             authorityRegex)) {
-      throw std::invalid_argument("invalid URI authority");
+      throw std::invalid_argument(
+          to<std::string>("invalid URI authority ",
+                          StringPiece(authority.first, authority.second)));
     }
 
     StringPiece port(authorityMatch[4].first, authorityMatch[4].second);
index e4fe76996bc8fa27cf5552da97d10aae7d088708..cb37f075c7982267a36cfd04431ebd4cb08e3061 100644 (file)
@@ -16,6 +16,7 @@
 
 #include "folly/Uri.h"
 
+#include <boost/algorithm/string.hpp>
 #include <glog/logging.h>
 #include <gtest/gtest.h>
 
@@ -221,6 +222,25 @@ TEST(Uri, Simple) {
     EXPECT_EQ(s, u.fbstr());
   }
 
-  EXPECT_THROW({Uri("2http://www.facebook.com/");},
-               std::invalid_argument);
+  {
+    fbstring s("2http://www.facebook.com");
+
+    try {
+      Uri u(s);
+      CHECK(false) << "Control should not have reached here";
+    } catch (const std::invalid_argument& ex) {
+      EXPECT_TRUE(boost::algorithm::ends_with(ex.what(), s));
+    }
+  }
+
+  {
+    fbstring s("www[facebook]com");
+
+    try {
+      Uri u("http://" + s);
+      CHECK(false) << "Control should not have reached here";
+    } catch (const std::invalid_argument& ex) {
+      EXPECT_TRUE(boost::algorithm::ends_with(ex.what(), s));
+    }
+  }
 }