feat(api): add error logs
This commit is contained in:
parent
ba7edec2cb
commit
808bf3f0ba
@ -1,5 +1,5 @@
|
||||
import { logger } from '@boring.tools/logger'
|
||||
import { OpenAPIHono } from '@hono/zod-openapi'
|
||||
|
||||
import { HTTPException } from 'hono/http-exception'
|
||||
import type { Variables } from '..'
|
||||
import { verifyAuthentication } from '../utils/authentication'
|
||||
@ -11,6 +11,8 @@ import Update from './update'
|
||||
|
||||
const app = new OpenAPIHono<{ Variables: Variables }>()
|
||||
|
||||
const changelog_logger = logger.child({ name: 'changelog' })
|
||||
|
||||
app.openapi(ById.route, async (c) => {
|
||||
const userId = verifyAuthentication(c)
|
||||
try {
|
||||
@ -18,6 +20,7 @@ app.openapi(ById.route, async (c) => {
|
||||
const result = await ById.func({ userId, id })
|
||||
return c.json(result, 200)
|
||||
} catch (error) {
|
||||
changelog_logger.error(error)
|
||||
if (error instanceof HTTPException) {
|
||||
return c.json({ message: error.message }, error.status)
|
||||
}
|
||||
@ -31,6 +34,7 @@ app.openapi(List.route, async (c) => {
|
||||
const result = await List.func({ userId })
|
||||
return c.json(result, 200)
|
||||
} catch (error) {
|
||||
changelog_logger.error(error)
|
||||
if (error instanceof HTTPException) {
|
||||
return c.json({ message: error.message }, error.status)
|
||||
}
|
||||
@ -48,6 +52,7 @@ app.openapi(Create.route, async (c) => {
|
||||
})
|
||||
return c.json(result, 201)
|
||||
} catch (error) {
|
||||
changelog_logger.error(error)
|
||||
if (error instanceof HTTPException) {
|
||||
return c.json({ message: error.message }, error.status)
|
||||
}
|
||||
@ -68,6 +73,7 @@ app.openapi(Delete.route, async (c) => {
|
||||
|
||||
return c.json({ message: 'Changelog removed' })
|
||||
} catch (error) {
|
||||
changelog_logger.error(error)
|
||||
if (error instanceof HTTPException) {
|
||||
return c.json({ message: error.message }, error.status)
|
||||
}
|
||||
@ -97,6 +103,7 @@ app.openapi(Update.route, async (c) => {
|
||||
|
||||
return c.json(result)
|
||||
} catch (error) {
|
||||
changelog_logger.error(error)
|
||||
if (error instanceof HTTPException) {
|
||||
return c.json({ message: error.message }, error.status)
|
||||
}
|
||||
|
@ -24,6 +24,9 @@ export const remove = createRoute({
|
||||
},
|
||||
description: 'Version not found',
|
||||
},
|
||||
500: {
|
||||
description: 'Internal Server Error',
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { logger } from '@boring.tools/logger'
|
||||
import { OpenAPIHono } from '@hono/zod-openapi'
|
||||
|
||||
import { HTTPException } from 'hono/http-exception'
|
||||
import type { Variables } from '../..'
|
||||
import { verifyAuthentication } from '../../utils/authentication'
|
||||
@ -10,6 +10,8 @@ import { update, updateFunc } from './update'
|
||||
|
||||
const app = new OpenAPIHono<{ Variables: Variables }>()
|
||||
|
||||
const version_logger = logger.child({ name: 'changelog_version' })
|
||||
|
||||
app.openapi(create, async (c) => {
|
||||
const userId = verifyAuthentication(c)
|
||||
try {
|
||||
@ -22,6 +24,7 @@ app.openapi(create, async (c) => {
|
||||
|
||||
return c.json(result, 201)
|
||||
} catch (error) {
|
||||
version_logger.error(error)
|
||||
if (error instanceof HTTPException) {
|
||||
return c.json({ message: error.message }, error.status)
|
||||
}
|
||||
@ -53,6 +56,7 @@ app.openapi(byId, async (c) => {
|
||||
200,
|
||||
)
|
||||
} catch (error) {
|
||||
version_logger.error(error)
|
||||
if (error instanceof HTTPException) {
|
||||
return c.json({ message: error.message }, error.status)
|
||||
}
|
||||
@ -77,6 +81,7 @@ app.openapi(update, async (c) => {
|
||||
|
||||
return c.json(result)
|
||||
} catch (error) {
|
||||
version_logger.error(error)
|
||||
if (error instanceof HTTPException) {
|
||||
return c.json({ message: error.message }, error.status)
|
||||
}
|
||||
@ -85,15 +90,23 @@ app.openapi(update, async (c) => {
|
||||
})
|
||||
|
||||
app.openapi(remove, async (c) => {
|
||||
const userId = verifyAuthentication(c)
|
||||
const id = c.req.param('id')
|
||||
const result = await removeFunc({ userId, id })
|
||||
try {
|
||||
const userId = verifyAuthentication(c)
|
||||
const id = c.req.param('id')
|
||||
const result = await removeFunc({ userId, id })
|
||||
|
||||
if (result.length === 0) {
|
||||
return c.json({ message: 'Version not found' }, 404)
|
||||
if (result.length === 0) {
|
||||
return c.json({ message: 'Version not found' }, 404)
|
||||
}
|
||||
|
||||
return c.json({ message: 'Version removed' })
|
||||
} catch (error) {
|
||||
version_logger.error(error)
|
||||
if (error instanceof HTTPException) {
|
||||
return c.json({ message: error.message }, error.status)
|
||||
}
|
||||
return c.json({ message: 'An unexpected error occurred' }, 500)
|
||||
}
|
||||
|
||||
return c.json({ message: 'Version removed' })
|
||||
})
|
||||
|
||||
export default app
|
||||
|
Loading…
Reference in New Issue
Block a user