You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

errorHandle.js 2.5 kB

5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /** Copyright 2020 Tianshu AI Platform. All Rights Reserved.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. * =============================================================
  15. */
  16. import Vue from 'vue';
  17. import { Message, MessageBox } from 'element-ui';
  18. import store from '@/store';
  19. const UNAUTHORIZED = 401; // 未授权
  20. const TOKENEXPIRE = 20012; // Token 失效
  21. let isMsgOn = false;
  22. // 全局未捕获异常处理(包括普通异常和 await 未被捕获的异常)
  23. Vue.config.errorHandler = (err) => {
  24. console.error(err);
  25. // 未授权只提示一次
  26. if (err.code === UNAUTHORIZED) {
  27. if (isMsgOn === true) return;
  28. isMsgOn = true;
  29. }
  30. if (err.name !== 'AssertError' && err.message) {
  31. Message.error({
  32. message: err.message,
  33. onClose: () => {
  34. isMsgOn = false;
  35. },
  36. });
  37. } else {
  38. isMsgOn = false;
  39. }
  40. };
  41. // 只针对 promise 异步捕获
  42. // eslint-disable-next-line func-names
  43. window.addEventListener('unhandledrejection', function(event) {
  44. const { reason } = event;
  45. if (reason) {
  46. // 未授权
  47. if (reason.code === TOKENEXPIRE) {
  48. // 弹窗只允许一次
  49. if (isMsgOn === true) return;
  50. isMsgOn = true;
  51. // Token 失效
  52. MessageBox.confirm('您已经登出,请重新登录', '请登录', {
  53. confirmButtonText: '确认',
  54. cancelButtonText: '取消',
  55. type: 'warning',
  56. })
  57. .then(() => {
  58. // 此处调用 store lotout
  59. store.dispatch('LogOut').then(() => {
  60. window.location.pathname !== '/login' && window.location.reload();
  61. });
  62. })
  63. .finally(() => {
  64. isMsgOn = false;
  65. });
  66. return;
  67. }
  68. if (reason.code === UNAUTHORIZED) {
  69. // 未授权提醒只展示一次
  70. if (isMsgOn === true) return;
  71. isMsgOn = true;
  72. }
  73. if (reason.message) {
  74. Message.error({
  75. message: reason.message,
  76. onClose: () => {
  77. isMsgOn = false;
  78. },
  79. });
  80. } else {
  81. isMsgOn = false;
  82. }
  83. }
  84. });

一站式算法开发平台、高性能分布式深度学习框架、先进算法模型库、视觉模型炼知平台、数据可视化分析平台等一系列平台及工具,在模型高效分布式训练、数据处理和可视分析、模型炼知和轻量化等技术上形成独特优势,目前已在产学研等各领域近千家单位及个人提供AI应用赋能

Contributors (1)