Throw real Errors instead of objects

未關閉
#1,444 3 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

@psavan1655 已經在處理了。

開始於 2023年4月3日。

評估

難度
4/5
預估耗時
3-5 天
新手友好度
35/100
Issue 類型
缺陷
描述清晰度
基本清楚
活躍度
停滯
技術堆疊
typescript
領域
api

研究方向

從 packages/client-account/src/methods/accountCopyIndex.ts 和 packages/client-account/src/errors/createDestinationIndiceExistsError.ts 開始,接著檢查 issue 引用的其他錯誤輔助函式。決定一致的 Error 表示方式,並驗證拋出的失敗對 Sentry 等消費者保留其名稱、訊息和真實的堆疊追蹤。

由索引模型根據 Issue 內容生成。

描述

enhancement

This package has a non standard behavior of throwing objects instead of real JS Errors. As it works approximately in the same way in most cases and these objects are similar to Error (at least, TS is ok with that), there are some edge cases where it messes everything up.

My specific use case is with Sentry, which is not able to handle properly these "errors". Instead of having a proper stack trace, I have the Sentry error Non-Error exception captured and all the informations about the real error are lost.

We do this kind of thing all over the package:

accountCopyIndex.ts

throw createDestinationIndiceExistsError()

createDestinationIndiceExistsError.ts

export function createDestinationIndiceExistsError(): Error {
  return {
    name: 'DestinationIndiceAlreadyExistsError',
    message: 'Destination indice already exists.',
  };
}

I propose instead to either throw a basic Error with the message...

export function createDestinationIndiceExistsError(): Error {
  return new Error('DestinationIndiceAlreadyExistsError');
}

Or even to create custom error objects

class AlgoliaDestinationIndiceAlreadyExistsError extends Error {
  constructor(message) {
    super(message);
    this.name = 'AlgoliaDestinationIndiceAlreadyExistsError';
  }
}

export function createDestinationIndiceExistsError(): Error {
  return new AlgoliaDestinationIndiceAlreadyExistsError('Destination indice already exists.');
}

I even think that this helper methods are not necessary and that custom errors could be thrown directly. For instance, the custom message is useless in this case.

I can open a PR with a proof of concept if you think it's a good idea. I would also be glad to hear your ideas about the right implementation.

主要語言
TypeScript
星號
1.4k
分支
227
PR 合併指標
30 天內沒有已合併 PR

環境準備

這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

algolia/algoliasearch-client-javascript 的其他 Issue

查看 algolia/algoliasearch-client-javascript 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。