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

215
Vistas
How to improve a validation functionality which uses try/catch

I built a validation function in the NodeJS app which filters out the bad data and allows the insert of the good data into the DB

Link to example of what I did Snippet

I would like to write this better with fewer try/catch if that is possible but not sure how

This is the code of the validation I'm trying to make it better

function validateTimeInStatusInput({ startAt, endAt, lastContact, firstContact, candidateId, status }, index) {

    try {
        if (endAt && endAt <= startAt) {
            throw new Error(
              `End ${endAt} cannot be before start ${startAt} (for ${candidateId} in ${status})`
            );
          }
    } catch (error) {
        console.log("\x1b[31mThis is bad data | removing data " + index + ", error:\x1b[0m\n ");
        console.warn(error);
        return false;
    }

    try {
        if ([firstContact, lastContact].some(date => date && (date < startAt || date > endAt))) {
            throw new Error(
              `First contact ${firstContact} and last contact ${lastContact} must be between start ${startAt} and end ${endAt} (for ${candidateId} in ${status})`
            );
        }
    } catch (error) {
      console.log("\x1b[31mThis is bad data | removing data " + index + ", error:\x1b[0m\n");
        console.warn(error);
        return false;
    }

    try {
  if (lastContact && firstContact && lastContact < firstContact) {
    throw new Error(
      `Last contact ${lastContact} cannot be before first contact ${firstContact} (for ${candidateId} in ${status})`
    );
  }
  } catch (error) {
    console.log("\x1b[31mThis is bad data | removing data " + index + ", error:\x1b[0m\n");
      console.warn(error);
      return false;
  }

  console.log("\n\t\x1b[32mThis is good data, it will be inserted\x1b[0m\n");

  return true;
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Make a logger function to reduce unnescesarry try/catch blocks.

const green = "\x1b[32m";
const red = "\x1b[31m";
const endColor = "\x1b[0m\n";
const logColor = (colorStr, msg) => colorStr + msg + endColor;

const createErrorLogger = (index) => (error) => {
  console.log(
    logColor(red, `This is bad data | removing data ${index}, error:`)
  );
  console.warn(error);
  return false;
};
function validateTimeInStatusInput(
  { startAt, endAt, lastContact, firstContact, candidateId, status },
  index
) {
  const logError = createErrorLogger(index);
  if (endAt && endAt <= startAt)
    return logError(
      `End ${endAt} cannot be before start ${startAt} (for ${candidateId} in ${status})`
    );
  if (
    [firstContact, lastContact].some(
      (date) => date && (date < startAt || date > endAt)
    )
  )
    return logError(
      `First contact ${firstContact} and last contact ${lastContact} must be between start ${startAt} and end ${endAt} (for ${candidateId} in ${status})`
    );
  if (lastContact && firstContact && lastContact < firstContact)
    return logError(
      `Last contact ${lastContact} cannot be before first contact ${firstContact} (for ${candidateId} in ${status})`
    );
  console.log(`
    ${logColor(green, "This is good data, it will be inserted")}
`);
  return true;
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

function validateTimeInStatusInput({
  startAt,
  endAt,
  lastContact,
  firstContact,
  candidateId,
  status
}, index) {

  try {
    if (endAt && endAt <= startAt) {
      throw new Error(
        `End ${endAt} cannot be before start ${startAt} (for ${candidateId} in ${status})`
      );
    }

    if ([firstContact, lastContact].some(date => date && (date < startAt || date > endAt))) {
      throw new Error(
        `First contact ${firstContact} and last contact ${lastContact} must be between start ${startAt} and end ${endAt} (for ${candidateId} in ${status})`
      );
    }

    if (lastContact && firstContact && lastContact < firstContact) {
      throw new Error(
        `Last contact ${lastContact} cannot be before first contact ${firstContact} (for ${candidateId} in ${status})`
      );
    }
  } catch (error) {
    console.log("\x1b[31mThis is bad data | removing data " + index + ", error:\x1b[0m\n");
    console.warn(error);
    return false;
  }

  console.log("\n\t\x1b[32mThis is good data, it will be inserted\x1b[0m\n");

  return true;
}

about 4 years ago · Juan Pablo Isaza Denunciar
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