Skip to content

Commit

Permalink
feat: 优化异常信息展示
Browse files Browse the repository at this point in the history
  • Loading branch information
bangbang93 committed Sep 19, 2024
1 parent 64d3647 commit cfe0bda
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 0 deletions.
34 changes: 34 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
},
"homepage": "https://github.com/bangbang93/openbmclapi#readme",
"dependencies": {
"@bangbang93/service-errors": "^1.1.1",
"@bangbang93/utils": "^6.9.1",
"@mongodb-js/zstd": "^1.2.0",
"@xmcl/nat-api": "^0.4.3",
Expand Down
2 changes: 2 additions & 0 deletions src/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {FileListSchema} from './constants.js'
import {validateFile} from './file.js'
import {Keepalive} from './keepalive.js'
import {logger} from './logger.js'
import {beforeError} from './modules/got-hooks.js'
import {AuthRouteFactory} from './routes/auth.route.js'
import MeasureRouteFactory from './routes/measure.route.js'
import {getStorage, type IStorage} from './storage/base.storage.js'
Expand Down Expand Up @@ -117,6 +118,7 @@ export class Cluster {
}
},
],
beforeError,
},
})
this.storage = getStorage(config)
Expand Down
16 changes: 16 additions & 0 deletions src/modules/got-hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {ServiceError} from '@bangbang93/service-errors'
import {BeforeErrorHook, HTTPError, RequestError} from 'got'

export const beforeError: BeforeErrorHook[] = [catchServiceError]

export function catchServiceError(error: RequestError): RequestError {
if (error instanceof HTTPError) {
if (error.response.headers['content-type']?.includes('application/json')) {
const body = JSON.parse(error.response.body.toString())

Check failure on line 9 in src/modules/got-hooks.ts

View workflow job for this annotation

GitHub Actions / build (20)

Unsafe assignment of an `any` value

Check failure on line 9 in src/modules/got-hooks.ts

View workflow job for this annotation

GitHub Actions / build (20)

Unsafe argument of type `any` assigned to a parameter of type `string`

Check failure on line 9 in src/modules/got-hooks.ts

View workflow job for this annotation

GitHub Actions / build (20)

Unsafe call of an `any` typed value

Check failure on line 9 in src/modules/got-hooks.ts

View workflow job for this annotation

GitHub Actions / build (20)

Unsafe member access .toString on an `any` value

Check failure on line 9 in src/modules/got-hooks.ts

View workflow job for this annotation

GitHub Actions / build (22)

Unsafe assignment of an `any` value

Check failure on line 9 in src/modules/got-hooks.ts

View workflow job for this annotation

GitHub Actions / build (22)

Unsafe argument of type `any` assigned to a parameter of type `string`

Check failure on line 9 in src/modules/got-hooks.ts

View workflow job for this annotation

GitHub Actions / build (22)

Unsafe call of an `any` typed value

Check failure on line 9 in src/modules/got-hooks.ts

View workflow job for this annotation

GitHub Actions / build (22)

Unsafe member access .toString on an `any` value
if (ServiceError.isServiceError(body)) {
throw ServiceError.fromJSON(body as unknown as Record<string, unknown>)
}
}
}
return error
}
4 changes: 4 additions & 0 deletions src/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import got, {type Got} from 'got'
import ms from 'ms'
import {createHmac} from 'node:crypto'
import {logger} from './logger.js'
import {beforeError} from './modules/got-hooks.js'

export class TokenManager {
private token: string | undefined
Expand All @@ -22,6 +23,9 @@ export class TokenManager {
timeout: {
request: ms('5m'),
},
hooks: {
beforeError,
},
})
}

Expand Down

0 comments on commit cfe0bda

Please sign in to comment.