• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 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, softwarecheck{  }
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 */
15import { ApiResultSimpleInfo, ApiResultInfo, ErrorMessage, ApiCheckInfo } from '../../../typedef/checker/result_type';
16import { CommonFunctions } from '../../../utils/checkUtils';
17
18export class AddErrorLogs {
19  /**
20   * 按照格式生成错误信息
21   * @param { ApiCheckInfo } apiInfo -api infomation
22   * @param { ApiResultSimpleInfo[] } checkErrorInfos -save api check base error info
23   * @param { ApiResultInfo[] } checkErrorAllInfos -save api check error info
24   */
25  static addAPICheckErrorLogs(apiInfo: ApiCheckInfo, checkErrorInfos: ApiResultSimpleInfo[],
26    checkErrorAllInfos: ApiResultInfo[],
27  ): void {
28    const location = JSON.stringify(apiInfo.getApiPostion().line);
29    const errorMessage: string = `API check error of [${apiInfo.getErrorType()}]: ${apiInfo.getErrorInfo()}`;
30    const apiChecktSimpleErrorLog: ApiResultSimpleInfo = new ApiResultSimpleInfo();
31
32    apiChecktSimpleErrorLog
33      .setID(apiInfo.getErrorID())
34      .setLevel(apiInfo.getErrorLevel())
35      .setLocation(location)
36      .setFilePath(apiInfo.getFilePath())
37      .setMessage(errorMessage)
38      .setApiText(apiInfo.getApiText())
39      .setApiName(apiInfo.getApiName())
40      .setApiType(apiInfo.getApiType())
41      .setHierarchicalRelations(apiInfo.getHierarchicalRelations())
42      .setParentModuleName(apiInfo.getParentModuleName());
43
44    const apiCheckErrorLog: ApiResultInfo = new ApiResultInfo();
45    apiCheckErrorLog
46      .setErrorType(apiInfo.getErrorType())
47      .setLocation(apiInfo.getFilePath().slice(apiInfo.getFilePath().indexOf('api'), apiInfo.getFilePath().length) + `(line: ${location})`)
48      .setApiType(apiInfo.getApiType())
49      .setMessage(errorMessage)
50      .setVersion(apiInfo.getSinceNumber())
51      .setLevel(apiInfo.getErrorLevel())
52      .setApiName(apiInfo.getApiName())
53      .setApiFullText(apiInfo.getApiText())
54      .setBaseName(apiInfo.getFilePath().slice(apiInfo.getFilePath().lastIndexOf('\\') + 1, apiInfo.getFilePath().length))
55      .setHierarchicalRelations(apiInfo.getHierarchicalRelations())
56      .setParentModuleName(apiInfo.getParentModuleName())
57      .setDefectType('');
58    let isLostKitErrorInfo: boolean = apiInfo.getErrorInfo() === CommonFunctions.createErrorInfo(ErrorMessage.ERROR_LOST_LABEL, ['kit']);
59    let isLostFileErrorInfo: boolean = apiInfo.getErrorInfo() === CommonFunctions.createErrorInfo(ErrorMessage.ERROR_LOST_LABEL, ['file']);
60    let hasLostKitErrorInfo: string[] = [];
61    let hasLostfileErrorInfo: string[] = [];
62
63    checkErrorInfos.forEach((checkErrorInfo: ApiResultSimpleInfo) => {
64      const checkErrorMessage: string = checkErrorInfo.getMessage().replace(/API check error of \[.*\]: /g, '');
65      const keyMessage: string = checkErrorInfo.getFilePath() + checkErrorMessage;
66      if (keyMessage === apiInfo.getFilePath() + CommonFunctions.createErrorInfo(ErrorMessage.ERROR_LOST_LABEL, ['kit'])) {
67        hasLostKitErrorInfo.push(checkErrorInfo.getFilePath() + checkErrorInfo.getMessage());
68      }
69      if (keyMessage === apiInfo.getFilePath() + CommonFunctions.createErrorInfo(ErrorMessage.ERROR_LOST_LABEL, ['file'])) {
70        hasLostfileErrorInfo.push(checkErrorInfo.getFilePath() + checkErrorInfo.getMessage());
71      }
72    });
73    if ((isLostKitErrorInfo && hasLostKitErrorInfo.length !== 0) ||
74      (isLostFileErrorInfo && hasLostfileErrorInfo.length !== 0)) {
75      return;
76    }
77    checkErrorInfos.push(apiChecktSimpleErrorLog);
78    checkErrorAllInfos.push(apiCheckErrorLog);
79  }
80}
81