import { RequestApiError } from "@imengyu/imengyu-utils";

export function showError(e: any, title = '糟糕，出错了', callback?: () => void) {
  console.log('showError', e);
  let message = '';
  if (e?.errMsg) 
    message = e.errMsg;
  else 
    message = '' + (e ?? '未知错误');
  uni.showModal({ 
    title,
    content: message,
    showCancel: false,
    success() {
      callback?.();
    }
  })
  uni.hideLoading();
}

export interface MpErrorInfo {
  errMsg: string;
}
export function formatError(e: any) {
  if (e instanceof RequestApiError) 
    return e.errorMessage + (e.errorCodeMessage ? ` (${e.errorCodeMessage})` : '');
  if (e instanceof Error) 
    return e.message;
  else 
    return '' + (e ?? '未知错误');
}
