Skip to content

Commit 0cdb18f

Browse files
authored
improve: LDP-2150: Implemented custom page and menu error handlers (#155)
* improve: LDP-2150: Implement custom page and menu error handlers * LDP-2150: Added README * LDP-2150: Added override error handler parameters * LDP-2150: Added error parameter for custom error handlers * LDP-2150: Added logging for menu errors and improved README
1 parent 27c337e commit 0cdb18f

File tree

3 files changed

+58
-20
lines changed

3 files changed

+58
-20
lines changed

.eslintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"rules": {
66
"@typescript-eslint/no-unused-vars": [
77
"off"
8-
]
8+
],
9+
"no-console": "off"
910
}
1011
}

README.md

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,47 @@ is added automatically to requests. Defaults to `false`.
8686

8787
- `useLocalizedMenuEndpoint`: If enabled, the menu endpoint will use a language prefix as configured by [nuxtjs/i18n](https://v8.i18n.nuxtjs.org) module. Defaults to `true`.
8888

89-
90-
## TODO - List of 1.x options not yet implemented
91-
92-
- `pageErrorHandler`: The default page error handler can be overridden.
93-
94-
- `menuErrorHandler`: The default menu error handler can be overridden.
95-
89+
## Error handling
90+
91+
The module provides a default error handler for the `fetchPage` and `fetchMenu` methods:
92+
93+
- `fetchPage`: Throws an error with the status code and message provided by Drupal.
94+
- `fetchMenu`: Logs an error message to the console and displays a message in the frontend.
95+
96+
## Customizing error handling
97+
98+
You have the option to override the default error handlers by using a parameter when calling the `fetch` methods.
99+
100+
- `fetchPage`:
101+
```javascript
102+
<script lang="ts" setup>
103+
const { fetchPage } = useDrupalCe()
104+
105+
function customPageError (error: Record<string, any>) {
106+
throw createError({ statusCode: error.value.statusCode, statusMessage: 'No access.', data: {}, fatal: true })
107+
}
108+
const page = await fetchPage(useRoute().path, { query: useRoute().query }, customPageError)
109+
</script>
110+
```
111+
112+
- `fetchMenu`:
113+
```javascript
114+
<script lang="ts" setup>
115+
const { fetchMenu } = useDrupalCe()
116+
const { getMessages } = useDrupalCe()
117+
const messages = getMessages()
118+
119+
function customMenuError (error: Record<string, any>) {
120+
messages.value.push({
121+
type: 'error',
122+
message: `Menu error: Unavailable. ${error.value.statusCode}`
123+
})
124+
}
125+
const mainMenu = await fetchMenu('main', {}, customMenuError)
126+
</script>
127+
```
128+
129+
Note: The `error` parameter is optional and can be omitted.
96130

97131
## Previous options not supported in 2.x version
98132

src/runtime/composables/useDrupalCe.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export const useDrupalCe = () => {
2727
* @param path Path of the Drupal page to fetch
2828
* @param useFetchOptions Optional Nuxt useFetch options
2929
*/
30-
const fetchPage = async (path: string, useFetchOptions:UseFetchOptions<any> = {}) => {
30+
const fetchPage = async (path: string, useFetchOptions: UseFetchOptions<any> = {}, overrideErrorHandler?: (error?: any) => void) => {
3131
const nuxtApp = useNuxtApp()
3232

3333
// Workaround for issue - useState is not available after async call (Nuxt instance unavailable)
@@ -71,12 +71,8 @@ export const useDrupalCe = () => {
7171
return pageState
7272
}
7373

74-
if (error.value && (!error.value?.data?.content || config.customErrorPages)) {
75-
throw createError({ statusCode: error.value.statusCode, statusMessage: error.value.message, data: error.value.data, fatal: true })
76-
}
77-
7874
if (error.value) {
79-
callWithNuxt(nuxtApp, setResponseStatus, [error.value.statusCode])
75+
overrideErrorHandler ? overrideErrorHandler(error) : pageErrorHandler(error, { config, nuxtApp })
8076
page.value = error.value?.data
8177
}
8278

@@ -91,7 +87,7 @@ export const useDrupalCe = () => {
9187
* @param name Menu name being fetched
9288
* @param useFetchOptions Optional Nuxt useFetch options
9389
*/
94-
const fetchMenu = async (name: string, useFetchOptions:UseFetchOptions<any> = {}) => {
90+
const fetchMenu = async (name: string, useFetchOptions: UseFetchOptions<any> = {}, overrideErrorHandler?: (error?: any) => void) => {
9591
const nuxtApp = useNuxtApp()
9692
useFetchOptions = processFetchOptions(useFetchOptions)
9793
useFetchOptions.key = `menu-${name}`
@@ -110,21 +106,20 @@ export const useDrupalCe = () => {
110106
const { data: menu, error } = await useFetch(menuPath, useFetchOptions)
111107

112108
if (error.value) {
113-
errorMenuHandler(error)
114-
return
109+
overrideErrorHandler ? overrideErrorHandler(error) : menuErrorHandler(error)
115110
}
116111
return menu
117112
}
118113

119114
/**
120115
* Use messages state
121116
*/
122-
const getMessages = () => useState('drupal-ce-messages', () => [])
117+
const getMessages = (): Ref => useState('drupal-ce-messages', () => [])
123118

124119
/**
125120
* Use page data
126121
*/
127-
const getPage = () => useState('drupal-ce-page-data', () => ({}))
122+
const getPage = (): Ref => useState('drupal-ce-page-data', () => ({}))
128123

129124
/**
130125
* Render elements from page data returned from fetchPage
@@ -160,9 +155,17 @@ const pushMessagesToState = (messages) => {
160155
process.client && useDrupalCe().getMessages().value.push(...messagesArray)
161156
}
162157

163-
const errorMenuHandler = (error) => {
158+
const menuErrorHandler = (error: Record<string, any>) => {
159+
console.error({ statusCode: error.value.statusCode, statusMessage: error.value.message, data: error.value.data })
164160
process.client && useDrupalCe().getMessages().value.push({
165161
type: 'error',
166162
message: `Menu error: ${error.value.message}.`
167163
})
168164
}
165+
166+
const pageErrorHandler = (error: Record<string, any>, context: Record<string, any>) => {
167+
if (error.value && (!error.value?.data?.content || context.config.customErrorPages)) {
168+
throw createError({ statusCode: error.value.statusCode, statusMessage: error.value.message, data: error.value.data, fatal: true })
169+
}
170+
callWithNuxt(context.nuxtApp, setResponseStatus, [error.value.statusCode])
171+
}

0 commit comments

Comments
 (0)