100aff185Sopenharmony_ci/*
200aff185Sopenharmony_ci * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
300aff185Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
400aff185Sopenharmony_ci * you may not use this file except in compliance with the License.
500aff185Sopenharmony_ci * You may obtain a copy of the License at
600aff185Sopenharmony_ci *
700aff185Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
800aff185Sopenharmony_ci *
900aff185Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1000aff185Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1100aff185Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1200aff185Sopenharmony_ci * See the License for the specific language governing permissions and
1300aff185Sopenharmony_ci * limitations under the License.
1400aff185Sopenharmony_ci */
1500aff185Sopenharmony_ciimport { Log } from './Log';
1600aff185Sopenharmony_ci
1700aff185Sopenharmony_ciconst TAG: string = 'ErrUtil';
1800aff185Sopenharmony_ci
1900aff185Sopenharmony_ciexport function tryFunc(getValue: Function, ...args): any {
2000aff185Sopenharmony_ci  if (typeof getValue !== 'function') {
2100aff185Sopenharmony_ci    Log.e(TAG, typeof getValue, 'is not a func ', ...args);
2200aff185Sopenharmony_ci    return;
2300aff185Sopenharmony_ci  }
2400aff185Sopenharmony_ci  let res;
2500aff185Sopenharmony_ci  try {
2600aff185Sopenharmony_ci    res = getValue(...args);
2700aff185Sopenharmony_ci  } catch (error) {
2800aff185Sopenharmony_ci    Log.e(TAG, {
2900aff185Sopenharmony_ci      function: getValue.name,
3000aff185Sopenharmony_ci      args: args,
3100aff185Sopenharmony_ci      error,
3200aff185Sopenharmony_ci      errMsg: String(error)
3300aff185Sopenharmony_ci    });
3400aff185Sopenharmony_ci  }
3500aff185Sopenharmony_ci  return res;
3600aff185Sopenharmony_ci}
3700aff185Sopenharmony_ci
3800aff185Sopenharmony_ciexport async function tryFuncAsync(getValue: Function, ...args): Promise<any> {
3900aff185Sopenharmony_ci  if (typeof getValue !== 'function') {
4000aff185Sopenharmony_ci    Log.e(TAG, 'not a func ', ...args);
4100aff185Sopenharmony_ci    return;
4200aff185Sopenharmony_ci  }
4300aff185Sopenharmony_ci  let res;
4400aff185Sopenharmony_ci  try {
4500aff185Sopenharmony_ci    res = await getValue(...args);
4600aff185Sopenharmony_ci  } catch (error) {
4700aff185Sopenharmony_ci    Log.e(TAG, {
4800aff185Sopenharmony_ci      function: getValue.name,
4900aff185Sopenharmony_ci      args: args,
5000aff185Sopenharmony_ci      error,
5100aff185Sopenharmony_ci      errMsg: String(error)
5200aff185Sopenharmony_ci    });
5300aff185Sopenharmony_ci  }
5400aff185Sopenharmony_ci  return res;
5500aff185Sopenharmony_ci}