Forem

Cover image for Tratamento de Erros (Código Limpo: Que Bruxaria é Essa?!?! - Parte 9)
ananopaisdojavascript
ananopaisdojavascript

Posted on

3 2

Tratamento de Erros (Código Limpo: Que Bruxaria é Essa?!?! - Parte 9)

Lançar erros é uma coisa boa! O que significa que algo no seu programa que não deu certo foi identificado com sucesso e avisará você ao desativar a função na "stack" atual, interrompendo o processo (no Node) e ainda notifica você com um rastreamento de pilha.

Não ignore os erros capturados

Não fazer nada com um erro capturado não dá a você a habilidade de consertar ou de reagir ao dito cujo. Colocar o erro no console.log não é muito melhor às vezes já pode se perder em um mar de coisas impressas no console. Se você coloca qualquer pedaço de código em um try/catch significa que você pensa que um erro acontece ali e portanto você deve ter um plano, ou criar um plano, para quando ocorrer.

Não é recomendável:

try {
  functionThatMightThrow();
} catch (error) {
  console.log(error);
}
Enter fullscreen mode Exit fullscreen mode

É recomendável:

try {
  functionThatMightThrow();
} catch (error) {
  // One option (more noisy than console.log):
  console.error(error);
  // Another option:
  notifyUserOfError(error);
  // Another option:
  reportErrorToService(error);
  // OR do all three!
}
Enter fullscreen mode Exit fullscreen mode

Não ignore promessas rejeitadas

Pela mesma razão você não deve ignorar os erros capturados do try/catch

Não é recomendável:

getdata()
  .then(data => {
    functionThatMightThrow(data);
  })
  .catch(error => {
    console.log(error);
  });
Enter fullscreen mode Exit fullscreen mode

É recomendável:

getdata()
  .then(data => {
    functionThatMightThrow(data);
  })
  .catch(error => {
    // One option (more noisy than console.log):
    console.error(error);
    // Another option:
    notifyUserOfError(error);
    // Another option:
    reportErrorToService(error);
    // OR do all three!
  });
Enter fullscreen mode Exit fullscreen mode

E aí? Gostaram? Até a próxima tradução! 🤗

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay