Skip to content

Commit 17b34cb

Browse files
authored
feat: add error handling page (#269)
* feat: add error handling page * fix: add files to solutions
1 parent af65c62 commit 17b34cb

File tree

15 files changed

+271
-0
lines changed

15 files changed

+271
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
ogImage: true
3+
---
4+
5+
# Error Handling
6+
7+
// TODO:
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template>
2+
<NuxtPage />
3+
</template>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<script setup lang="ts">
2+
import type { NuxtError } from '#app'
3+
4+
const props = defineProps({
5+
error: Object as () => NuxtError,
6+
})
7+
8+
const handleError = () => clearError({ redirect: '/' })
9+
</script>
10+
11+
<template>
12+
<div>
13+
<h1>{{ error.statusCode }}</h1>
14+
<p>{{ error.message }}</p>
15+
<button type="button" @click="handleError">
16+
Clear errors
17+
</button>
18+
</div>
19+
</template>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<script setup lang="ts">
2+
const { data: todos } = useFetch('/api/todos')
3+
</script>
4+
5+
<template>
6+
<ul>
7+
<li v-for="todo in todos" :key="todo.id">
8+
<NuxtLink :to="`/todo/${todo.id}`">
9+
{{ todo.title }}
10+
</NuxtLink>
11+
</li>
12+
</ul>
13+
</template>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<script setup lang="ts">
2+
const route = useRoute()
3+
const id = route.params.id
4+
const { data } = useFetch(`/api/todos/${id}`)
5+
6+
// Challenge
7+
</script>
8+
9+
<template>
10+
<h1 v-if="data">
11+
{{ data.title }}
12+
</h1>
13+
</template>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { data } from './index'
2+
3+
export default defineEventHandler((event) => {
4+
const id = Number(event.context.params?.id)
5+
const todo = data.find(item => item.id === id)
6+
return todo
7+
})
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export const data = [
2+
{ id: 1, title: 'Todo 1' },
3+
{ id: 2, title: 'Todo 2' },
4+
{ id: 3, title: 'Todo 3' },
5+
] as const
6+
7+
export default defineEventHandler(() => {
8+
return data
9+
})
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type { GuideMeta } from '~/types/guides'
2+
3+
export const meta: GuideMeta = {
4+
startingFile: 'pages/todo/[id].vue',
5+
features: {
6+
fileTree: true,
7+
},
8+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template>
2+
<NuxtPage />
3+
</template>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<script setup lang="ts">
2+
import type { NuxtError } from '#app'
3+
4+
const props = defineProps({
5+
error: Object as () => NuxtError,
6+
})
7+
8+
const handleError = () => clearError({ redirect: '/' })
9+
</script>
10+
11+
<template>
12+
<div>
13+
<h1>{{ error.statusCode }}</h1>
14+
<p>{{ error.message }}</p>
15+
<button type="button" @click="handleError">
16+
Clear errors
17+
</button>
18+
</div>
19+
</template>

0 commit comments

Comments
 (0)