diff --git a/.changeset/bright-windows-listen.md b/.changeset/bright-windows-listen.md new file mode 100644 index 000000000..9ac6265ae --- /dev/null +++ b/.changeset/bright-windows-listen.md @@ -0,0 +1,5 @@ +--- +"@effect-app/infra": minor +--- + +fix: Wrap Error in Emailer diff --git a/packages/infra/src/Emailer/Sendgrid.ts b/packages/infra/src/Emailer/Sendgrid.ts index 2d2bc79d9..955df6256 100644 --- a/packages/infra/src/Emailer/Sendgrid.ts +++ b/packages/infra/src/Emailer/Sendgrid.ts @@ -5,7 +5,7 @@ import { Array, Effect, Equivalence, Redacted } from "effect-app" import { dropUndefinedT } from "effect-app/utils" import { inspect } from "util" import { InfraLogger } from "../logger.js" -import { Emailer } from "./service.js" +import { Emailer, SendMailError } from "./service.js" import type { EmailMsg, EmailMsgOptionalFrom, SendgridConfig } from "./service.js" const makeSendgrid = ({ apiKey, defaultFrom, defaultReplyTo, realMail, subjectPrefix }: SendgridConfig) => @@ -32,20 +32,22 @@ const makeSendgrid = ({ apiKey, defaultFrom, defaultReplyTo, realMail, subjectPr } yield* InfraLogger.logDebug("Sending email").pipe(Effect.annotateLogs("msg", inspect(renderedMsg, false, 5))) - const ret = yield* Effect.async< - [sgMail.ClientResponse, Record], - Error | sgMail.ResponseError - >( - (cb) => - void sgMail.send( - renderedMsg as any, // sue me - msg.isMultiple ?? true, - (err, result) => - err - ? cb(Effect.fail(err)) - : cb(Effect.sync(() => result)) - ) - ) + const ret = yield* Effect + .async< + [sgMail.ClientResponse, Record], + Error | sgMail.ResponseError + >( + (cb) => + void sgMail.send( + renderedMsg as any, // sue me + msg.isMultiple ?? true, + (err, result) => + err + ? cb(Effect.fail(err)) + : cb(Effect.sync(() => result)) + ) + ) + .pipe(Effect.mapError((raw) => new SendMailError({ raw }))) // const event = { // name: "EmailSent", diff --git a/packages/infra/src/Emailer/service.ts b/packages/infra/src/Emailer/service.ts index 0964ffa9c..d124a70d9 100644 --- a/packages/infra/src/Emailer/service.ts +++ b/packages/infra/src/Emailer/service.ts @@ -1,15 +1,15 @@ import type { MailContent, MailData } from "@sendgrid/helpers/classes/mail.js" import type { ResponseError } from "@sendgrid/mail" import type { Effect, NonEmptyReadonlyArray, Redacted } from "effect-app" -import { Context } from "effect-app" +import { Context, Data } from "effect-app" import type { Email } from "effect-app/Schema" -/** - * @tsplus type Emailer - * @tsplus companion Emailer.Ops - */ +export class SendMailError extends Data.TaggedError("SendMailError")<{ + readonly raw: Error | ResponseError +}> {} + export class Emailer extends Context.TagId("effect-app/Emailer") Effect + sendMail: (msg: EmailMsgOptionalFrom) => Effect }>() {} export type EmailData = Email | {