Skip to content

Commit

Permalink
fix: wrap Error in Emailer
Browse files Browse the repository at this point in the history
  • Loading branch information
patroza committed Dec 7, 2024
1 parent ed995e3 commit f727eab
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/bright-windows-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect-app/infra": minor
---

fix: Wrap Error in Emailer
32 changes: 17 additions & 15 deletions packages/infra/src/Emailer/Sendgrid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand All @@ -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<string, unknown>],
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<string, unknown>],
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",
Expand Down
12 changes: 6 additions & 6 deletions packages/infra/src/Emailer/service.ts
Original file line number Diff line number Diff line change
@@ -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")<Emailer, {
sendMail: (msg: EmailMsgOptionalFrom) => Effect<void, Error | ResponseError>
sendMail: (msg: EmailMsgOptionalFrom) => Effect<void, SendMailError>
}>() {}

export type EmailData = Email | {
Expand Down

0 comments on commit f727eab

Please sign in to comment.