Coverage Report

Created: 2026-03-15 17:28

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
16
io::ObjectStorageStatus convert_to_obj_response(Status st) {
36
16
    int code = st._code;
37
16
    std::string msg = st._err_msg == nullptr ? "" : std::move(st._err_msg->_msg);
38
16
    return io::ObjectStorageStatus {.code = code, .msg = std::move(msg)};
39
16
}
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
0
std::string hdfs_error() {
53
0
    std::stringstream ss;
54
0
    char buf[1024];
55
0
    ss << "(" << errno << "), " << strerror_r(errno, buf, 1024) << ")";
56
0
#ifdef USE_HADOOP_HDFS
57
0
    char* root_cause = hdfsGetLastExceptionRootCause();
58
0
    if (root_cause != nullptr) {
59
0
        ss << ", reason: " << root_cause;
60
0
    }
61
#else
62
    ss << ", reason: " << hdfsGetLastError();
63
#endif
64
0
    return ss.str();
65
0
}
66
67
1
std::string glob_err_to_str(int code) {
68
1
    std::string msg;
69
    // https://sites.uclouvain.be/SystInfo/usr/include/glob.h.html
70
1
    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
1
    case 3:
78
1
        msg = "No matches found";
79
1
        break;
80
0
    default:
81
0
        msg = "unknown";
82
0
        break;
83
1
    }
84
1
    return fmt::format("({}), {}", code, msg);
85
1
}
86
87
8.44k
Status localfs_error(const std::error_code& ec, std::string_view msg) {
88
8.44k
    auto message = fmt::format("{}: {}", msg, ec.message());
89
8.44k
    if (ec == std::errc::io_error) {
90
0
        return Status::Error<IO_ERROR, false>(message);
91
8.44k
    } else if (ec == std::errc::no_such_file_or_directory) {
92
8.41k
        return Status::Error<NOT_FOUND, false>(message);
93
8.41k
    } else if (ec == std::errc::file_exists) {
94
0
        return Status::Error<ALREADY_EXIST, false>(message);
95
34
    } else if (ec == std::errc::no_space_on_device) {
96
0
        return Status::Error<DISK_REACH_CAPACITY_LIMIT, false>(message);
97
34
    } else if (ec == std::errc::permission_denied) {
98
0
        return Status::Error<PERMISSION_DENIED, false>(message);
99
34
    } else if (ec == std::errc::directory_not_empty) {
100
0
        return Status::Error<DIRECTORY_NOT_EMPTY, false>(message);
101
34
    } else {
102
34
        return Status::Error<ErrorCode::INTERNAL_ERROR, false>(message);
103
34
    }
104
8.44k
}
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
5
Status s3fs_error(const Aws::S3::S3Error& err, std::string_view msg) {
126
5
    using namespace Aws::Http;
127
5
    switch (err.GetResponseCode()) {
128
0
    case HttpResponseCode::NOT_FOUND:
129
0
        return Status::Error<NOT_FOUND, false>("{}: {} {} code=NOT_FOUND, type={}, request_id={}",
130
0
                                               msg, err.GetExceptionName(), err.GetMessage(),
131
0
                                               err.GetErrorType(), err.GetRequestId());
132
2
    case HttpResponseCode::FORBIDDEN:
133
2
        return Status::Error<PERMISSION_DENIED, false>(
134
2
                "{}: {} {} code=FORBIDDEN, type={}, request_id={}", msg, err.GetExceptionName(),
135
2
                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
5
    }
141
5
}
142
143
} // namespace io
144
} // namespace doris