be/src/io/fs/err_utils.cpp
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #include "io/fs/err_utils.h" |
19 | | |
20 | | // IWYU pragma: no_include <bthread/errno.h> |
21 | | #include <aws/s3/S3Errors.h> |
22 | | #include <errno.h> // IWYU pragma: keep |
23 | | #include <fmt/format.h> |
24 | | #include <string.h> |
25 | | |
26 | | #include <sstream> |
27 | | |
28 | | #include "common/status.h" |
29 | | #include "io/fs/hdfs.h" |
30 | | #include "io/fs/obj_storage_client.h" |
31 | | |
32 | | namespace doris { |
33 | | using namespace ErrorCode; |
34 | | |
35 | 37.7k | io::ObjectStorageStatus convert_to_obj_response(Status st) { |
36 | 37.7k | int code = st._code; |
37 | 37.7k | std::string msg = st._err_msg == nullptr ? "" : std::move(st._err_msg->_msg); |
38 | 37.7k | return io::ObjectStorageStatus {.code = code, .msg = std::move(msg)}; |
39 | 37.7k | } |
40 | | |
41 | | namespace io { |
42 | | |
43 | 0 | std::string errno_to_str() { |
44 | 0 | char buf[1024]; |
45 | 0 | return fmt::format("({}), {}", errno, strerror_r(errno, buf, 1024)); |
46 | 0 | } |
47 | | |
48 | 0 | std::string errcode_to_str(const std::error_code& ec) { |
49 | 0 | return fmt::format("({}), {}", ec.value(), ec.message()); |
50 | 0 | } |
51 | | |
52 | 10 | std::string hdfs_error() { |
53 | 10 | std::stringstream ss; |
54 | 10 | char buf[1024]; |
55 | 10 | ss << "(" << errno << "), " << strerror_r(errno, buf, 1024) << ")"; |
56 | 10 | #ifdef USE_HADOOP_HDFS |
57 | 10 | char* root_cause = hdfsGetLastExceptionRootCause(); |
58 | 10 | if (root_cause != nullptr) { |
59 | 10 | ss << ", reason: " << root_cause; |
60 | 10 | } |
61 | | #else |
62 | | ss << ", reason: " << hdfsGetLastError(); |
63 | | #endif |
64 | 10 | return ss.str(); |
65 | 10 | } |
66 | | |
67 | 12 | std::string glob_err_to_str(int code) { |
68 | 12 | std::string msg; |
69 | | // https://sites.uclouvain.be/SystInfo/usr/include/glob.h.html |
70 | 12 | switch (code) { |
71 | 0 | case 1: |
72 | 0 | msg = "Ran out of memory"; |
73 | 0 | break; |
74 | 0 | case 2: |
75 | 0 | msg = "read error"; |
76 | 0 | break; |
77 | 12 | case 3: |
78 | 12 | msg = "No matches found"; |
79 | 12 | break; |
80 | 0 | default: |
81 | 0 | msg = "unknown"; |
82 | 0 | break; |
83 | 12 | } |
84 | 12 | return fmt::format("({}), {}", code, msg); |
85 | 12 | } |
86 | | |
87 | 19.0k | Status localfs_error(const std::error_code& ec, std::string_view msg) { |
88 | 19.0k | auto message = fmt::format("{}: {}", msg, ec.message()); |
89 | 19.0k | if (ec == std::errc::io_error) { |
90 | 0 | return Status::Error<IO_ERROR, false>(message); |
91 | 19.0k | } else if (ec == std::errc::no_such_file_or_directory) { |
92 | 19.0k | return Status::Error<NOT_FOUND, false>(message); |
93 | 18.4E | } else if (ec == std::errc::file_exists) { |
94 | 0 | return Status::Error<ALREADY_EXIST, false>(message); |
95 | 18.4E | } else if (ec == std::errc::no_space_on_device) { |
96 | 0 | return Status::Error<DISK_REACH_CAPACITY_LIMIT, false>(message); |
97 | 18.4E | } else if (ec == std::errc::permission_denied) { |
98 | 0 | return Status::Error<PERMISSION_DENIED, false>(message); |
99 | 18.4E | } else if (ec == std::errc::directory_not_empty) { |
100 | 0 | return Status::Error<DIRECTORY_NOT_EMPTY, false>(message); |
101 | 18.4E | } else { |
102 | 18.4E | return Status::Error<ErrorCode::INTERNAL_ERROR, false>(message); |
103 | 18.4E | } |
104 | 19.0k | } |
105 | | |
106 | 3 | Status localfs_error(int posix_errno, std::string_view msg) { |
107 | 3 | char buf[1024]; |
108 | 3 | auto message = fmt::format("{}: {}", msg, strerror_r(errno, buf, 1024)); |
109 | 3 | switch (posix_errno) { |
110 | 2 | case EIO: |
111 | 2 | return Status::Error<IO_ERROR, false>(message); |
112 | 1 | case ENOENT: |
113 | 1 | return Status::Error<NOT_FOUND, false>(message); |
114 | 0 | case EEXIST: |
115 | 0 | return Status::Error<ALREADY_EXIST, false>(message); |
116 | 0 | case ENOSPC: |
117 | 0 | return Status::Error<DISK_REACH_CAPACITY_LIMIT, false>(message); |
118 | 0 | case EACCES: |
119 | 0 | return Status::Error<PERMISSION_DENIED, false>(message); |
120 | 0 | default: |
121 | 0 | return Status::Error<ErrorCode::INTERNAL_ERROR, false>(message); |
122 | 3 | } |
123 | 3 | } |
124 | | |
125 | 16 | Status s3fs_error(const Aws::S3::S3Error& err, std::string_view msg) { |
126 | 16 | using namespace Aws::Http; |
127 | 16 | switch (err.GetResponseCode()) { |
128 | 10 | case HttpResponseCode::NOT_FOUND: |
129 | 10 | return Status::Error<NOT_FOUND, false>("{}: {} {} code=NOT_FOUND, type={}, request_id={}", |
130 | 10 | msg, err.GetExceptionName(), err.GetMessage(), |
131 | 10 | err.GetErrorType(), err.GetRequestId()); |
132 | 3 | case HttpResponseCode::FORBIDDEN: |
133 | 3 | return Status::Error<PERMISSION_DENIED, false>( |
134 | 3 | "{}: {} {} code=FORBIDDEN, type={}, request_id={}", msg, err.GetExceptionName(), |
135 | 3 | err.GetMessage(), err.GetErrorType(), err.GetRequestId()); |
136 | 3 | default: |
137 | 3 | return Status::Error<ErrorCode::INTERNAL_ERROR, false>( |
138 | 3 | "{}: {} {} code={} type={}, request_id={}", msg, err.GetExceptionName(), |
139 | 3 | err.GetMessage(), err.GetResponseCode(), err.GetErrorType(), err.GetRequestId()); |
140 | 16 | } |
141 | 16 | } |
142 | | |
143 | | } // namespace io |
144 | | } // namespace doris |