Skip to content

Commit

Permalink
fix: error server
Browse files Browse the repository at this point in the history
  • Loading branch information
riderx committed Jul 29, 2023
1 parent df8dfaa commit 6d2454e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
12 changes: 10 additions & 2 deletions server/api/blog/[slug].ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import { serverQueryContent } from '#content/server'
import type { MyCustomParsedContent } from '~/services/blog'

export default defineEventHandler(async (event) => {
if (!event.context.params?.slug) {
throw createError({
statusCode: 400,
statusMessage: 'Missing slug',
})
}
try {
const blog = await serverQueryContent<MyCustomParsedContent>(event)
.where({ slug: event.context.params?.slug ?? '' })
Expand All @@ -23,7 +29,9 @@ export default defineEventHandler(async (event) => {
}
catch (e) {
console.error(e)
event.res.statusCode = 500
return {}
throw createError({
statusCode: 500,
statusMessage: 'Internal Server Error',
})
}
})
6 changes: 4 additions & 2 deletions server/api/blogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ export default defineEventHandler(async (event) => {
}
catch (e) {
console.error(e)
event.res.statusCode = 500
return {}
throw createError({
statusCode: 500,
statusMessage: 'Internal Server Error',
})
}
})
8 changes: 5 additions & 3 deletions server/routes/robots.txt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ export default defineEventHandler(async (event) => {

try {
const data = `User-agent: *\nAllow: /\nUser-agent: *\nDisallow: /rss.xml\nSitemap: ${config.public.baseUrl}/sitemap.xml`
event.res.setHeader('Content-Type', 'text/plain')
event.node.res.setHeader('Content-Type', 'text/plain')
return data.toString()
}
catch (e) {
console.error(e)
event.res.statusCode = 500
return {}
throw createError({
statusCode: 500,
statusMessage: 'Internal Server Error',
})
}
})
8 changes: 5 additions & 3 deletions server/routes/sitemap.xml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ export default defineEventHandler(async (event) => {
})
smStream.end()
const data = await streamToPromise(smStream)
event.res.setHeader('Content-Type', 'application/xml')
event.node.res.setHeader('Content-Type', 'application/xml')
return data.toString()
}
catch (e) {
console.error(e)
event.res.statusCode = 500
return {}
throw createError({
statusCode: 500,
statusMessage: 'Internal Server Error',
})
}
})

0 comments on commit 6d2454e

Please sign in to comment.