errorLog.ts 720 B

1234567891011121314151617181920212223
  1. import type { App } from 'vue'
  2. import { errorLog } from '/@/config'
  3. import pinia from '/@/store'
  4. import { useErrorLogStore } from '/@/store/modules/errorLog'
  5. import { isArray } from '/@/utils/validate'
  6. export const needErrorLog = () => {
  7. const errorLogArray = isArray(errorLog) ? [...errorLog] : [errorLog]
  8. return errorLogArray.includes(import.meta.env.MODE)
  9. }
  10. export const addErrorLog = (err: any) => {
  11. if (!err.isRequest) console.error('vue-shop-vite 错误拦截:', err)
  12. const url = window.location.href
  13. const { addErrorLog } = useErrorLogStore(pinia)
  14. addErrorLog({ err, url })
  15. }
  16. export default {
  17. install: (app: App<Element>) => {
  18. if (needErrorLog()) app.config.errorHandler = addErrorLog
  19. },
  20. }