sapling/eden/fs/inodes/InodeError.cpp
Chad Austin 2a6dd2879d folly::Optional -> std::optional
Summary: Eden's on C++17 so fully cross the rubicon!

Reviewed By: strager

Differential Revision: D10498200

fbshipit-source-id: 4e2af5a8d5cef9a106e8c05e6f93ea9e5b8e696e
2018-10-23 18:51:59 -07:00

69 lines
1.8 KiB
C++

/*
* Copyright (c) 2016-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#include "InodeError.h"
#include <folly/String.h>
#include "eden/fs/inodes/TreeInode.h"
namespace facebook {
namespace eden {
InodeError::InodeError(int errnum, TreeInodePtr inode, PathComponentPiece child)
: std::system_error(errnum, std::generic_category()),
inode_(std::move(inode)),
child_(PathComponent{child}) {}
InodeError::InodeError(
int errnum,
TreeInodePtr inode,
PathComponentPiece child,
std::string&& message)
: std::system_error(errnum, std::generic_category()),
inode_(std::move(inode)),
child_(PathComponent{child}),
message_(std::move(message)) {}
const char* InodeError::what() const noexcept {
try {
auto msg = fullMessage_.wlock();
if (msg->empty()) {
*msg = computeMessage();
}
return msg->c_str();
} catch (...) {
// Fallback value if anything goes wrong building the real message
return "<InodeError>";
}
}
std::string InodeError::computeMessage() const {
std::string path;
if (child_.has_value()) {
if (inode_->getNodeId() == kRootNodeId) {
path = child_.value().stringPiece().str();
} else {
path = inode_->getLogPath() + "/";
auto childName = child_.value().stringPiece();
path.append(childName.begin(), childName.end());
}
} else {
path = inode_->getLogPath();
}
if (message_.empty()) {
return path + ": " + folly::errnoStr(errnum()).toStdString();
}
return path + ": " + message_ + ": " +
folly::errnoStr(errnum()).toStdString();
}
} // namespace eden
} // namespace facebook