Skip to content

Commit 582e730

Browse files
committed
fix: throw http 500 when route query fails
This prevents serving a 404 for routes that exist, but can't be loaded currently
1 parent cac84fa commit 582e730

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

playground/app/app.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ d?.b
3535
// @ts-expect-error
3636
d?.bb
3737
38-
const e: { a?: number, e: number } = { e: 4 }
38+
const e: { a?: number; e: number } = { e: 4 }
3939
const f = Math.random() < 0.5 ? a : e
4040
const g = narrowTypeByProperty(f, 'a')
4141

src/runtime/composables/useDrupalRouteQuery/index.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,23 @@ type UseDrupalRouteQueryOptions = {
1414
* Whether responses should be cached on the client.
1515
*/
1616
clientCache?: boolean
17+
18+
/**
19+
* If true, the composable will not throw any errors.
20+
*/
21+
noError?: boolean
1722
}
1823

1924
/**
2025
* Performs a route query with the given name.
2126
*
2227
* Route queries are managed in nuxt.config.ts, in the vuepal.drupalRoute.routeQueries option.
28+
*
29+
* The composable will throw these errors:
30+
* - 500: The route query itself has an error (e.g. backend is down)
31+
* - 404: Route does not exist in Drupal or there is no entity.
32+
*
33+
* You can prevent throwing any errors by passing `{ noError: true }` as the second argument.
2334
*/
2435
export async function useDrupalRouteQuery<
2536
T extends ValidRouteQueryName,
@@ -28,11 +39,12 @@ export async function useDrupalRouteQuery<
2839
name: T,
2940
options?: UseDrupalRouteQueryOptions,
3041
): Promise<UseDrupalRoute<E | undefined>> {
42+
const noError = !!options?.noError
3143
const route = useRoute()
3244

3345
const queryName = mapping[name]
3446

35-
const { data } = await useAsyncGraphqlQuery(
47+
const { data, error } = await useAsyncGraphqlQuery(
3648
queryName,
3749
{
3850
path: route.path,
@@ -48,15 +60,34 @@ export async function useDrupalRouteQuery<
4860
},
4961
)
5062

63+
// Throw an error when there was an error with the query, so that we
64+
// dont' serve a 404 for a page that would exist, but can not be loaded.
65+
if (error.value && !noError) {
66+
throw createError({
67+
statusCode: 500,
68+
statusMessage: error.value.message,
69+
fatal: true,
70+
})
71+
}
72+
5173
// Don't pass the reactive object, because it will cause a 404 being thrown
5274
// by the watcher in useDrupalRoute().
53-
const ctx = await useDrupalRoute<E>(data.value, null, route)
75+
const ctx = await useDrupalRoute<E>(
76+
data.value,
77+
{
78+
// @ts-expect-error issue with overload, boolean is correct.
79+
noError,
80+
},
81+
route,
82+
)
5483

5584
// We only support EntityCanonicalUrl in this query.
5685
if (
86+
!noError &&
5787
data.value &&
5888
'route' in data.value &&
5989
data.value.route &&
90+
data.value.route.__typename &&
6091
!['EntityCanonicalUrl', 'DefaultEntityUrl'].includes(
6192
data.value.route?.__typename,
6293
)

0 commit comments

Comments
 (0)