-
Notifications
You must be signed in to change notification settings - Fork 22
feat(service): throw service error with context #149
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| import pickBy from 'lodash/pickBy'; | ||
| import { isDomain } from '../utils'; | ||
| import { BtcAssetsApiError, ErrorCodes } from '../error'; | ||
| import { BaseApis, BaseApiRequestOptions, BtcAssetsApiToken } from '../types'; | ||
| import { pickBy } from 'lodash'; | ||
| import { BaseApis, BaseApiRequestOptions, BtcAssetsApiToken, BtcAssetsApiContext } from '../types'; | ||
|
|
||
| export class BtcAssetsApiBase implements BaseApis { | ||
| public url: string; | ||
|
|
@@ -35,7 +35,8 @@ export class BtcAssetsApiBase implements BaseApis { | |
| const packedParams = params ? '?' + new URLSearchParams(pickBy(params, (val) => val !== undefined)).toString() : ''; | ||
| const withOriginHeaders = this.origin ? { origin: this.origin } : void 0; | ||
| const withAuthHeaders = requireToken && this.token ? { Authorization: `Bearer ${this.token}` } : void 0; | ||
| const res = await fetch(`${this.url}${route}${packedParams}`, { | ||
| const url = `${this.url}${route}${packedParams}`; | ||
| const res = await fetch(url, { | ||
| method, | ||
| headers: { | ||
| ...withOriginHeaders, | ||
|
|
@@ -56,8 +57,19 @@ export class BtcAssetsApiBase implements BaseApis { | |
| // do nothing | ||
| } | ||
|
|
||
| const status = res.status; | ||
| let comment: string | undefined; | ||
| const status = res.status; | ||
| const context: BtcAssetsApiContext = { | ||
| request: { | ||
| url, | ||
| params, | ||
| body: tryParseBody(otherOptions.body), | ||
| }, | ||
| response: { | ||
| status, | ||
| data: json ?? text, | ||
| }, | ||
| }; | ||
|
|
||
| if (!json) { | ||
| comment = text ? `(${status}) ${text}` : `${status}`; | ||
|
|
@@ -73,16 +85,16 @@ export class BtcAssetsApiBase implements BaseApis { | |
| } | ||
|
|
||
| if (status === 200 && !json) { | ||
| throw BtcAssetsApiError.withComment(ErrorCodes.ASSETS_API_RESPONSE_DECODE_ERROR, comment); | ||
| throw BtcAssetsApiError.withComment(ErrorCodes.ASSETS_API_RESPONSE_DECODE_ERROR, comment, context); | ||
| } | ||
| if (status === 401) { | ||
| throw BtcAssetsApiError.withComment(ErrorCodes.ASSETS_API_UNAUTHORIZED, comment); | ||
| throw BtcAssetsApiError.withComment(ErrorCodes.ASSETS_API_UNAUTHORIZED, comment, context); | ||
| } | ||
| if (status === 404 && !allow404) { | ||
| throw BtcAssetsApiError.withComment(ErrorCodes.ASSETS_API_RESOURCE_NOT_FOUND, comment); | ||
| throw BtcAssetsApiError.withComment(ErrorCodes.ASSETS_API_RESOURCE_NOT_FOUND, comment, context); | ||
| } | ||
| if (status !== 200 && status !== 404 && !allow404) { | ||
| throw BtcAssetsApiError.withComment(ErrorCodes.ASSETS_API_RESPONSE_ERROR, comment); | ||
| throw BtcAssetsApiError.withComment(ErrorCodes.ASSETS_API_RESPONSE_ERROR, comment, context); | ||
| } | ||
| if (status !== 200) { | ||
| return void 0 as T; | ||
|
|
@@ -126,3 +138,11 @@ export class BtcAssetsApiBase implements BaseApis { | |
| this.token = token.token; | ||
| } | ||
| } | ||
|
|
||
| function tryParseBody(body: unknown): Record<string, any> | undefined { | ||
| try { | ||
| return typeof body === 'string' ? JSON.parse(body) : void 0; | ||
| } catch { | ||
| return void 0; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should avoid using Some libraries/packages use
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree that undefined is better than void 0
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
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 => !!valmore 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 !== undefinedis better, if val is 0, it will be filtered out hereThere 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