Skip to content

Commit

Permalink
improve: LDP-2496: Add server error logging
Browse files Browse the repository at this point in the history
  • Loading branch information
vloss3 committed Apr 18, 2024
1 parent 0e9c3e8 commit 882dd7e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fileURLToPath } from 'url'
import { defineNuxtModule, addPlugin, createResolver, addImportsDir, addServerHandler, addServerImports, addImports } from '@nuxt/kit'
import { defineNuxtModule, addPlugin, addServerPlugin, createResolver, addImportsDir, addServerHandler } from '@nuxt/kit'
import { defu } from 'defu'
import type { NuxtOptionsWithDrupalCe } from './types'

Expand Down Expand Up @@ -58,6 +58,7 @@ export default defineNuxtModule<ModuleOptions>({
const runtimeDir = fileURLToPath(new URL('./runtime', import.meta.url))
nuxt.options.build.transpile.push(runtimeDir)
addPlugin(resolve(runtimeDir, 'plugin'))
addServerPlugin(resolve(runtimeDir, 'server/plugins/errorHandler'))
addImportsDir(resolve(runtimeDir, 'composables/useDrupalCe'))

nuxt.options.runtimeConfig.public.drupalCe = defu(nuxt.options.runtimeConfig.public.drupalCe ?? {}, options)
Expand Down
19 changes: 19 additions & 0 deletions src/runtime/server/plugins/errorHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { getDrupalBaseUrl } from '../../composables/useDrupalCe/server'
import { useRuntimeConfig, defineNitroPlugin } from '#imports'

export default defineNitroPlugin((nitro: any) => {
const { ceApiEndpoint } = useRuntimeConfig().public.drupalCe

nitro.hooks.hook('error', (error: any, { event }: any) => {
const url = getDrupalBaseUrl() + ceApiEndpoint + event?.path
console.error(`[${event?.node.req.method}] ${url} - ${error}`)
})

if (nitro.h3App.options.debug) {
// Log all requests when in debug mode.
nitro.hooks.hook('render:response', (response: any, { event }: any) => {
const url = getDrupalBaseUrl() + ceApiEndpoint + event.path
console.log(`[${response.statusCode}] [${event.node.req.method}] ${url}`)
})
}
})

0 comments on commit 882dd7e

Please sign in to comment.