-
Notifications
You must be signed in to change notification settings - Fork 22
feat(service): throw service error with context #149
Conversation
| await this.init(); | ||
| } | ||
|
|
||
| const packedParams = params ? '?' + new URLSearchParams(pickBy(params, (val) => val !== undefined)).toString() : ''; |
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.
Is val => !!val more simple and clear?
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.
I think val !== undefined is better, if val is 0, it will be filtered out here
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.
Yes. The type of val is unknown
| try { | ||
| return typeof body === 'string' ? JSON.parse(body) : void 0; | ||
| } catch { | ||
| return void 0; |
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.
We should avoid using void 0, it is the same as undefined, but not as easy to understand.
Some libraries/packages use void 0 because undefined can be shadowed by local variables, and void 0 is shorter and can save a few bytes. You may see underscore or lodash used this way, but it doesn't mean we need it.
For modern JavaScript/TypeScript, this is a problem that does not require concern. Local variable shadowing cannot occur in TypeScript, and bundle size should be left to webpack/esbuild or other tools, rather than written as void 0
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.
I agree that undefined is better than void 0
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.
It's kinda a coding style habit for me, I use it because it's easy to type.
I'll replace all void 0 to undefined in another pull request.
Changes
contextin the error object of the service libRelated issues
Details
You can now find context in the
BtcAssetsApiErrorif the error was thrown from a request:This is the type of
e.context: