Repository: freeCodeCamp/freeCodeCamp
File: api/src/routes/donate.ts (TypeScript)

Code before:
        });
        if (!donation)
          throw Error(`Stripe donation record not found: ${req.user?.id}`);
        const { customerId, subscriptionId } = donation;
        const session = await stripe.checkout.sessions.create({
          payment_method_types: ['card'],
          mode: 'setup',
          customer: customerId,
          setup_intent_data: {
            metadata: {
              customer_id: customerId,
              subscription_id: subscriptionId
            }
          },
          success_url: `${HOME_LOCATION}/update-stripe-card?session_id={CHECKOUT_SESSION_ID}`,
          cancel_url: `${HOME_LOCATION}/update-stripe-card`
        });
        return { sessionId: session.id } as const;
      } catch (error) {
        fastify.log.error(error);
        void reply.code(500);
        return {
          type: 'danger',
          message: 'Something went wrong.'
        } as const;
      }
    }
  );

  fastify.post(
    '/donate/add-donation',
    {
      schema: {
        body: Type.Object({}),
        response: {
          200: Type.Object({
            isDonating: Type.Boolean()
          }),
          400: Type.Object({
            message: Type.Literal('User is already donating.'),
            type: Type.Literal('info')
          }),
          500: Type.Object({
            message: Type.Literal('Something went wrong.'),
            type: Type.Literal('danger')
          })
        }
      }
    },
    async (req, reply) => {
      try {

Review comment (suggestion):
```suggestion
    async req => {
      const donation = await fastify.prisma.donation.findFirst({
        where: { userId: req.user?.id }
      });
      if (!donation)
        throw Error(`Stripe donation record not found: ${req.user?.id}`);
      const { customerId, subscriptionId } = donation;
      const session = await stripe.checkout.sessions.create({
        payment_method_types: ['card'],
        mode: 'setup',
        customer: customerId,
        setup_intent_data: {
          metadata: {
            customer_id: customerId,
            subscription_id: subscriptionId
          }
        },
        success_url: `${HOME_LOCATION}/update-stripe-card?session_id={CHECKOUT_SESSION_ID}`,
        cancel_url: `${HOME_LOCATION}/update-stripe-card`
      });
      return { sessionId: session.id } as const;
```
Everything gets a bit simpler if we simply throw any errors. The handler will return `{ message: 'flash.generic-error', type: 'danger' }` and the client handles that the same as any other error response.

Code after:
      if (!donation)
        throw Error(`Stripe donation record not found: ${req.user?.id}`);
      const { customerId, subscriptionId } = donation;
      const session = await stripe.checkout.sessions.create({
        payment_method_types: ['card'],
        mode: 'setup',
        customer: customerId,
        setup_intent_data: {
          metadata: {
            customer_id: customerId,
            subscription_id: subscriptionId
          }
        },
        success_url: `${HOME_LOCATION}/update-stripe-card?session_id={CHECKOUT_SESSION_ID}`,
        cancel_url: `${HOME_LOCATION}/update-stripe-card`
      });
      return { sessionId: session.id } as const;
    }
  );

  fastify.post(
    '/donate/add-donation',
    {
      schema: {
        body: Type.Object({}),
        response: {
          200: Type.Object({
            isDonating: Type.Boolean()
          }),
          400: Type.Object({
            message: Type.Literal('User is already donating.'),
            type: Type.Literal('info')
          }),
          500: Type.Object({
            message: Type.Literal('Something went wrong.'),
            type: Type.Literal('danger')
          })
        }
      }
    },
    async (req, reply) => {
      try {
        const user = await fastify.prisma.user.findUnique({
          where: { id: req.user?.id }
        });

        if (user?.isDonating) {
          void reply.code(400);
          return {
            type: 'info',
            message: 'User is already donating.'