From 3437a0284f8d5311e48f3b9532e51f532a3148b2 Mon Sep 17 00:00:00 2001 From: Rajat Goel Date: Tue, 1 Oct 2013 20:07:23 -0700 Subject: [PATCH] Add bad input to Uri in exception messages 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 | 6 ++++-- folly/test/UriTest.cpp | 24 ++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/folly/Uri.cpp b/folly/Uri.cpp index 9b1151a8..9d4ad2ac 100644 --- a/folly/Uri.cpp +++ b/folly/Uri.cpp @@ -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("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("invalid URI authority ", + StringPiece(authority.first, authority.second))); } StringPiece port(authorityMatch[4].first, authorityMatch[4].second); diff --git a/folly/test/UriTest.cpp b/folly/test/UriTest.cpp index e4fe7699..cb37f075 100644 --- a/folly/test/UriTest.cpp +++ b/folly/test/UriTest.cpp @@ -16,6 +16,7 @@ #include "folly/Uri.h" +#include #include #include @@ -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)); + } + } } -- 2.34.1