diff --git a/backend/src/graphql/resolvers/posts.spec.ts b/backend/src/graphql/resolvers/posts.spec.ts index 7f679d2b9..01550b04a 100644 --- a/backend/src/graphql/resolvers/posts.spec.ts +++ b/backend/src/graphql/resolvers/posts.spec.ts @@ -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', () => { diff --git a/backend/src/graphql/resolvers/posts.ts b/backend/src/graphql/resolvers/posts.ts index cef255634..b70d2bf12 100644 --- a/backend/src/graphql/resolvers/posts.ts +++ b/backend/src/graphql/resolvers/posts.ts @@ -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') }