log post resolver errors

This commit is contained in:
Moriz Wahl 2025-06-24 13:56:57 +02:00
parent 75d106e847
commit 53e3a0fd96
2 changed files with 27 additions and 4 deletions

View File

@ -27,6 +27,12 @@ let server: ApolloServer
let authenticatedUser
let query, mutate
const loggerErrorMock: (e) => void = jest.fn()
jest.mock('@src/logger', () => ({
error: (e) => loggerErrorMock(e),
}))
beforeAll(async () => {
await cleanDatabase()
@ -1396,6 +1402,10 @@ describe('pin posts', () => {
errors: [{ message: 'Pinned posts are not allowed!' }],
})
})
it('logs the error', () => {
expect(loggerErrorMock).toBeCalledWith('Pinned posts are not allowed!')
})
})
describe('MAX_PINNED_POSTS is 1', () => {
@ -1916,6 +1926,10 @@ describe('pin posts', () => {
errors: [{ message: 'Max number of pinned posts is reached!' }],
})
})
it('logs the error', () => {
expect(loggerErrorMock).toBeCalledWith('Max number of pinned posts is reached!')
})
})
describe('post ordering', () => {

View File

@ -110,7 +110,7 @@ export default {
},
},
Mutation: {
CreatePost: async (_parent, params, context, _resolveInfo) => {
CreatePost: async (_parent, params, context: Context, _resolveInfo) => {
const { categoryIds, groupId } = params
const { image: imageInput } = params
@ -188,11 +188,14 @@ export default {
}
return post
} catch (e) {
if (e.code === 'Neo.ClientError.Schema.ConstraintValidationFailed')
if (e.code === 'Neo.ClientError.Schema.ConstraintValidationFailed') {
context.logger.error('Post with this slug already exists!')
throw new UserInputError('Post with this slug already exists!')
}
context.logger.error('CreatePost error', e.message)
throw new Error(e)
} finally {
session.close()
await session.close()
}
},
UpdatePost: async (_parent, params, context, _resolveInfo) => {
@ -344,7 +347,10 @@ export default {
}
},
pinPost: async (_parent, params, context: Context, _resolveInfo) => {
if (CONFIG.MAX_PINNED_POSTS === 0) throw new Error('Pinned posts are not allowed!')
if (CONFIG.MAX_PINNED_POSTS === 0) {
context.logger.error('Pinned posts are not allowed!')
throw new Error('Pinned posts are not allowed!')
}
let pinnedPostWithNestedAttributes
const { driver, user } = context
const session = driver.session()
@ -404,6 +410,7 @@ export default {
})
).records.map((r) => Number(r.get('count').toString()))
if (currentPinnedPostCount >= CONFIG.MAX_PINNED_POSTS) {
context.logger.error('Max number of pinned posts is reached!')
throw new Error('Max number of pinned posts is reached!')
}
const [pinPostResult] = (
@ -506,6 +513,7 @@ export default {
).records.map((record) => record.get('post'))
if (posts.length !== 1) {
context.logger.error('pushPost: Could not find Post')
throw new Error('Could not find Post')
}
@ -523,6 +531,7 @@ export default {
).records.map((record) => record.get('post'))
if (posts.length !== 1) {
context.logger.error('unpushPost: Could not find Post')
throw new Error('Could not find Post')
}