refactor: pass target instance to errorCaptured hook

This commit is contained in:
Evan You 2018-10-03 13:03:37 -04:00
parent d893046830
commit a0321b39b7
2 changed files with 9 additions and 5 deletions

View File

@ -55,7 +55,11 @@ export interface MountedComponent<D = Data, P = Data>
updated?(vnode: VNode): void updated?(vnode: VNode): void
beforeUnmount?(): void beforeUnmount?(): void
unmounted?(): void unmounted?(): void
errorCaptured?(): (err: Error, type: ErrorTypes) => boolean | void errorCaptured?(): (
err: Error,
type: ErrorTypes,
target: MountedComponent
) => boolean | void
activated?(): void activated?(): void
deactivated?(): void deactivated?(): void

View File

@ -45,17 +45,17 @@ export function handleError(
const handler = cur.errorCaptured const handler = cur.errorCaptured
if (handler) { if (handler) {
try { try {
const captured = handler.call(cur, err, type) const captured = handler.call(cur, err, type, instance)
if (captured) return if (captured) return
} catch (err2) { } catch (err2) {
logError(err2, cur, ErrorTypes.ERROR_CAPTURED) logError(err2, ErrorTypes.ERROR_CAPTURED)
} }
} }
} }
logError(err, instance, type) logError(err, type)
} }
function logError(err: Error, instance: MountedComponent, type: ErrorTypes) { function logError(err: Error, type: ErrorTypes) {
if (__DEV__) { if (__DEV__) {
const info = ErrorTypeStrings[type] const info = ErrorTypeStrings[type]
console.warn(`Unhandled error${info ? ` in ${info}` : ``}:`) console.warn(`Unhandled error${info ? ` in ${info}` : ``}:`)