zhou zhou
2 天以前 33bd4dd1f0e41131cd8e5bbf87204a1f0b72bb08
rsf-design/src/utils/http/error.js
@@ -9,6 +9,8 @@
    this.timestamp = /* @__PURE__ */ new Date().toISOString()
    this.url = options?.url
    this.method = options?.method
    this.status = options?.status
    this.contentType = options?.contentType
  }
  toLogData() {
    return {
@@ -18,8 +20,50 @@
      timestamp: this.timestamp,
      url: this.url,
      method: this.method,
      status: this.status,
      contentType: this.contentType,
      stack: this.stack
    }
  }
}
function resolveRequestUrl(config) {
  const baseURL = String(config?.baseURL || '').trim()
  const requestUrl = String(config?.url || '').trim()
  if (!baseURL && !requestUrl) {
    return void 0
  }
  if (!baseURL) {
    return requestUrl || void 0
  }
  if (!requestUrl) {
    return baseURL
  }
  const isAbsoluteBase = /^https?:\/\//i.test(baseURL)
  if (!isAbsoluteBase) {
    const normalizedBase = baseURL.replace(/\/+$/, '')
    const normalizedUrl = requestUrl.replace(/^\/+/, '')
    if (!normalizedBase) {
      return requestUrl
    }
    if (!normalizedUrl) {
      return normalizedBase
    }
    return `${normalizedBase}/${normalizedUrl}`
  }
  try {
    const origin = globalThis?.location?.origin || 'http://localhost'
    const normalizedBase = new URL(baseURL, origin)
    return new URL(requestUrl, normalizedBase).toString()
  } catch {
    const normalizedBase = baseURL.replace(/\/+$/, '')
    const normalizedUrl = requestUrl.replace(/^\/+/, '')
    if (!normalizedBase) {
      return requestUrl
    }
    if (!normalizedUrl) {
      return normalizedBase
    }
    return `${normalizedBase}/${normalizedUrl}`
  }
}
const getErrorMessage = (status) => {
@@ -52,7 +96,7 @@
  const requestConfig = error.config
  if (isRequestCancelled(error)) {
    throw new HttpError($t('httpMsg.requestCancelled'), 'REQUEST_CANCELLED', {
      url: requestConfig?.url,
      url: resolveRequestUrl(requestConfig),
      method: requestConfig?.method?.toUpperCase()
    })
  }
@@ -60,7 +104,7 @@
  const errorMessage = error.response?.data?.msg || error.message
  if (!error.response) {
    throw new HttpError($t('httpMsg.networkError'), ApiStatus.error, {
      url: requestConfig?.url,
      url: resolveRequestUrl(requestConfig),
      method: requestConfig?.method?.toUpperCase()
    })
  }
@@ -69,8 +113,10 @@
    : errorMessage || $t('httpMsg.requestFailed')
  throw new HttpError(message, statusCode || ApiStatus.error, {
    data: error.response.data,
    url: requestConfig?.url,
    method: requestConfig?.method?.toUpperCase()
    url: resolveRequestUrl(requestConfig),
    method: requestConfig?.method?.toUpperCase(),
    status: statusCode,
    contentType: error.response?.headers?.['content-type']
  })
}
function showError(error, showMessage = true) {
@@ -90,4 +136,4 @@
const isHttpError = (error) => {
  return error instanceof HttpError
}
export { HttpError, handleError, isHttpError, showError, showSuccess }
export { HttpError, handleError, isHttpError, resolveRequestUrl, showError, showSuccess }