Skip to content

Fix: prevent applyFormatting from crashing on string or undefined input #1659

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from

Conversation

JrStarkmidia
Copy link

@JrStarkmidia JrStarkmidia commented Jun 26, 2025

Esse PR corrige o erro Cannot read properties of undefined (reading 'applyFormatting') que ocorre quando o Typebot envia mensagens simples em formato de string ou valores indefinidos.

A função applyFormatting agora verifica se o valor é uma string ou falsy antes de tentar aplicar formatação. Isso evita que o sistema quebre ao tentar acessar .text ou .children de uma string.

Correção útil para integração com Typebot, ChatGPT e outras automações que retornam texto direto.

Summary by Sourcery

Bug Fixes:

  • Guard applyFormatting against string and falsy values to avoid reading properties of undefined

…ormatting-e-proteger-entrada

Corrige erro em applyFormatting com valores não estruturados

Esta correção adiciona verificação de tipo e valor falsy antes de aplicar o processamento de texto.

Correção baseada em:
EvolutionAPI#1624
Copy link
Contributor

sourcery-ai bot commented Jun 26, 2025

Reviewer's Guide

Introduce early input validation in applyFormatting to safely handle string and falsy values, preventing runtime errors when processing simple or undefined message inputs.

Class diagram for updated applyFormatting method in TypebotService

classDiagram
    class TypebotService {
        - applyFormatting(element: any): string
    }
    TypebotService : +applyFormatting(element)
    %% The method now returns the element if it's a string, returns '' if falsy, otherwise processes as before.
Loading

File-Level Changes

Change Details Files
Guard applyFormatting against non-object inputs
  • Return raw string when input is a string
  • Return empty string when input is falsy
src/api/integrations/chatbot/typebot/services/typebot.service.ts

Possibly linked issues

  • Docker folder is deleted #1: The PR fixes the TypeError in applyFormatting that happens when Typebot sends string or undefined messages, resolving the issue.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @JrStarkmidia - I've reviewed your changes - here's some feedback:

  • Consider replacing the any type on element with a more specific union (e.g. string | RichTextNode | undefined) so TypeScript can help catch invalid inputs at compile time.
  • You could simplify the guard clause by combining the falsy and string checks into a single condition (if (!element || typeof element === 'string')).
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider replacing the `any` type on `element` with a more specific union (e.g. `string | RichTextNode | undefined`) so TypeScript can help catch invalid inputs at compile time.
- You could simplify the guard clause by combining the falsy and string checks into a single condition (`if (!element || typeof element === 'string')`).

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@JrStarkmidia
Copy link
Author

Olá! Envio esta correção para evitar erros ao processar respostas do Typebot que são strings simples ou indefinidas.
Ela adiciona verificação de tipo e ajusta a assinatura da função applyFormatting.

Sugestão aplicada conforme feedback automático do bot Sourcery.
Agradeço pela análise e fico à disposição para ajustes se necessário 🙌

@DavidsonGomes DavidsonGomes changed the base branch from main to develop June 27, 2025 12:55
@Santosl2
Copy link
Contributor

Santosl2 commented Jun 29, 2025

A sua correção não corrige o erro informado, que é Cannot read properties of undefined (reading 'applyFormatting') o correção desse erro está em #1670

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants