Skip to content

Commit dc1bb22

Browse files
committed
Factor stringifying canonicalize_path error handling
1 parent 6dfc81d commit dc1bb22

File tree

3 files changed

+43
-34
lines changed

3 files changed

+43
-34
lines changed

src/file-canonical.cpp

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -710,32 +710,10 @@ sloppy_result<canonical_path_result> canonicalize_path_sloppy(
710710
if (!canonical) return canonical.error();
711711
return sloppy_result<canonical_path_result>(*canonical);
712712
},
713-
[&](boost::leaf::e_errno error,
714-
const e_canonicalizing_path &canonicalizing) {
715-
return sloppy_result<canonical_path_result>::failure(
716-
"failed to canonicalize "s + canonicalizing.path + ": "s + path +
717-
": "s + std::strerror(error.value));
718-
},
719-
[&](boost::leaf::e_errno error) {
720-
return sloppy_result<canonical_path_result>::failure(
721-
"failed to canonicalize "s + path + ": "s +
722-
std::strerror(error.value));
723-
},
724-
[&](e_too_many_symlinks) {
713+
make_canonicalize_path_error_handlers([](std::string &&message) {
725714
return sloppy_result<canonical_path_result>::failure(
726-
"failed to canonicalize "s + path +
727-
": Too many levels of symlink"s);
728-
},
729-
[&](e_invalid_argument_empty_path) {
730-
return sloppy_result<canonical_path_result>::failure(
731-
"failed to canonicalize empty path: "s + std::strerror(EINVAL));
732-
},
733-
[&]() {
734-
QLJS_ASSERT(false); // One of the above handlers should have handled
735-
// the error already.
736-
return sloppy_result<canonical_path_result>::failure(
737-
"failed to canonicalize path: "s + path);
738-
});
715+
std::move(message));
716+
}));
739717
}
740718

741719
sloppy_result<canonical_path_result> canonicalize_path_sloppy(

src/quick-lint-js/file-canonical.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
#ifndef QUICK_LINT_JS_FILE_CANONICAL_H
55
#define QUICK_LINT_JS_FILE_CANONICAL_H
66

7+
#include <boost/leaf/common.hpp>
78
#include <boost/leaf/result.hpp>
89
#include <cstddef>
910
#include <functional>
1011
#include <optional>
1112
#include <quick-lint-js/sloppy-result.h>
1213
#include <string>
1314
#include <string_view>
15+
#include <tuple>
1416

1517
namespace quick_lint_js {
1618
class canonical_path_result;
@@ -125,6 +127,41 @@ boost::leaf::result<canonical_path_result> canonicalize_path_2(
125127
sloppy_result<canonical_path_result> canonicalize_path_sloppy(const char *path);
126128
sloppy_result<canonical_path_result> canonicalize_path_sloppy(
127129
const std::string &path);
130+
131+
// Valid signatures for handle_error:
132+
// <<any rvalue result type>> handle_error(const std::string &message);
133+
// <<any rvalue result type>> handle_error(std::string &&message);
134+
template <class Func>
135+
auto make_canonicalize_path_error_handlers(const Func &handle_error) {
136+
using namespace std::literals::string_literals;
137+
return std::tuple(
138+
[handle_error](const boost::leaf::e_file_name &path,
139+
boost::leaf::e_errno error,
140+
const e_canonicalizing_path &canonicalizing) {
141+
return handle_error("failed to canonicalize "s + canonicalizing.path +
142+
": "s + path.value + ": "s +
143+
std::strerror(error.value));
144+
},
145+
[handle_error](const boost::leaf::e_file_name &path,
146+
boost::leaf::e_errno error) {
147+
return handle_error("failed to canonicalize "s + path.value + ": "s +
148+
std::strerror(error.value));
149+
},
150+
[handle_error](const boost::leaf::e_file_name &path,
151+
e_too_many_symlinks) {
152+
return handle_error("failed to canonicalize "s + path.value +
153+
": Too many levels of symlink"s);
154+
},
155+
[handle_error](e_invalid_argument_empty_path) {
156+
return handle_error("failed to canonicalize empty path: "s +
157+
std::strerror(EINVAL));
158+
},
159+
[handle_error]() {
160+
QLJS_ASSERT(false); // One of the above handlers should have handled
161+
// the error already.
162+
return handle_error("failed to canonicalize path"s);
163+
});
164+
}
128165
}
129166

130167
namespace std {

test/test-file-canonical.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,9 @@ boost::leaf::result<void> canonicalize_expecting_failure(const Path& path) {
8383
}
8484

8585
auto fail_test_error_handlers() {
86-
return std::tuple(
87-
[](boost::leaf::e_errno error) {
88-
ADD_FAILURE() << "error: " << std::strerror(error.value);
89-
},
90-
[](e_too_many_symlinks) { ADD_FAILURE() << "error: too many symlinks"; },
91-
[](e_invalid_argument_empty_path) {
92-
ADD_FAILURE() << "error: invalid argument: empty path";
93-
},
94-
[]() { ADD_FAILURE() << "unknown error"; });
86+
return make_canonicalize_path_error_handlers([](const std::string& message) {
87+
ADD_FAILURE() << "error: " << message;
88+
});
9589
}
9690

9791
bool process_ignores_filesystem_permissions() noexcept {

0 commit comments

Comments
 (0)