Coverage Report

Created: 2026-03-12 17:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
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
36.7k
io::ObjectStorageStatus convert_to_obj_response(Status st) {
36
36.7k
    int code = st._code;
37
36.7k
    std::string msg = st._err_msg == nullptr ? "" : std::move(st._err_msg->_msg);
38
36.7k
    return io::ObjectStorageStatus {.code = code, .msg = std::move(msg)};
39
36.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
14
std::string hdfs_error() {
53
14
    std::stringstream ss;
54
14
    char buf[1024];
55
14
    ss << "(" << errno << "), " << strerror_r(errno, buf, 1024) << ")";
56
14
#ifdef USE_HADOOP_HDFS
57
14
    char* root_cause = hdfsGetLastExceptionRootCause();
58
14
    if (root_cause != nullptr) {
59
14
        ss << ", reason: " << root_cause;
60
14
    }
61
#else
62
    ss << ", reason: " << hdfsGetLastError();
63
#endif
64
14
    return ss.str();
65
14
}
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
16.8k
Status localfs_error(const std::error_code& ec, std::string_view msg) {
88
16.8k
    auto message = fmt::format("{}: {}", msg, ec.message());
89
16.8k
    if (ec == std::errc::io_error) {
90
0
        return Status::Error<IO_ERROR, false>(message);
91
16.8k
    } else if (ec == std::errc::no_such_file_or_directory) {
92
16.7k
        return Status::Error<NOT_FOUND, false>(message);
93
16.7k
    } else if (ec == std::errc::file_exists) {
94
0
        return Status::Error<ALREADY_EXIST, false>(message);
95
89
    } else if (ec == std::errc::no_space_on_device) {
96
0
        return Status::Error<DISK_REACH_CAPACITY_LIMIT, false>(message);
97
89
    } else if (ec == std::errc::permission_denied) {
98
0
        return Status::Error<PERMISSION_DENIED, false>(message);
99
89
    } else if (ec == std::errc::directory_not_empty) {
100
0
        return Status::Error<DIRECTORY_NOT_EMPTY, false>(message);
101
89
    } else {
102
89
        return Status::Error<ErrorCode::INTERNAL_ERROR, false>(message);
103
89
    }
104
16.8k
}
105
106
4
Status localfs_error(int posix_errno, std::string_view msg) {
107
4
    char buf[1024];
108
4
    auto message = fmt::format("{}: {}", msg, strerror_r(errno, buf, 1024));
109
4
    switch (posix_errno) {
110
2
    case EIO:
111
2
        return Status::Error<IO_ERROR, false>(message);
112
2
    case ENOENT:
113
2
        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
4
    }
123
4
}
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