Skip to content

Commit aa6904c

Browse files
improve std code and properly throw std errors
1 parent 07c6f8e commit aa6904c

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

src/env-inl.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,13 @@ inline void Environment::ThrowError(
775775
isolate()->ThrowException(fun(OneByteString(isolate(), errmsg), {}));
776776
}
777777

778+
inline void Environment::ThrowStdErrException(std::error_code error_code,
779+
const char* syscall,
780+
const char* path) {
781+
ThrowErrnoException(
782+
error_code.value(), syscall, error_code.message().c_str(), path);
783+
}
784+
778785
inline void Environment::ThrowErrnoException(int errorno,
779786
const char* syscall,
780787
const char* message,

src/env.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,9 @@ class Environment final : public MemoryRetainer {
830830
inline void ThrowError(const char* errmsg);
831831
inline void ThrowTypeError(const char* errmsg);
832832
inline void ThrowRangeError(const char* errmsg);
833+
inline void ThrowStdErrException(std::error_code error_code,
834+
const char* syscall,
835+
const char* path = nullptr);
833836
inline void ThrowErrnoException(int errorno,
834837
const char* syscall = nullptr,
835838
const char* message = nullptr,

src/node_file.cc

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3379,26 +3379,21 @@ static void CpSyncOverrideFile(const FunctionCallbackInfo<Value>& args) {
33793379

33803380
std::error_code error;
33813381

3382-
std::filesystem::remove(*dest, error);
3383-
if (error) {
3384-
std::string message = "operation not permitted, unlink";
3385-
return env->ThrowErrnoException(
3386-
EPERM, "unlink", message.c_str(), dest.out());
3382+
if (!std::filesystem::remove(*dest, error)) {
3383+
return env->ThrowStdErrException(error, "unlink", *dest);
33873384
}
33883385

33893386
if (mode == 0) {
33903387
// if no mode is specified use the faster std::filesystem API
3391-
std::filesystem::copy_file(
3392-
*src, *dest, std::filesystem::copy_options::none, error);
3393-
if (error) {
3394-
return env->ThrowError(error.message().c_str());
3388+
if (!std::filesystem::copy_file(*src, *dest, error)) {
3389+
return env->ThrowStdErrException(error, "cp", *dest);
33953390
}
33963391
} else {
33973392
uv_fs_t req;
33983393
auto cleanup = OnScopeLeave([&req]() { uv_fs_req_cleanup(&req); });
3399-
int result = uv_fs_copyfile(nullptr, &req, *src, *dest, mode, nullptr);
3394+
auto result = uv_fs_copyfile(nullptr, &req, *src, *dest, mode, nullptr);
34003395
if (is_uv_error(result)) {
3401-
return env->ThrowUVException(result, "copyfile", nullptr, *src, *dest);
3396+
return env->ThrowUVException(result, "cp", nullptr, *src, *dest);
34023397
}
34033398
}
34043399

0 commit comments

Comments
 (0)