-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(errorboundary): miscellaneous improvements
add onError callback and error emit, forward params to fallback component
- Loading branch information
1 parent
6dcb9aa
commit 33f40b7
Showing
7 changed files
with
102 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,59 @@ | ||
<template> | ||
<slot v-if="!error$" /> | ||
<component | ||
:is="component" | ||
v-if="error" | ||
v-else | ||
class="pdap-flex-container-center h-[full]" | ||
:v-bind="params" | ||
> | ||
<h1>Oops, something went wrong!</h1> | ||
<p class="max-w-full" data-test="error-boundary-message"> | ||
If you keep seeing this message, please email | ||
<a href="mailto:[email protected]">[email protected]</a> for assistance. | ||
</p> | ||
</component> | ||
<slot v-else /> | ||
</template> | ||
<script> | ||
export default { | ||
props: { | ||
component: { | ||
type: String, | ||
default: 'div', | ||
}, | ||
}, | ||
data() { | ||
return { | ||
error: false, | ||
}; | ||
}, | ||
/* TODO: figure out how to cover this lifecycle method in tests */ | ||
errorCaptured(error) { | ||
this.interceptError(error); | ||
}, | ||
methods: { | ||
interceptError(error) { | ||
this.error = error; | ||
}, | ||
}, | ||
}; | ||
|
||
<script setup lang="ts"> | ||
import { ComponentPublicInstance, onErrorCaptured, ref } from 'vue'; | ||
import { PdapErrorBoundaryProps } from './types'; | ||
const props = withDefaults(defineProps<PdapErrorBoundaryProps>(), { | ||
component: 'div', | ||
onError: undefined, | ||
params: undefined, | ||
stopPropagation: false, | ||
}); | ||
interface PdapErrorEmitted { | ||
error: Error; | ||
vm: ComponentPublicInstance | null; | ||
info?: string; | ||
} | ||
const emits = defineEmits<{ | ||
(event: 'error', { error, vm, info }: PdapErrorEmitted): void; | ||
}>(); | ||
const error$ = ref<Error>(); | ||
const info$ = ref<string | undefined>(''); | ||
function interceptError( | ||
error: Error, | ||
vm: ComponentPublicInstance | null, | ||
info?: string | ||
) { | ||
error$.value = error; | ||
info$.value = info; | ||
props.onError?.(error, vm); | ||
emits('error', { error: error, vm, info }); | ||
if (props.stopPropagation) return false; | ||
} | ||
/* Impossible and unwise to try testing this, so we remove from the coverage report */ | ||
/* c8 ignore next 3 */ | ||
onErrorCaptured((err, vm, info) => { | ||
interceptError(err, vm, info); | ||
}); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as ErrorBoundary } from './PdapErrorBoundary.vue'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { ComponentPublicInstance } from 'vue'; | ||
|
||
export interface PdapErrorBoundaryProps { | ||
component: string; | ||
onError?: ( | ||
error: Error, | ||
target?: ComponentPublicInstance | null | undefined | ||
) => void; | ||
params?: Record<string, string>; | ||
stopPropagation?: boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters