Skip to content

Commit ff1a5dc

Browse files
authored
fix: Set http status code for API error responses containing page data (#124)
* Set status code for API error responses with page data. * test: Add test coverage for API errors containing page data.
1 parent 5f3abd7 commit ff1a5dc

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

src/runtime/composables/useDrupalCe.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import { useRuntimeConfig, useState, useFetch, navigateTo, createError, useRoute, h, resolveComponent } from '#imports'
1+
import { callWithNuxt } from '#app'
2+
import { useRuntimeConfig, useState, useFetch, navigateTo, createError, useRoute, h, resolveComponent, setResponseStatus, useNuxtApp } from '#imports'
23
export const useDrupalCe = () => {
34
/**
45
* Fetches page data from Drupal, handles redirects, errors and messages
56
* @param path Path of the Drupal page to fetch
67
* @param useFetchOptions Optional Nuxt useFetch options
78
*/
89
const fetchPage = async (path: string, useFetchOptions = {}) => {
10+
const nuxtApp = useNuxtApp()
911
const config = useRuntimeConfig()
1012
const baseURL = config.public.drupalCe.baseURL
1113

@@ -35,6 +37,7 @@ export const useDrupalCe = () => {
3537
}
3638

3739
if (error.value) {
40+
callWithNuxt(nuxtApp, setResponseStatus, [error.value.status])
3841
page.value = error.value?.data
3942
}
4043

test/errorhandling.test.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ describe('Module error handling', async () => {
88
configFile: 'nuxt.config4test'
99
})
1010
it('renders Drupal error page', async () => {
11-
const html = await $fetch('/node/404')
12-
expect(html).toContain('The requested page could not be found')
13-
// This doesn't pass yet, because the module doesn't set the status code for Drupal error pages
14-
// Uncomment when fixed
15-
// const { status } = await fetch('/node/404')
16-
// expect(status).toEqual(404)
11+
const response = await fetch('/node/404')
12+
expect(response.status).toEqual(404)
13+
// HTML returned from SSR page to contain
14+
// (same as what $fetch returns, but can't use $fetch because the promise rejects)
15+
expect(await response.text()).toContain('The requested page could not be found')
1716
})
1817
it('handles 500 statusCode', async () => {
1918
const response = await fetch('/error500')

0 commit comments

Comments
 (0)