Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

137
Vistas
Can you wrap all typescript instance methods into a single custom error handler?

I would like to ensure that, even when it screws up, the event tracking code in my front end never causes an error that impacts the rest of the application. The event tracking is all handled by a few custom classes that deal with labeling evenings and adding the correct properties:

export class TaskTrackerService {

  constructor(private abstractTracker: TrackerService) { }

  // ------
  created(task: Task): void {
    this. abstractTracker.track('Task created', this.taskProps(task))
  }
  // ------
  edited(task: Task): void {
    this.abstractTracker.track('Task edited', this.taskProps(task))
  }

  // ------
  private taskProps(task: Task): EventProperties{
    return {
      id: task.id,
      task_title_char_count: task.title.length,
      task_duration_seconds: task.duration,
      completed: task.done,
      created_at: new Date(task.created_at)
    }
  }
}

This class contains logic which could cause errors (e.g. task.created_at is null in the code above) and I want to ensure I catch these before they cause damage.

I want to neatly firewall this code

I want to ensure that any errors that are triggered in the tracking class don't propagate anywhere else in the app.

The only way I can think to do this is to add error handling for each individual tracking method as shown below:

export class TaskTrackerService {

  constructor(private abstractTracker: TrackerService) { }

  // ------
  created(task: Task): void {
    try{
      this.abstractTracker.track('Task created', this.taskProps(task))
    } catch(error){
      this.abstractTracker.trackError('Error occurred')
    }
  }
  ...
}

Is there any way to wrap all instance-methods in a single, custom error handler?

To reduce the amount of boilerplate code, I'd like some means to ensure that all of the instanc methods bubble their errors into a class-level handler. Something like this:

export class TaskTrackerService {

  constructor(private abstractTracker: TrackerService) { }

  // I know this isn't possible but...
  catchAll(error){
    this.abstractTracker.trackError('Error occurred')
  }

  // ------
  created(task: Task): void {
    this.abstractTracker.track('Task created', this.taskProps(task))
  }
  ...
}

Is anything like this possible?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda