nodejs / nodejs/node

Prevent native Error class hi-jacking in FS errors

Aperta
#40,232 2 commenti 3 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

errors
Lingua principale
JavaScript
Stelle
122k
Fork
37.3k
Merge medio
4g 2h
PR unite (30g)
283

Descrizione

Version

v16.9.0

Platform

Linux vagrant-docker 4.19.0-6-amd64 #1 SMP Debian 4.19.67-2+deb10u2 (2019-11-11) x86_64 GNU/Linux

Subsystem

fs

What steps will reproduce the bug?

In the current state of the promises section of the native fs module, when an error occurs, the native type Error is hi-jacked and a code property is being added to the error object.

This can easily be reproduced with the following code:

import { promises as FileSystem } from "fs";

try
{
    FileSystem.stat("/a/path/leading/to/nothing");
}
catch (error)
{
    console.log(Object.getPrototypeOf(error); // Will display the native class "Error"
    console.log(Object.getOwnPropertyDescriptors(error); // Will display the added code property that isn't native to Error.
}
How often does it reproduce? Is there a required condition?

No required conditions outside of the use of the fs native module.

What is the expected behavior?

This isn't a bug per-se but is a bad practice. A custom error of type FileSystemError should be thrown instead of hi-jacking the Error class to dynamically add a property to the object.
This behavior leads to problematic linting and testing with super-sets such as TypeScript or Eslint. Since code is not a native property of Error and dynamic property setting is often considered bad practice, it leads to situations where handling those specific issues requires extra effort to circumvent this design.

What do you see instead?

No response

Additional information

Ideally a custom FileSystemError should be created such as (Typescript example for types clarity):

class FileSystemError extends Error
{
    public code: string; // Ideally should be an enum listing all the possible values.
}

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia tracciando il percorso nativo di fs promises utilizzato da FileSystem.stat quando segnala un percorso mancante, quindi esamina come l’oggetto Error corrente riceve la propria proprietà code. Il lavoro è completato quando gli errori utilizzano un tipo di errore dedicato del filesystem con le informazioni code necessarie e i test coprono il comportamento dell’API promises.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
javascript
Ambito
backend, operating-systems
Tipo di issue
Funzionalità
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Tranquilla
Chiarezza
Abbastanza chiara
Idoneità per principianti
38/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.