Skip to content

fix(api): move origin outside try block in email subscription handlers#67624

Merged
majestic-owl448 merged 3 commits into
freeCodeCamp:mainfrom
Nithin0620:fix/email-subscription-origin-scope
Jun 18, 2026
Merged

fix(api): move origin outside try block in email subscription handlers#67624
majestic-owl448 merged 3 commits into
freeCodeCamp:mainfrom
Nithin0620:fix/email-subscription-origin-scope

Conversation

@Nithin0620

@Nithin0620 Nithin0620 commented May 27, 2026

Copy link
Copy Markdown
Contributor

Description

In both the /ue/:unsubscribeId and /resubscribe/:unsubscribeId handlers in email-subscription.ts, origin was declared inside the try block. Since const is block-scoped, the catch block could not access it.

If an unexpected error occurred (for example, a Prisma connection failure), the catch block itself would throw:

ReferenceError: origin is not defined

This caused the request to return a 500 response instead of performing a graceful redirect with an error message.

Before

async (req, reply) => {
  try {
    const { origin } = getRedirectParams(req); // scoped to try block only
    // ...
  } catch (error) {
    return reply.redirectWithMessage(origin, { ... }); // ❌ ReferenceError
  }
}

After

async (req, reply) => {
  const { origin } = getRedirectParams(req); // accessible throughout handler

  try {
    // ...
  } catch (error) {
    return reply.redirectWithMessage(origin, { ... }); // ✅ works correctly
  }
}

Changes

  • Moved getRedirectParams(req) outside the try block in the unsubscribe handler
  • Moved getRedirectParams(req) outside the try block in the resubscribe handler

Affected File

  • api/src/routes/public/email-subscription.ts

Checklist:

Closes #67623

@Nithin0620 Nithin0620 requested review from a team as code owners May 27, 2026 16:35
@github-actions github-actions Bot added platform: api Server application that needs familiarity with Express, Fastify, MongoDB etc. deprioritized PR reviews are deprioritized; lacks productive input and ignores codebase best practices. labels May 27, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Hi there,

Thanks for opening this pull request.

The automated checks found some issues:

Checklist: The PR description is missing the required checklist or some of its items are not completed:

  1. The Checklist: heading is present in the PR description.
  2. The checkbox items are ticked (changed from [ ] to [x]).
  3. You have actually completed the items in the checklist.

Please edit your PR description to include the following template with the checklist items completed.

Checklist:

<!-- Please follow this checklist and put an x in each of the boxes, like this: [x]. It will ensure that our team takes your pull request seriously. -->

- [ ] I have read and followed the [contribution guidelines](https://contribute.freecodecamp.org).
- [ ] I have read and followed the [how to open a pull request guide](https://contribute.freecodecamp.org/how-to-open-a-pull-request/).
- [ ] My pull request targets the `main` branch of freeCodeCamp.
- [ ] I have tested these changes either locally on my machine, or GitHub Codespaces.

<!--If your pull request closes a GitHub issue, replace the XXXXX below with the issue number.-->

Closes #XXXXX

<!-- Feel free to add any additional description of changes below this line -->

Linked Issue: The linked issue is not open for contribution. If you are looking for issues to contribute to, please check out issues labeled help wanted or first timers only.


Join us in our chat room or our forum if you have any questions or need help with contributing.

@gikf gikf added the status: waiting review To be applied to PR's that are ready for QA, especially when additional review is pending. label Jun 8, 2026
@majestic-owl448 majestic-owl448 removed the deprioritized PR reviews are deprioritized; lacks productive input and ignores codebase best practices. label Jun 18, 2026

@majestic-owl448 majestic-owl448 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution to the page! 👍
We are happy to accept these changes and look forward to future contributions. 🎉

@majestic-owl448 majestic-owl448 merged commit 81aa296 into freeCodeCamp:main Jun 18, 2026
11 checks passed
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Jun 18, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

platform: api Server application that needs familiarity with Express, Fastify, MongoDB etc. status: waiting review To be applied to PR's that are ready for QA, especially when additional review is pending.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Move origin extraction outside try block in email subscription handlers

4 participants