Commit 3437a028 authored by Rajat Goel's avatar Rajat Goel Committed by Peter Griess

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
parent e3a49e3d
......@@ -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);
......
......@@ -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));
}
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment