From 4e02d62dc47182f34318370642d6c202e6ce6700 Mon Sep 17 00:00:00 2001 From: JounQin Date: Tue, 19 Dec 2023 13:45:47 +0800 Subject: [PATCH] refactor: handle GitLab API error cause details for tracing (#158) --- .changeset/silent-cameras-explain.md | 5 ++ package.json | 11 ++-- src/comment.ts | 47 ++++++++++++++++- yarn.lock | 75 +++++++++++++++------------- 4 files changed, 97 insertions(+), 41 deletions(-) create mode 100644 .changeset/silent-cameras-explain.md diff --git a/.changeset/silent-cameras-explain.md b/.changeset/silent-cameras-explain.md new file mode 100644 index 00000000..2ee1b57d --- /dev/null +++ b/.changeset/silent-cameras-explain.md @@ -0,0 +1,5 @@ +--- +"changesets-gitlab": patch +--- + +refactor: handle GitLab API error cause details for tracing diff --git a/package.json b/package.json index de147b67..ff08ec70 100644 --- a/package.json +++ b/package.json @@ -65,18 +65,19 @@ "unified": "^11.0.4" }, "devDependencies": { - "@1stg/lib-config": "^12.0.0", + "@1stg/lib-config": "^12.0.1", "@changesets/changelog-github": "^0.4.8", "@changesets/cli": "^2.26.2", "@types/global-agent": "^2.1.3", - "@types/jest": "^29.5.8", + "@types/jest": "^29.5.11", "@types/js-yaml": "^4.0.9", - "@types/micromatch": "^4.0.5", + "@types/micromatch": "^4.0.6", + "@types/web": "^0.0.128", "jest": "^29.7.0", "ts-jest": "^29.1.1", - "ts-node": "^10.9.1", + "ts-node": "^10.9.2", "type-coverage": "^2.27.0", - "typescript": "^5.2.2", + "typescript": "^5.3.3", "yarn-deduplicate": "^6.0.2" }, "jest": { diff --git a/src/comment.ts b/src/comment.ts index e0015c02..5dea359a 100644 --- a/src/comment.ts +++ b/src/comment.ts @@ -208,6 +208,36 @@ const hasChangesetBeenAdded = async ( }) } +/** + * @see https://github.com/jdalrymple/gitbeaker/blob/52ef0e622de304d98afb811f4937560edefd8889/packages/rest/src/Requester.ts#L79-L86 + */ +export interface GitLabAPIError extends Error { + cause: { + description: string + request: Request + response: Response + } +} + +const GITLAB_API_ERROR_CAUSE_KEYS = new Set([ + 'description', + 'request', + 'response', +]) + +// eslint-disable-next-line @typescript-eslint/unbound-method +const { toString } = Object.prototype + +const isError = (value: unknown): value is Error => + toString.call(value) === '[object Error]' + +const isGitLabAPIError = (error: unknown): error is GitLabAPIError => + isError(error) && + !!error.cause && + typeof error.cause === 'object' && + Object.keys(error.cause).every(key => GITLAB_API_ERROR_CAUSE_KEYS.has(key)) + +// eslint-disable-next-line sonarjs/cognitive-complexity export const comment = async () => { const mrBranch = env.CI_MERGE_REQUEST_SOURCE_BRANCH_NAME if (!mrBranch) { @@ -320,7 +350,22 @@ export const comment = async () => { } } } catch (err: unknown) { - console.error(err) + if (isGitLabAPIError(err)) { + const { + cause: { description, request, response }, + } = err + console.error(description) + try { + console.error('request:', await request.text()) + } catch { + console.error("The error's request could not be used as plain text") + } + try { + console.error('response:', await response.text()) + } catch { + console.error("The error's response could not be used as plain text") + } + } throw err } } diff --git a/yarn.lock b/yarn.lock index 568ec799..12bdc2d3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -35,20 +35,20 @@ "@commitlint/config-lerna-scopes" "^17.0.2" "@pkgr/utils" "^2.3.1" -"@1stg/common-config@^9.0.0": - version "9.0.0" - resolved "https://registry.yarnpkg.com/@1stg/common-config/-/common-config-9.0.0.tgz#7755fb2b47f7e5020c7a65944aaaca4a02bba8a4" - integrity sha512-4eObSMl7h7ZJZeat82SQykv4H38QVp38kB/nxZyIwRWhBptHpXvEIA62AQ5pear/+qJkQ6blq78X86lgbh90dg== +"@1stg/common-config@^9.0.1": + version "9.0.1" + resolved "https://registry.yarnpkg.com/@1stg/common-config/-/common-config-9.0.1.tgz#6398ee154a277ef52193ffd9e2671b14a8ed18fc" + integrity sha512-XEAcqqjQyRcDyqYXRLgPLs12Vi6sLqQMjkgewJo3awXVUuFgJD2ppHdSjS9AlBujsf9XwYeEBBWAk2CJUVmD2Q== dependencies: "@1stg/babel-preset" "^3.2.3" "@1stg/commitlint-config" "^3.2.0" - "@1stg/eslint-config" "^7.0.0" + "@1stg/eslint-config" "^7.0.1" "@1stg/lint-staged" "^3.4.1" "@1stg/markuplint-config" "^3.0.1" "@1stg/prettier-config" "^3.9.2" "@1stg/remark-preset" "^2.0.0" "@1stg/simple-git-hooks" "^0.2.3" - "@1stg/tsconfig" "^2.3.2" + "@1stg/tsconfig" "^2.3.3" "@babel/core" "^7.22.5" "@commitlint/cli" "^17.6.5" eslint "^8.43.0" @@ -63,10 +63,10 @@ resolved "https://registry.yarnpkg.com/@1stg/config/-/config-0.2.1.tgz#12d35ff8e243bd4b35b55c2f244cd5cbfea844d4" integrity sha512-fStBgzyQnL0/bdIcHCplmrjFN9SBfnkldQ+TnpKnBFmp7jIxoggSRL3kaEFbGlq2lbV/H7Udy3bmJWTtdDH5Iw== -"@1stg/eslint-config@^7.0.0": - version "7.0.0" - resolved "https://registry.yarnpkg.com/@1stg/eslint-config/-/eslint-config-7.0.0.tgz#986eb594bc3cb6957cbb30e2a1ebddfda071b472" - integrity sha512-J1+0FbTvZK2CB/JokzP/D37GhAtJi42yAJep1wBw213QdDhktI2iV7VmwKvoPlLL8CB8JwF955UFKusG9lC7tA== +"@1stg/eslint-config@^7.0.1": + version "7.0.1" + resolved "https://registry.yarnpkg.com/@1stg/eslint-config/-/eslint-config-7.0.1.tgz#3e814033ce327ef59205b068fc353cfcfecbd0f7" + integrity sha512-qdGayfrv/LPanOs4N/yjt1yfMyeqYHGen/33wbBDVs9mmAJ+W9LCeueYaSfEzm0GOWeIJ6qIiLSHYiTcpGCDnw== dependencies: "@1stg/config" "^0.2.1" "@angular-eslint/eslint-plugin" "^16.0.3" @@ -108,12 +108,12 @@ eslint-plugin-vue "^9.15.0" eslint-plugin-yml "^1.8.0" -"@1stg/lib-config@^12.0.0": - version "12.0.0" - resolved "https://registry.yarnpkg.com/@1stg/lib-config/-/lib-config-12.0.0.tgz#843891ecd0478efd5ae28b74c22d88d9f6884dce" - integrity sha512-Dkz0ouc5M9DvkY0EG/RXNjTkcE544e5gScU2mKBInc8W7QOidBjji9OCsw29jxjS0o6cPRSELXZJbSJC2xfkrg== +"@1stg/lib-config@^12.0.1": + version "12.0.1" + resolved "https://registry.yarnpkg.com/@1stg/lib-config/-/lib-config-12.0.1.tgz#1fdf045b6ab57439eba4df1ab8a5cfc26909ac91" + integrity sha512-IovDXaUtwI4+mb0Ker81Hb618jDsaz1qXaQ+gMFX52l6G93DTFdULqfwCsqu0Mie4787bxL8l3r6ORHwuVq2Ww== dependencies: - "@1stg/common-config" "^9.0.0" + "@1stg/common-config" "^9.0.1" "@pkgr/rollup" "^4.1.1" "@1stg/lint-staged@^3.4.1": @@ -177,10 +177,10 @@ dependencies: "@pkgr/utils" "^2.3.1" -"@1stg/tsconfig@^2.3.2": - version "2.3.2" - resolved "https://registry.yarnpkg.com/@1stg/tsconfig/-/tsconfig-2.3.2.tgz#d843c542845d4d14350162507f711f216159cd8c" - integrity sha512-vNYoC1cvdGHCA3tJMUskaVsNVTt6QIOFO+eDtdO4JvqHllvi+o2QSqy5vTpAGMODSqbyFcLdPW7sD5kj9VQmNA== +"@1stg/tsconfig@^2.3.2", "@1stg/tsconfig@^2.3.3": + version "2.3.3" + resolved "https://registry.yarnpkg.com/@1stg/tsconfig/-/tsconfig-2.3.3.tgz#dc598fea2f75bf24a6da6039827679ca0213ece3" + integrity sha512-JHXiryRSs4w/ho/Uf3lDMlrYIf2p8+2gMgp23/j/HJfGF2+XwgRCnPgLA2VZ+LtGdqYX8ZJ4EmMgjFDdEdTMbg== "@aashutoshrathi/word-wrap@^1.2.3": version "1.2.6" @@ -3092,10 +3092,10 @@ dependencies: "@types/istanbul-lib-report" "*" -"@types/jest@^29.5.8": - version "29.5.8" - resolved "https://registry.yarnpkg.com/@types/jest/-/jest-29.5.8.tgz#ed5c256fe2bc7c38b1915ee5ef1ff24a3427e120" - integrity sha512-fXEFTxMV2Co8ZF5aYFJv+YeA08RTYJfhtN5c9JSv/mFEMe+xxjufCb+PHL+bJcMs/ebPUsBu+UNTEz+ydXrR6g== +"@types/jest@^29.5.11": + version "29.5.11" + resolved "https://registry.yarnpkg.com/@types/jest/-/jest-29.5.11.tgz#0c13aa0da7d0929f078ab080ae5d4ced80fa2f2c" + integrity sha512-S2mHmYIVe13vrm6q4kN6fLYYAka15ALQki/vgDC3mIukEOx8WJlv0kQPM+d4w8Gp6u0uSdKND04IlTXBv0rwnQ== dependencies: expect "^29.0.0" pretty-format "^29.0.0" @@ -3131,10 +3131,10 @@ dependencies: meow "*" -"@types/micromatch@^4.0.5": - version "4.0.5" - resolved "https://registry.yarnpkg.com/@types/micromatch/-/micromatch-4.0.5.tgz#a88fb878f7f7e7e1909c1db50f97acfede6b4d85" - integrity sha512-B1o0zVdb9GsbKT4Fucy3oeG9G1qy/TOHrYM+NsEPazT+ktsGXOJSb1+Bg9hP7BH14Bv4dd5m7r+FohwXkY/39A== +"@types/micromatch@^4.0.6": + version "4.0.6" + resolved "https://registry.yarnpkg.com/@types/micromatch/-/micromatch-4.0.6.tgz#340535c2b90098ace8fc5e7eaec8562deedb4f2f" + integrity sha512-2eulCHWqjEpk9/vyic4tBhI8a9qQEl6DaK2n/sF7TweX9YESlypgKyhXMDGt4DAOy/jhLPvVrZc8pTDAMsplJA== dependencies: "@types/braces" "*" @@ -3220,6 +3220,11 @@ resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-9.0.7.tgz#b14cebc75455eeeb160d5fe23c2fcc0c64f724d8" integrity sha512-WUtIVRUZ9i5dYXefDEAI7sh9/O7jGvHg7Df/5O/gtH3Yabe5odI3UWopVR1qbPXQtvOxWu3mM4XxlYeZtMWF4g== +"@types/web@^0.0.128": + version "0.0.128" + resolved "https://registry.yarnpkg.com/@types/web/-/web-0.0.128.tgz#a431a051fbed2a03da97f032990d27c27150735c" + integrity sha512-STOv2gEXqlhUSB8ea9Tm3MlGrG9Pn+mh5+Eg8Rt6oC6Ju92kAK109/KzRb/F1PvBg+G1fX0cQayDZbvBqBET4Q== + "@types/whatwg-mimetype@3.0.1": version "3.0.1" resolved "https://registry.yarnpkg.com/@types/whatwg-mimetype/-/whatwg-mimetype-3.0.1.tgz#7aa02e0340e69b94aa549a3c72ad3c8be65bc370" @@ -11404,10 +11409,10 @@ ts-jest@^29.1.1: semver "^7.5.3" yargs-parser "^21.0.1" -ts-node@^10.8.1, ts-node@^10.9.1: - version "10.9.1" - resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-10.9.1.tgz#e73de9102958af9e1f0b168a6ff320e25adcff4b" - integrity sha512-NtVysVPkxxrwFGUUxGYhfux8k78pQB3JqYBXlLRZgdGUqTO5wU/UyHop5p70iEbGhB7q5KmiZiU0Y3KlJrScEw== +ts-node@^10.8.1, ts-node@^10.9.1, ts-node@^10.9.2: + version "10.9.2" + resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-10.9.2.tgz#70f021c9e185bccdca820e26dc413805c101c71f" + integrity sha512-f0FFpIdcHgn8zcPSbf1dRevwt047YMnaiJM3u2w2RewrB+fob/zePZcrOyQoLMMO7aBIddLcQIEK5dYjkLnGrQ== dependencies: "@cspotcode/source-map-support" "^0.8.0" "@tsconfig/node10" "^1.0.7" @@ -11602,10 +11607,10 @@ typedarray@^0.0.6: resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777" integrity sha512-/aCDEGatGvZ2BIk+HmLf4ifCJFwvKFNb9/JeZPMulfgFracn9QFcAf5GO8B/mweUjSoblS5In0cWhqpfs/5PQA== -"typescript@^4.6.4 || ^5.2.2", typescript@^5.2.2: - version "5.2.2" - resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.2.2.tgz#5ebb5e5a5b75f085f22bc3f8460fba308310fa78" - integrity sha512-mI4WrpHsbCIcwT9cF4FZvr80QUeKvsUsUvKDoR+X/7XHQH98xYD8YHZg7ANtz2GtZt/CBq2QJ0thkGJMHfqc1w== +"typescript@^4.6.4 || ^5.2.2", typescript@^5.2.2, typescript@^5.3.3: + version "5.3.3" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.3.3.tgz#b3ce6ba258e72e6305ba66f5c9b452aaee3ffe37" + integrity sha512-pXWcraxM0uxAS+tN0AG/BF2TyqmHO014Z070UsJ+pFvYuRSq8KH8DmWpnbXe0pEPDHXZV3FcAbJkijJ5oNEnWw== unassert@^2.0.0, unassert@^2.0.2: version "2.0.2"