Adding error ntfy on email client error

This commit is contained in:
Collin Duncan 2023-09-26 15:24:52 +02:00
parent d9b62e6c6a
commit 5a90d16872
No known key found for this signature in database
3 changed files with 18 additions and 1 deletions

View file

@ -2,6 +2,7 @@ import { Inject, Injectable } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
import * as Imap from 'imap'
import { MailParser, ParsedEmail } from 'mailparser-mit'
import { NtfyProvider } from 'src/ntfy/provider'
import { LoggerService } from '../logger/service.logger'
import { Email } from './types'
@ -23,6 +24,9 @@ export class EmailClient {
@Inject(LoggerService)
private readonly loggerService: LoggerService,
@Inject(NtfyProvider)
private readonly ntfyProvider: NtfyProvider,
@Inject(ConfigService)
private readonly configService: ConfigService,
) {
@ -230,8 +234,9 @@ export class EmailClient {
}
})
this.imapClient.on('error', (error: Error) => {
this.imapClient.on('error', async (error: Error) => {
this.loggerService.error(`Error with imap client ${error.message}`)
await this.ntfyProvider.sendEmailClientErrorNotification(error.message)
this.setStatus(EmailClientStatus.Error)
})
}

View file

@ -2,6 +2,7 @@ import { BullModule } from '@nestjs/bull'
import { Module } from '@nestjs/common'
import { LoggerModule } from '../logger/module'
import { NtfyModule } from '../ntfy/module'
import { EmailClient } from './client'
import { EMAILS_QUEUE_NAME } from './config'
import { EmailProvider } from './provider'
@ -10,6 +11,7 @@ import { EmailProvider } from './provider'
imports: [
LoggerModule,
BullModule.registerQueueAsync({ name: EMAILS_QUEUE_NAME }),
NtfyModule,
],
providers: [EmailClient, EmailProvider],
exports: [EmailClient, EmailProvider],

View file

@ -131,4 +131,14 @@ export class NtfyProvider implements OnApplicationBootstrap {
}),
)
}
async sendEmailClientErrorNotification(errorMessage: string) {
await this.publishQueue.add(
...NtfyProvider.defaultJob({
title: 'Email client error',
message: errorMessage,
tags: [MessageTags.exclamation],
}),
)
}
}