nodejs / nodejs/node

Prevent native Error class hi-jacking in FS errors

オープン
#40,232 コメント 2 件 リアクション 3 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

errors
主要言語
JavaScript
スター
122k
フォーク
37.3k
平均マージ
4日 2時間
マージ済み PR(30日)
283

説明

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.
}

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、欠落しているパスを報告するときに FileSystem.stat が使用するネイティブな fs promises パスを追跡し、次に現在の Error オブジェクトがどのように code プロパティを受け取るかを調べます。エラーが必要な code 情報を持つ専用のファイルシステムエラー型を使用し、promises API の動作をテストでカバーできれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
javascript
領域
backend, operating-systems
issue の種類
機能追加
難易度
4/5
見積もり時間
3〜5日
活発さ
静か
明瞭さ
おおむね明確
初心者へのやさしさ
38/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。