1/*
2 * Copyright (c) 2022-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, 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 */
15import { Log } from './Log';
16
17const TAG: string = 'ErrUtil';
18
19export function tryFunc(getValue: Function, ...args): any {
20  if (typeof getValue !== 'function') {
21    Log.e(TAG, typeof getValue, 'is not a func ', ...args);
22    return;
23  }
24  let res;
25  try {
26    res = getValue(...args);
27  } catch (error) {
28    Log.e(TAG, {
29      function: getValue.name,
30      args: args,
31      error,
32      errMsg: String(error)
33    });
34  }
35  return res;
36}
37
38export async function tryFuncAsync(getValue: Function, ...args): Promise<any> {
39  if (typeof getValue !== 'function') {
40    Log.e(TAG, 'not a func ', ...args);
41    return;
42  }
43  let res;
44  try {
45    res = await getValue(...args);
46  } catch (error) {
47    Log.e(TAG, {
48      function: getValue.name,
49      args: args,
50      error,
51      errMsg: String(error)
52    });
53  }
54  return res;
55}