Permalink
Browse files

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
  • Loading branch information...
1 parent f0abc60 commit 218b523148612c9eda1e4e817573c2309dae71fa Tom Jackson committed with jdelong Mar 4, 2013
Showing with 16 additions and 1 deletion.
  1. +4 −1 folly/File.cpp
  2. +12 −0 folly/test/FileTest.cpp
View
@@ -15,6 +15,7 @@
*/
#include "folly/File.h"
+#include "folly/Format.h"
#include "folly/ScopeGuard.h"
#include <system_error>
@@ -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;
}
View
@@ -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");
+ }
+}

0 comments on commit 218b523

Please sign in to comment.