Errors in handlers
All core error rules apply across Vue templates and scripts—JSON.parse inside {{ }} is flagged as unhandled-failure. Event handlers need special discipline because Vue invokes them with no caller around them and discards whatever they return. (For data fetching, see Async state.)
In short
- Catch at the handler boundary: never let a
PropagateErroror rejected promise escape an@clickor lifecycle hook. - Don't return a
Resultto Vue: handleisErr(...)inside the handler and reflect the failure in a single component stateEnum. - Model UI states exhaustively: render
Editing,Rejected, andPublishedstates cleanly through<Match>.
Event handlers run on their own
Vue calls a handler when the event happens, long after the code that rendered the button has finished. Nothing of yours is around it to catch an error. Vue passes what escapes to app.config.errorHandler, or logs it to the console; the user sees a button that does nothing. So a PropagateError must not leave a handler (uncaught-propagation):
✕ Rejecteduncaught-propagation
vue
<script setup lang="ts">
import { PropagateError } from "@evobug_com/safe-ts-std/result";
class NotFound extends PropagateError {}
type Item = { readonly id: string; readonly name: string };
const props = defineProps<{ items: readonly Item[] }>();
function find(id: string): Item {
const item = props.items.find((i) => i.id === id);
if (item === undefined) throw new NotFound(id);
return item;
}
function open(id: string): void {
console.log(`opening ${find(id).name}`);
}
</script>
<template>
<button v-for="item in props.items" :key="item.id" @click="open(item.id)">{{ item.name }}</button>
</template>✅ Accepted: a missing item is a state of the screen, and the template shows each state
✓ Accepted0 diagnostics
vue
<script setup lang="ts">
import { shallowRef } from "vue";
import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";
import { Match } from "@evobug_com/safe-ts-vue/match";
type Item = { readonly id: string; readonly name: string };
type Opening = Enum<{ Browsing: {}; Opened: { item: Item }; Gone: {} }>;
const Opening = constructors<Opening>();
const props = defineProps<{ items: readonly Item[] }>();
const opening = shallowRef<Opening>(Opening.Browsing());
function open(id: string): void {
const item = props.items.find((i) => i.id === id);
opening.value = item === undefined ? Opening.Gone() : Opening.Opened({ item });
}
</script>
<template>
<button v-for="item in props.items" :key="item.id" @click="open(item.id)">{{ item.name }}</button>
<Match :value="opening">
<template #Browsing>Pick an item</template>
<template #Opened="{ item }">Opening {{ item.name }}</template>
<template #Gone>That item is gone</template>
</Match>
</template>✅ Also accepted: when find is shared code that throws a PropagateError for callers that end a whole request, the handler is the top for this one, and catches it
✓ Accepted0 diagnostics
vue
<script setup lang="ts">
import { ref } from "vue";
import { PropagateError } from "@evobug_com/safe-ts-std/result";
class NotFound extends PropagateError {}
type Item = { readonly id: string; readonly name: string };
const props = defineProps<{ items: readonly Item[] }>();
const message = ref("");
function find(id: string): Item {
const item = props.items.find((i) => i.id === id);
if (item === undefined) throw new NotFound(id);
return item;
}
function open(id: string): void {
try {
message.value = `opening ${find(id).name}`;
} catch (error) {
if (!(error instanceof NotFound)) throw error;
message.value = "That item is gone";
}
}
</script>
<template>
<button v-for="item in props.items" :key="item.id" @click="open(item.id)">{{ item.name }}</button>
<p>{{ message }}</p>
</template>An async handler that can reject is the same mistake: @click="save" with an async save() that can reject is floating-rejection. Catch inside it.
Setup, render and hooks run on their own too
Vue calls a component's setup itself, and later its render function and lifecycle hooks. What they throw goes to app.config.errorHandler, not to any code of yours. So a PropagateError must not leave them either. That holds in every way a component is written:
<script setup>;defineComponent({ setup() { ... } })andcreateApp({ ... }), withsetupas a method, a function or an arrow, and the function formdefineComponent((props) => ...);export default { setup() { ... } }(ordefineComponent({ ... })) in a<script>block.
In the options object, setup, render, data, the lifecycle hooks (mounted, beforeUnmount, ...), computed getters and setters and watch handlers count, and so does the render function setup returns. methods don't: your code calls them too, so they are followed like any function.
✕ Rejecteduncaught-propagation
ts
import { defineComponent, h } from "vue";
import { PropagateError } from "@evobug_com/safe-ts-std/result";
class NotFound extends PropagateError {}
function price(id: string): number {
if (id === "") throw new NotFound("no id");
return 10;
}
export const Price = defineComponent({
props: { id: { type: String, required: true } },
setup(props) {
return () => h("p", `${price(props.id)} €`);
},
});✅ Accepted: the render function catches it and shows something else
✓ Accepted0 diagnostics
ts
import { defineComponent, h } from "vue";
import { PropagateError } from "@evobug_com/safe-ts-std/result";
class NotFound extends PropagateError {}
function price(id: string): number {
if (id === "") throw new NotFound("no id");
return 10;
}
export const Price = defineComponent({
props: { id: { type: String, required: true } },
setup(props) {
return () => {
try {
return h("p", `${price(props.id)} €`);
} catch (error) {
if (!(error instanceof NotFound)) throw error;
return h("p", "No price");
}
};
},
});An errorCaptured hook (or onErrorCaptured) in a parent component isn't counted: which parent a component ends up in is decided at runtime. The options are recognized when they are written in the call or in export default; defineComponent(options) with options declared elsewhere isn't.
A Result returned to Vue is dropped
Vue throws away whatever a handler returns, so a Result returned from @click is never looked at (must-use-result):
✕ Rejectedmust-use-result
vue
<script setup lang="ts">
import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";
import { err, type Result } from "@evobug_com/safe-ts-std/result";
type PublishError = Enum<{ Empty: {}; TooLong: { max: number } }>;
const PublishError = constructors<PublishError>();
const props = defineProps<{ draft: string }>();
function publish(): Result<void, PublishError> {
if (props.draft === "") return err(PublishError.Empty());
if (props.draft.length > 280) return err(PublishError.TooLong({ max: 280 }));
return undefined;
}
</script>
<template>
<button @click="publish">Publish</button>
</template>✅ Accepted: the component's state is one Enum of where publishing stands, and the template shows each state
✓ Accepted0 diagnostics
vue
<script setup lang="ts">
import { shallowRef } from "vue";
import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";
import { err, isErr, type Result } from "@evobug_com/safe-ts-std/result";
import { Match } from "@evobug_com/safe-ts-vue/match";
type PublishError = Enum<{ Empty: {}; TooLong: { max: number } }>;
const PublishError = constructors<PublishError>();
type Publishing = Enum<{ Editing: {}; Rejected: { error: PublishError }; Published: {} }>;
const Publishing = constructors<Publishing>();
const props = defineProps<{ draft: string }>();
const emit = defineEmits<{ publish: [text: string] }>();
const state = shallowRef<Publishing>(Publishing.Editing());
function publish(): Result<void, PublishError> {
if (props.draft === "") return err(PublishError.Empty());
if (props.draft.length > 280) return err(PublishError.TooLong({ max: 280 }));
emit("publish", props.draft);
return undefined;
}
function onPublish(): void {
const published = publish();
state.value = isErr(published) ? Publishing.Rejected({ error: published.error }) : Publishing.Published();
}
</script>
<template>
<Match :value="state">
<template #Editing>
<button @click="onPublish">Publish</button>
</template>
<template #Rejected="{ error }">
<button @click="onPublish">Publish</button>
<Match :value="error">
<template #Empty>Write something first.</template>
<template #TooLong="{ max }">Keep it under {{ max }} characters.</template>
</Match>
</template>
<template #Published>Published.</template>
</Match>
</template>The state names each place the component can be in: Editing before the first try, Rejected with the reason, Published after it worked. An Option<PublishError> would hold the error too, but its undefined would mean both "not tried yet" and "published", so the template couldn't tell them apart and success would leave no trace. The error is a named Enum, not a string: each kind can carry its data (max), and the words the user reads are written once, in the inner <Match>, which is checked against PublishError's variants like the outer one.
When the error really doesn't matter, write @click="void publish()". (Not void publish without the parentheses: that doesn't call it.)
Rules on this page: uncaught-propagation, floating-rejection, must-use-result