fix logger on contributionMessageResolver

This commit is contained in:
Ulf Gebhardt 2023-02-03 00:13:25 +01:00
parent ee8a338ec4
commit 33e87ddd92
Signed by: ulfgebhardt
GPG Key ID: DA6B843E748679C9
2 changed files with 42 additions and 31 deletions

View File

@ -88,6 +88,7 @@ describe('ContributionMessageResolver', () => {
describe('input not valid', () => {
it('throws error when contribution does not exist', async () => {
jest.clearAllMocks()
await expect(
mutate({
mutation: adminCreateContributionMessage,
@ -98,16 +99,20 @@ describe('ContributionMessageResolver', () => {
}),
).resolves.toEqual(
expect.objectContaining({
errors: [
new GraphQLError(
'ContributionMessage was not successful: Error: Contribution not found',
),
],
errors: [new GraphQLError('ContributionMessage was not sent successfully')],
}),
)
})
it('logs the error thrown', () => {
expect(logger.error).toBeCalledWith(
'ContributionMessage was not sent successfully',
new Error('Contribution not found'),
)
})
it('throws error when contribution.userId equals user.id', async () => {
jest.clearAllMocks()
await mutate({
mutation: login,
variables: { email: 'peter@lustig.de', password: 'Aa12345_' },
@ -130,14 +135,17 @@ describe('ContributionMessageResolver', () => {
}),
).resolves.toEqual(
expect.objectContaining({
errors: [
new GraphQLError(
'ContributionMessage was not successful: Error: Admin can not answer on own contribution',
),
],
errors: [new GraphQLError('ContributionMessage was not sent successfully')],
}),
)
})
it('logs the error thrown', () => {
expect(logger.error).toBeCalledWith(
'ContributionMessage was not sent successfully',
new Error('Admin can not answer on his own contribution'),
)
})
})
describe('valid input', () => {
@ -210,6 +218,7 @@ describe('ContributionMessageResolver', () => {
describe('input not valid', () => {
it('throws error when contribution does not exist', async () => {
jest.clearAllMocks()
await expect(
mutate({
mutation: createContributionMessage,
@ -220,16 +229,20 @@ describe('ContributionMessageResolver', () => {
}),
).resolves.toEqual(
expect.objectContaining({
errors: [
new GraphQLError(
'ContributionMessage was not successful: Error: Contribution not found',
),
],
errors: [new GraphQLError('ContributionMessage was not sent successfully')],
}),
)
})
it('logs the error thrown', () => {
expect(logger.error).toBeCalledWith(
'ContributionMessage was not sent successfully',
new Error('Contribution not found'),
)
})
it('throws error when other user tries to send createContributionMessage', async () => {
jest.clearAllMocks()
await mutate({
mutation: login,
variables: { email: 'peter@lustig.de', password: 'Aa12345_' },
@ -244,14 +257,17 @@ describe('ContributionMessageResolver', () => {
}),
).resolves.toEqual(
expect.objectContaining({
errors: [
new GraphQLError(
'ContributionMessage was not successful: Error: Can not send message to contribution of another user',
),
],
errors: [new GraphQLError('ContributionMessage was not sent successfully')],
}),
)
})
it('logs the error thrown', () => {
expect(logger.error).toBeCalledWith(
'ContributionMessage was not sent successfully',
new Error('Can not send message to contribution of another user'),
)
})
})
describe('valid input', () => {

View File

@ -16,6 +16,7 @@ import { backendLogger as logger } from '@/server/logger'
import { RIGHTS } from '@/auth/RIGHTS'
import { Context, getUser } from '@/server/context'
import { sendAddedContributionMessageEmail } from '@/emails/sendEmailVariants'
import LogError from '@/server/LogError'
@Resolver()
export class ContributionMessageResolver {
@ -54,8 +55,7 @@ export class ContributionMessageResolver {
await queryRunner.commitTransaction()
} catch (e) {
await queryRunner.rollbackTransaction()
logger.error(`ContributionMessage was not successful: ${e}`)
throw new Error(`ContributionMessage was not successful: ${e}`)
throw new LogError('ContributionMessage was not sent successfully', e)
} finally {
await queryRunner.release()
}
@ -95,9 +95,7 @@ export class ContributionMessageResolver {
@Ctx() context: Context,
): Promise<ContributionMessage> {
const user = getUser(context)
if (!user.emailContact) {
user.emailContact = await UserContact.findOneOrFail({ where: { id: user.emailId } })
}
const queryRunner = getConnection().createQueryRunner()
await queryRunner.connect()
await queryRunner.startTransaction('REPEATABLE READ')
@ -108,12 +106,10 @@ export class ContributionMessageResolver {
relations: ['user'],
})
if (!contribution) {
logger.error('Contribution not found')
throw new Error('Contribution not found')
throw new LogError('Contribution not found', contributionId)
}
if (contribution.userId === user.id) {
logger.error('Admin can not answer on own contribution')
throw new Error('Admin can not answer on own contribution')
throw new LogError('Admin can not answer on his own contribution', contributionId)
}
if (!contribution.user.emailContact) {
contribution.user.emailContact = await UserContact.findOneOrFail({
@ -149,8 +145,7 @@ export class ContributionMessageResolver {
await queryRunner.commitTransaction()
} catch (e) {
await queryRunner.rollbackTransaction()
logger.error(`ContributionMessage was not successful: ${e}`)
throw new Error(`ContributionMessage was not successful: ${e}`)
throw new LogError('ContributionMessage was not sent successfully', e)
} finally {
await queryRunner.release()
}