-
Notifications
You must be signed in to change notification settings - Fork 28
リクエストエラーからステータスコード・URL・原因を参照できるようにする #109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Sinhalite
merged 2 commits into
microcmsio:main
from
Sinhalite:codex/improve-request-errors
Sep 3, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| export { createClient } from './createClient'; | ||
| export { createManagementClient } from './createManagementClient'; | ||
| export { isMicroCMSRequestError } from './lib/error'; | ||
| export * from './types'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import { MicroCMSRequestError } from '../types'; | ||
|
|
||
| interface CreateMicroCMSRequestErrorOptions { | ||
| status?: number; | ||
| url: string; | ||
| originalError?: unknown; | ||
| } | ||
|
|
||
| const maskDraftKey = (url: string): string => { | ||
| return url.replace(/([?&]draftKey=)[^&#]*/g, '$1***'); | ||
| }; | ||
|
|
||
| export const createMicroCMSRequestError = ( | ||
| error: Error, | ||
| { status, url, originalError }: CreateMicroCMSRequestErrorOptions, | ||
| ): MicroCMSRequestError => { | ||
| // Errorとしての実行時の同一性と既存のログ出力を維持するため、独自クラスは生成せず、 | ||
| // 既存のErrorに追加情報を付与する。独自のErrorクラスは将来のメジャーリリースで再検討できる。 | ||
| // 設計背景: https://github.com/microcmsio/microcms-js-sdk/pull/109 | ||
| const microCMSRequestError = error as MicroCMSRequestError; | ||
|
|
||
| Object.defineProperties(microCMSRequestError, { | ||
| status: { | ||
| value: status, | ||
| enumerable: false, | ||
| configurable: true, | ||
| writable: true, | ||
| }, | ||
| url: { | ||
| value: maskDraftKey(url), | ||
| enumerable: false, | ||
| configurable: true, | ||
| writable: true, | ||
| }, | ||
| originalError: { | ||
| value: originalError, | ||
| enumerable: false, | ||
| configurable: true, | ||
| writable: true, | ||
| }, | ||
| }); | ||
|
|
||
| return microCMSRequestError; | ||
| }; | ||
|
|
||
| export const isMicroCMSRequestError = ( | ||
| error: unknown, | ||
| ): error is MicroCMSRequestError => { | ||
| if (!(error instanceof Error)) return false; | ||
|
|
||
| return ( | ||
| Object.prototype.hasOwnProperty.call(error, 'status') && | ||
| (typeof (error as MicroCMSRequestError).status === 'number' || | ||
| typeof (error as MicroCMSRequestError).status === 'undefined') && | ||
| Object.prototype.hasOwnProperty.call(error, 'url') && | ||
| typeof (error as MicroCMSRequestError).url === 'string' && | ||
| Object.prototype.hasOwnProperty.call(error, 'originalError') | ||
| ); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
なんとなくですが、Errorクラスを拡張して新たにErrorクラスを作るのが一般的ですかね?
Object.definePropertiesを選んだ理由などがあれば知りたいです!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dc7290
ありがとうございます!
こちらはAIとのやり取りでも論点になったところでしたね・・!
今回はマイナー/パッチリリースを想定しており、既存のErrorのconstructor、name、通常のconsole.log表示、列挙・シリアライズ結果への影響をできる限り避けることを優先しました。
そのため、従来どおり生成したErrorに非列挙プロパティを追加する方式を選択しています。
メジャーバージョンアップのタイミングであれば、破壊的変更として影響範囲を明示したうえで、Errorを継承した独自classへ移行する方が、良いのかなとは思っています。
今回は後方互換性を優先したこの方式で進めたいと考えていますが、違和感があれば相談させてください!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Sinhalite
なるほどですね・・・!
Errorクラスを拡張した上で後方互換性を保つ方法がないか調べてましたが、かなり複雑になるのと完璧には保てなさそうだったので、
マイナーバージョンでのリリースを考えると今回の方法で良さそうです!👍
1点、理想となる実装パターンと次のメジャーバージョンで移行したい旨をコメントに残しておけるとより良さそうですかね?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
あ、もしくは今回メジャーバージョンリリースするという手は無しなんですかね??
今回の実装でリリースした後に、独自classへ移行してもらう方がユーザーに手間をかけてしまうような気もしており、、、
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dc7290
諸々検討ありがとうございます!
マイナーバージョンで進める場合はこちらは残しておくようにします!
選択肢としてはありそうです!
ただ、今回のエラーハンドリング改善だけを理由にメジャーバージョンを上げるのは、変更内容とのバランスを考えると少し重いように感じています。
将来的な移行コストもそこまで大きくなさそうなので、ほかの破壊的変更と合わせたメジャーアップデートのタイミングで、改めて検討するのがよいかなと考えた部分ではありました!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Sinhalite
ここは少し気になっていて、「他の破壊的変更と合わせる」だと、まとまった分だけ1回のアップデートが重くなる可能性もあるなと思っています。
また、この先あるかわからない破壊的変更を前提に、公開APIの理想形を先送りする、という立て付けもやや弱い気がしています・・・!
とはいえ、JS SDKはユーザーも多いので気軽にメジャーは上げたくない、という点には同意です!
今回ユーザーが得たい価値(status / url / originalError と type guard)は、現状の方式でも届けられるので、マイナーで進める判断自体は妥当だと思いました。
独自 Error class については、「他の破壊的変更待ち」ではなく、
「Error の identity(constructor / name / instanceof)を変える価値が、単独のメジャーに見合うと判断したタイミング」
で改めて入れる、くらいの基準にしておけると良さそうです。
理想形と移行意図のコメントは残してもらえると助かります!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dc7290
ありがとうございます!
たしかにメジャーアップデートの条件が曖昧に受け取られそうとは思ったため、提案いただいた価値ベースの記述に変更してます!
(コメントだけだと長くなりそうだったので、一部PRをリンクする形としました。)
2790315