Skip to content

Commit 4928989

Browse files
committed
improve vue 3 lifecycle , expose vue integration
1 parent 9470ea7 commit 4928989

File tree

2 files changed

+78
-18
lines changed

2 files changed

+78
-18
lines changed

src/catcher.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ export default class Catcher {
3434
*/
3535
public readonly version: string = VERSION;
3636

37+
/**
38+
* Vue.js integration instance
39+
*/
40+
public vue: VueIntegration | null = null;
41+
3742
/**
3843
* Catcher Type
3944
*/
@@ -202,7 +207,7 @@ export default class Catcher {
202207
*/
203208
public connectVue(vue): void {
204209
// eslint-disable-next-line no-new
205-
new VueIntegration(vue, (error: Error, addons: VueIntegrationAddons) => {
210+
this.vue = new VueIntegration(vue, (error: Error, addons: VueIntegrationAddons) => {
206211
void this.formatAndSend(error, {
207212
vue: addons,
208213
});

src/integrations/vue.ts

Lines changed: 72 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,24 @@ export class VueIntegration {
4949
}
5050
}
5151

52+
/**
53+
* Extract additional useful information from the Vue app
54+
*
55+
* Can be used outside of this class, for example, by Nuxt integration
56+
*
57+
* @param vm - component instance
58+
* @param info - a Vue-specific error info, e.g. which lifecycle hook the error was found in.
59+
*/
60+
public spoilAddons(vm: { [key: string]: unknown }, info: string): VueIntegrationAddons {
61+
const isVue3 = vm.$ !== undefined;
62+
63+
if (isVue3) {
64+
return this.spoilAddonsFromVue3(vm, info);
65+
} else {
66+
return this.spoilAddonsFromVue2(vm, info);
67+
}
68+
}
69+
5270
/**
5371
* Setups event handlers for Vue.js instance
5472
*/
@@ -75,22 +93,6 @@ export class VueIntegration {
7593
};
7694
}
7795

78-
/**
79-
* Extract additional useful information from the Vue app
80-
*
81-
* @param vm - component instance
82-
* @param info - a Vue-specific error info, e.g. which lifecycle hook the error was found in.
83-
*/
84-
private spoilAddons(vm: { [key: string]: unknown }, info: string): VueIntegrationAddons {
85-
const isVue3 = vm.$ !== undefined;
86-
87-
if (isVue3) {
88-
return this.spoilAddonsFromVue3(vm, info);
89-
} else {
90-
return this.spoilAddonsFromVue2(vm, info);
91-
}
92-
}
93-
9496
/**
9597
* Extract additional useful information from the Vue 2 app
9698
*
@@ -155,7 +157,7 @@ export class VueIntegration {
155157
// eslint-disable-next-line @typescript-eslint/no-explicit-any
156158
private spoilAddonsFromVue3(vm: { [key: string]: any }, info: string): VueIntegrationAddons {
157159
const addons: VueIntegrationAddons = {
158-
lifecycle: info,
160+
lifecycle: this.getRuntimeErrorSourceByCode(info),
159161
component: null,
160162
};
161163

@@ -176,6 +178,59 @@ export class VueIntegration {
176178
return addons;
177179
}
178180

181+
/**
182+
* In production, the error code is a link with reference to doc.
183+
* This method returns the error message by the code extracted from the link
184+
*
185+
* @param code - Error source info (3rd argument of the vue:error hook)
186+
* https://vuejs.org/api/composition-api-lifecycle.html#onerrorcaptured
187+
*/
188+
private getRuntimeErrorSourceByCode(code: string): string {
189+
if (!code.includes('https://vuejs.org/error-reference/#runtime-')) {
190+
return code;
191+
}
192+
193+
const codeParts = code.split('https://vuejs.org/error-reference/#runtime-');
194+
const errorCode = codeParts[codeParts.length - 1];
195+
196+
const errorCodeMap = new Map([
197+
['0', 'setup function'],
198+
['1', 'render function'],
199+
['2', 'watcher getter'],
200+
['3', 'watcher callback'],
201+
['4', 'watcher cleanup function'],
202+
['5', 'native event handler'],
203+
['6', 'component event handler'],
204+
['7', 'vnode hook'],
205+
['8', 'directive hook'],
206+
['9', 'transition hook'],
207+
['10', 'app errorHandler'],
208+
['11', 'app warnHandler'],
209+
['12', 'ref function'],
210+
['13', 'async component loader'],
211+
['14', 'scheduler flush'],
212+
['15', 'component update'],
213+
['16', 'app unmount cleanup function'],
214+
['sp', 'serverPrefetch hook'],
215+
['bc', 'beforeCreate hook'],
216+
['c', 'created hook'],
217+
['bm', 'beforeMount hook'],
218+
['m', 'mounted hook'],
219+
['bu', 'beforeUpdate hook'],
220+
['u', 'updated'],
221+
['bum', 'beforeUnmount hook'],
222+
['um', 'unmounted hook'],
223+
['a', 'activated hook'],
224+
['da', 'deactivated hook'],
225+
['ec', 'errorCaptured hook'],
226+
['rtc', 'renderTracked hook'],
227+
['rtg', 'renderTriggered hook'],
228+
]);
229+
230+
return errorCodeMap.get(errorCode) || code;
231+
}
232+
233+
179234
/**
180235
* Write error to the console
181236
*

0 commit comments

Comments
 (0)