返回文章列表文章索引阅读全文
STARBLOG / 阅读手记
Web前端

Integrating ExceptionLess Monitoring into a Vue 2.x Project

Preface We've always used Sentry for project monitoring, but a while ago our Sentry broke ~~(I broke it)~~ But monitoring is still very necessary. Before Sentry is fixed, I wanted to find a temporary alternative. At the same time, I found that online materials about ExceptionLess are pitifully scarce, and ExceptionLess's official documentation isn't very complete either. After digging through the docs and source code for a long time, this article came about... About ExceptionLess Those who write .NET Core shouldn't be unfamiliar, this

曦远 (DealiAxy)约 12 分钟
目录7 个章节

Preface

We've always used Sentry for project monitoring, but a while ago our Sentry broke (I broke it)

But monitoring is still very necessary. Before Sentry is fixed, I wanted to find a temporary alternative. At the same time, I found that online materials about ExceptionLess are pitifully scarce, and ExceptionLess's official documentation isn't very complete either. After digging through the docs and source code for a long time, this article came about...

About ExceptionLess

Those who write .NET Core shouldn't be unfamiliar with it. This is a monitoring tool for the .NET platform, similar to Sentry. However, we feel the UI is cleaner than Sentry's, and the information is also clearer and easier to grasp at a glance, so all of our .NET Core service monitoring is done with ExceptionLess.

ExceptionLess also supports the frontend, so I plan to temporarily use ExceptionLess for our frontend project monitoring.

Preparation

Create a project in ExceptionLess — those who know, know; I won't repeat it~

ExceptionLess provides several modes, including Node.js and browser applications. Here I chose the browser application.

The official docs also have a Vue example, but it's for the Vue 3.x version. Since we're still using Vue 2.x, I had to wrap one myself based on the browser application SDK~

Installing the ExceptionLess Client

Install the client with yarn

yarn add exceptionless

The version I used is ^1.6.4

Integrating ExceptionLess

Create a new JS file under src/utils: exceptionless.js

import {ExceptionlessClient} from 'exceptionless/dist/exceptionless';

const exLessClient = ExceptionlessClient.default;
exLessClient.config.apiKey = '';
exLessClient.config.serverUrl = ''

const install = Vue => {
  if (install.installed)
    return

  install.installed = true

  Object.defineProperties(Vue.prototype, {
    $exLess: {
      get() {
        return exLessClient
      }
    }
  })
}

export default install

This wraps ExceptionLess as a Vue module

Then edit main.js to register the module

import Exceptionless from './utils/exceptionless'
// Exceptionless模块
Vue.use(Exceptionless)

Then, wherever you need to submit logs, you can use it directly:

this.$exLess.submitLog('测试信息')
this.$exLess.submitException(error)

Integrating into Global Exception Handling

If you had to catch and submit every exception yourself manually, it would be too much trouble, and some would slip through the net

I referred to materials online and put together a Vue global exception handler that collects exceptions that weren't caught manually, then submits them all to the ExceptionLess platform.

Straight to the code, src/utils/errorHandler.js

import {ExceptionlessClient} from 'exceptionless/dist/exceptionless';

const exLessClient = ExceptionlessClient.default;

function isPromise(ret) {
  return (ret && typeof ret.then === 'function' && typeof ret.catch === "function")
}

const errorHandler = (error, vm, info) => {
  console.error('抛出全局异常', 'vm=', vm, 'info=', info)
  console.error(error)
  exLessClient.submitException(error)
}

function registerActionHandle(actions) {
  Object.keys(actions).forEach(key => {
    let fn = actions[key]
    actions[key] = function (...args) {
      let ret = fn.apply(this, args)
      if (isPromise(ret)) {
        return ret.catch(errorHandler)
      } else { // 默认错误处理
        return ret
      }
    }
  })
}

const registerVuex = (instance) => {
  if (instance.$options['store']) {
    let actions = instance.$options['store']['_actions'] || {}
    if (actions) {
      let tempActions = {}
      Object.keys(actions).forEach(key => {
        tempActions[key] = actions[key][0]
      })
      registerActionHandle(tempActions)
    }
  }
}
const registerVue = (instance) => {
  if (instance.$options.methods) {
    let actions = instance.$options.methods || {}
    if (actions) {
      registerActionHandle(actions)
    }
  }
}

let GlobalError = {
  install: (Vue, options) => {
    /**
     * 全局异常处理
     * @param {*} error
     * @param {*} vm
     */
    Vue.config.errorHandler = errorHandler
    Vue.mixin({
      beforeCreate() {
        registerVue(this)
        registerVuex(this)
      }
    })
    Vue.prototype.$throw = errorHandler
  }
}

export default GlobalError

Register it in main.js

import ErrorHandler from "./utils/errorHandler"

// 全局异步处理模块
Vue.use(ErrorHandler)

Done~

References

_EOF_

Integrating ExceptionLess Monitoring into a Vue 2.x Project

本文作者:
程序设计实验室
版权声明:
本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

相关文章

使用国内NPM镜像源提高速度

NPM介绍 npm(全称 Node Package Manager,即“node包管理器”)是Node.js默认的、以JavaScript编写的软件包管理系统。 npm可以管理本地项目的所需模块并自动维护依赖情况,也可以管理全局安装的JavaScript工具。 但是由于npm的源在国外,所以国内使用起来动辄10kb/s,甚至根本下载不了,所以使用国内镜像源替代官方源就很有必要了。 国内源 淘宝np

2022-02-25 · Web前端 · 约 1 分钟

魔改了一下bootstrap-treeview组件,发布个NPM包体验一下

前言 之前在这篇文章 基于.NetCore开发博客项目 StarBlog - (8) 分类层级结构展示 中说到,我为了让文章分类列表支持层级结构,用了一个树形组件,不过这个组件太老了,使用的Bootstrap版本居然是3.x的,但又找不到更好的,只能硬着头皮用。 但实际效果有很多我不满意的,人家作者直接放弃了,在GitHub上标记项目为Archive状态,没辙。 感谢开源,我直接fork一份代码来

2022-05-27 · Web前端 · 约 1 分钟

讨论

共 0 条

良言一句三冬暖,恶语伤人六月寒。

加载评论中…