1 | import type Raven from 'raven'
|
2 |
|
3 | import os from 'os'
|
4 |
|
5 | import {
|
6 | log,
|
7 | } from './config'
|
8 | import {
|
9 | VERSION,
|
10 | GIT_COMMIT_HASH,
|
11 | } from './version'
|
12 |
|
13 | let raven: typeof Raven
|
14 |
|
15 | function getRavenDsn (): undefined | string {
|
16 |
|
17 | const dsn = process.env['WECHATY_RAVEN_DSN']
|
18 | return dsn
|
19 | }
|
20 |
|
21 | function enableRaven (dsn: string):void {
|
22 | |
23 |
|
24 |
|
25 | const ravenOptions = {
|
26 | release: VERSION,
|
27 | tags: {
|
28 | git_commit: GIT_COMMIT_HASH,
|
29 | platform: process.env['WECHATY_DOCKER']
|
30 | ? 'docker'
|
31 | : os.platform(),
|
32 | },
|
33 | }
|
34 |
|
35 | raven.disableConsoleAlerts()
|
36 | raven
|
37 | .config(dsn, ravenOptions)
|
38 | .install()
|
39 | }
|
40 | function init () {
|
41 | try {
|
42 | raven = require('raven')
|
43 | } catch (e) {
|
44 |
|
45 | log.verbose('Wechaty', 'init() require("raven") not succeed, skipped.')
|
46 | return
|
47 | }
|
48 |
|
49 | const dsn = getRavenDsn()
|
50 | if (!dsn) {
|
51 | log.verbose('Wechaty', 'init() getRavenDsn() return undefined, skipped.')
|
52 | return
|
53 | }
|
54 |
|
55 | enableRaven(dsn)
|
56 | }
|
57 |
|
58 | function captureException (e: Error) {
|
59 | if (raven) {
|
60 | raven.captureException(e)
|
61 | }
|
62 | }
|
63 |
|
64 | init()
|
65 |
|
66 | export {
|
67 | captureException,
|
68 | }
|