From 218b523148612c9eda1e4e817573c2309dae71fa Mon Sep 17 00:00:00 2001 From: Tom Jackson Date: Mon, 4 Mar 2013 11:52:58 -0800 Subject: [PATCH] Adding useful error message for File Summary: 'open() failed' isn't too helpful. Seeing a filename there is. So I added it. Test Plan: Unit test Reviewed By: mohittalwar@fb.com FB internal diff: D725204 --- folly/File.cpp | 5 ++++- folly/test/FileTest.cpp | 12 ++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/folly/File.cpp b/folly/File.cpp index b2f8900e..1262dd1c 100644 --- a/folly/File.cpp +++ b/folly/File.cpp @@ -15,6 +15,7 @@ */ #include "folly/File.h" +#include "folly/Format.h" #include "folly/ScopeGuard.h" #include @@ -38,7 +39,9 @@ File::File(const char* name, int flags, mode_t mode) , ownsFd_(false) { if (fd_ < 0) { - throw std::system_error(errno, std::system_category(), "open() failed"); + throw std::system_error(errno, std::system_category(), + folly::format("open(\"{}\", {:#o}, 0{:#o}) failed", + name, flags, mode).str()); } ownsFd_ = true; } diff --git a/folly/test/FileTest.cpp b/folly/test/FileTest.cpp index 0111e4b9..14b027c9 100644 --- a/folly/test/FileTest.cpp +++ b/folly/test/FileTest.cpp @@ -84,3 +84,15 @@ TEST(File, OwnsFd) { ::close(p[0]); } +#define EXPECT_CONTAINS(haystack, needle) \ + EXPECT_NE(::std::string::npos, ::folly::StringPiece(haystack).find(needle)) \ + << "Haystack: '" << haystack << "'\nNeedle: '" << needle << "'"; + +TEST(File, UsefulError) { + try { + File("does_not_exist.txt", 0, 0666); + } catch (const std::runtime_error& e) { + EXPECT_CONTAINS(e.what(), "does_not_exist.txt"); + EXPECT_CONTAINS(e.what(), "0666"); + } +} -- 2.34.1