errorLog.ts 830 B

123456789101112131415161718192021222324252627282930
  1. import { App } from 'vue'
  2. import pinia from '@/store'
  3. import { useErrorLogStore } from '@/store/modules/errorLog'
  4. import { errorLog } from '@/config'
  5. import { isArray, isString } from '@/utils/validate'
  6. export const needErrorLog = () => {
  7. const errorLogArray = isArray(errorLog)
  8. ? [...errorLog]
  9. : isString(errorLog)
  10. ? [...[errorLog]]
  11. : []
  12. return errorLogArray.includes(process.env.NODE_ENV as string)
  13. }
  14. export const addErrorLog = (err: any) => {
  15. // eslint-disable-next-line no-console
  16. if (!err.isRequest) console.error('vue-admin-better错误拦截:', err)
  17. const url = window.location.href
  18. const { addErrorLog } = useErrorLogStore(pinia)
  19. addErrorLog({ err, url })
  20. }
  21. export default {
  22. install(app: App<Element>) {
  23. if (needErrorLog()) {
  24. app.config.errorHandler = addErrorLog
  25. }
  26. },
  27. }