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

121
Vistas
Is there a better way to handle the return on this try catch?
function getDirFromPath(req) {
    var path = req.header('path')

    if (path == null) {
        res.status(400).send('Path required')
    }

    path = 'files/' + path
    try {
        var dir = fs.opendirSync(path)
        return dir;
    } catch {
        if (fs.accessSync(path) != undefined) {
            console.log('Non-existing dir tried to be accessed.')
            res.status(400).send('Invalid path')
        }
        res.status(500).send('Failed to open directory')
        return -1;
    }
}

I can then check for -1 in the caller of this function. This seems inelegant though. Should I be doing some kind of exception throwing out of this function?

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