Commit 218b5231 authored by Tom Jackson's avatar Tom Jackson Committed by Jordan DeLong

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
parent f0abc60e
......@@ -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;
}
......
......@@ -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");
}
}
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