-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from fterh/notify-errors
Notify user of errors
- Loading branch information
Showing
3 changed files
with
73 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,61 @@ | ||
"use strict"; | ||
|
||
import { S3 } from "aws-sdk"; | ||
import { S3Event } from "aws-lambda"; | ||
import { simpleParser } from "mailparser"; | ||
import { email, operationalDomain } from "../lib/env"; | ||
import extractEmailAliases from "../lib/extractEmailAliases"; | ||
import processAliases from "../lib/processAliases"; | ||
import sendEmail from "../lib/sendEmail"; | ||
|
||
export const handler = async (event: S3Event) => { | ||
console.log(`Received incoming email; key=${event.Records[0].s3.object.key}`); | ||
const getRecordData = async ( | ||
request: S3.GetObjectRequest, | ||
s3: S3 | ||
): Promise<Buffer> => { | ||
console.log("Fetching email from S3 storage"); | ||
return (await s3.getObject(request).promise()).Body as Buffer; | ||
}; | ||
|
||
const deleteRecord = async ( | ||
request: S3.DeleteObjectRequest, | ||
s3: S3 | ||
): Promise<void> => { | ||
console.log("Deleting email from S3 storage"); | ||
await s3.deleteObject(request).promise(); | ||
console.log("Deleted email from S3 storage"); | ||
}; | ||
|
||
const notifyUserOfError = async (err: any): Promise<void> => { | ||
if (err instanceof Error) { | ||
console.error(err, err.stack); | ||
} else { | ||
console.error(err); | ||
} | ||
|
||
const s3 = new S3({ | ||
apiVersion: "2006-03-01", | ||
region: process.env.AWSREGION // TODO check if this should be AWS_REGION instead | ||
await sendEmail({ | ||
from: `heimdall@${operationalDomain}`, | ||
to: email, | ||
subject: "Oops, something went wrong!", | ||
text: `An error has occurred:\n\n${err}\n\nCheck the logs for more information.` | ||
}); | ||
}; | ||
|
||
export const handler = async (event: S3Event) => { | ||
const record = event.Records[0]; | ||
console.log(`Received incoming email (key=${record.s3.object.key})`); | ||
|
||
const s3 = new S3(); | ||
const request = { | ||
Bucket: record.s3.bucket.name, | ||
Key: record.s3.object.key | ||
}; | ||
|
||
try { | ||
const data = (await s3.getObject(request).promise()).Body as Buffer; | ||
const data = await getRecordData(request, s3); | ||
const email = await simpleParser(data); | ||
const aliases = extractEmailAliases(email); | ||
await processAliases(aliases, email); | ||
|
||
// Delete the email from S3 only after successfully processing it | ||
console.log("Deleting email from S3 storage"); | ||
await s3.deleteObject(request).promise(); | ||
console.log("Deleted email from S3 storage"); | ||
await deleteRecord(request, s3); | ||
} catch (err) { | ||
console.error(err); | ||
return err; | ||
await notifyUserOfError(err); | ||
} | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters