1 /*
2  * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "b_error/b_error.h"
17 
18 #include <algorithm>
19 #include <sstream>
20 #include <sys/syscall.h>
21 
22 #include "dfx_dump_catcher.h"
23 #include "filemgmt_libhilog.h"
24 
25 namespace OHOS::FileManagement::Backup {
26 using namespace std;
WrapMessageWithExtraInfos(const char * fileName,int lineNo,const char * functionName,Codes code,const vector<string_view> & msgs) const27 string BError::WrapMessageWithExtraInfos(const char *fileName,
28                                          int lineNo,
29                                          const char *functionName,
30                                          Codes code,
31                                          const vector<string_view> &msgs) const
32 {
33     stringstream ss;
34     ss << '[' << fileName << ':' << lineNo << " -> " << functionName << ']' << ' ';
35     for (size_t i = 0; i < msgs.size(); ++i) {
36         ss << msgs[i];
37         if (i != msgs.size() - 1) {
38             ss << ". ";
39         }
40     }
41 
42     if (code != Codes::OK) {
43         string msg;
44         HiviewDFX::DfxDumpCatcher().DumpCatch(getprocpid(), syscall(SYS_gettid), msg);
45         ss << endl << msg;
46         string res = ss.str();
47         (void)HILOG_IMPL(LOG_CORE, LOG_ERROR, LOG_DOMAIN, LOG_TAG, "%{public}s", res.c_str());
48         return res;
49     }
50 
51     string res = ss.str();
52     (void)HILOG_IMPL(LOG_CORE, LOG_DEBUG, LOG_DOMAIN, LOG_TAG, "%{public}s", res.c_str());
53     return res;
54 }
55 
GetCode() const56 int BError::GetCode() const
57 {
58     int code = static_cast<int>(GetRawCode());
59     if (errCodeTable_.find(code) != errCodeTable_.end()) {
60         return errCodeTable_.at(code);
61     }
62     HILOGE("Unknown code : %{public}d", code);
63     return BackupErrorCode::E_UKERR;
64 }
65 
GetCodeByErrno(int32_t errnoSys)66 int BError::GetCodeByErrno(int32_t errnoSys)
67 {
68     if (errnoSys == 0) {
69         return 0;
70     }
71     HILOGE("Unknown code : %{public}d", errnoSys);
72     if (sysErrnoCodeTable_.find(errnoSys) != sysErrnoCodeTable_.end()) {
73         return sysErrnoCodeTable_.at(errnoSys);
74     }
75     return BackupErrorCode::E_UKERR;
76 }
77 
GetBackupCodeByErrno(ErrCode err)78 ErrCode BError::GetBackupCodeByErrno(ErrCode err)
79 {
80     if (err == 0) {
81         return 0;
82     }
83     if (errCodeTable_.find(err) != errCodeTable_.end()) {
84         return errCodeTable_.at(err);
85     }
86     return BackupErrorCode::E_UKERR;
87 }
88 
GetBackupMsgByErrno(ErrCode err)89 string BError::GetBackupMsgByErrno(ErrCode err)
90 {
91     if (backupErrorMsgTable_.find(err) != backupErrorMsgTable_.end()) {
92         return backupErrorMsgTable_.at(err);
93     }
94     return backupErrorMsgTable_.at(BackupErrorCode::E_UKERR);
95 }
96 } // namespace OHOS::FileManagement::Backup