-
Notifications
You must be signed in to change notification settings - Fork 1
SP-648: Handle disabled asset registry feature flag in Content CLI #360
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
Open
Kastriot Salihu (ksalihu)
wants to merge
1
commit into
main
Choose a base branch
from
ksalihu/SP-648-asset-registry-flag-disabled
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+185
−32
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import { FatalError } from "../../core/utils/logger"; | ||
|
|
||
| export const ASSET_REGISTRY_DISABLED_ERROR = "Asset registry feature is currently disabled"; | ||
|
|
||
| export const ASSET_REGISTRY_DISABLED_USER_MESSAGE = | ||
| "Asset registry is not enabled for this team. Contact your administrator to enable the feature."; | ||
|
|
||
| function extractErrorText(error: unknown): string { | ||
| if (typeof error === "string") { | ||
| return error; | ||
| } | ||
| if (error instanceof Error) { | ||
| return error.message; | ||
| } | ||
| return String(error); | ||
|
Check warning on line 15 in src/commands/asset-registry/asset-registry-error.ts
|
||
| } | ||
|
|
||
| function parseErrorField(error: unknown): string | undefined { | ||
| const payload = extractJsonPayload(extractErrorText(error)); | ||
| if (!payload) { | ||
| return undefined; | ||
| } | ||
|
|
||
| try { | ||
| const parsed = JSON.parse(payload) as { error?: unknown }; | ||
| return typeof parsed.error === "string" ? parsed.error : undefined; | ||
| } catch { | ||
| return undefined; | ||
| } | ||
| } | ||
|
|
||
| function extractJsonPayload(text: string): string | undefined { | ||
| let candidate = text.trim(); | ||
|
|
||
| const fatalErrorPrefix = "FatalError: "; | ||
| const fatalErrorIndex = candidate.lastIndexOf(fatalErrorPrefix); | ||
| if (fatalErrorIndex >= 0) { | ||
| candidate = candidate.slice(fatalErrorIndex + fatalErrorPrefix.length).trim(); | ||
| } | ||
|
|
||
| if (!candidate.startsWith("{")) { | ||
| return undefined; | ||
| } | ||
|
|
||
| return candidate; | ||
| } | ||
|
|
||
| export function handleAssetRegistryApiError(operation: string, error: unknown): never { | ||
| if (parseErrorField(error) === ASSET_REGISTRY_DISABLED_ERROR) { | ||
| throw new FatalError(ASSET_REGISTRY_DISABLED_USER_MESSAGE); | ||
| } | ||
| throw new FatalError(`Problem ${operation}: ${extractErrorText(error)}`); | ||
| } | ||
77 changes: 77 additions & 0 deletions
77
tests/commands/asset-registry/asset-registry-error.spec.ts
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,77 @@ | ||
| import { FatalError } from "../../../src/core/utils/logger"; | ||
| import { | ||
| ASSET_REGISTRY_DISABLED_ERROR, | ||
| ASSET_REGISTRY_DISABLED_USER_MESSAGE, | ||
| handleAssetRegistryApiError, | ||
| } from "../../../src/commands/asset-registry/asset-registry-error"; | ||
| import { mockAxiosGetError } from "../../utls/http-requests-mock"; | ||
| import { AssetRegistryService } from "../../../src/commands/asset-registry/asset-registry.service"; | ||
| import { testContext } from "../../utls/test-context"; | ||
|
|
||
| const TYPES_URL = "https://myTeam.celonis.cloud/pacman/api/core/asset-registry/types"; | ||
| const SKILLS_URL = "https://myTeam.celonis.cloud/pacman/api/core/asset-registry/skills"; | ||
|
|
||
| describe("Asset registry error handling", () => { | ||
| describe("handleAssetRegistryApiError", () => { | ||
| it("Should throw a friendly message when the asset registry feature flag is disabled", () => { | ||
| const errorBody = JSON.stringify({ error: ASSET_REGISTRY_DISABLED_ERROR }); | ||
|
|
||
| expect(() => handleAssetRegistryApiError("listing asset registry types", errorBody)) | ||
| .toThrow(new FatalError(ASSET_REGISTRY_DISABLED_USER_MESSAGE)); | ||
| }); | ||
|
|
||
| it("Should detect the disabled flag when the error is wrapped by HttpClient and AssetRegistryApi", () => { | ||
| const wrappedError = new FatalError( | ||
| `Problem listing asset registry types: FatalError: ${JSON.stringify({ error: ASSET_REGISTRY_DISABLED_ERROR })}` | ||
| ); | ||
|
|
||
| expect(() => handleAssetRegistryApiError("listing asset registry types", wrappedError)) | ||
| .toThrow(new FatalError(ASSET_REGISTRY_DISABLED_USER_MESSAGE)); | ||
| }); | ||
|
|
||
| it("Should preserve generic errors for other 403 responses", () => { | ||
| const errorBody = JSON.stringify({ error: "Access denied" }); | ||
|
|
||
| expect(() => handleAssetRegistryApiError("listing asset registry types", errorBody)) | ||
| .toThrow(new FatalError(`Problem listing asset registry types: ${errorBody}`)); | ||
| }); | ||
|
|
||
| it("Should preserve generic errors for 404 responses", () => { | ||
| const errorBody = JSON.stringify({ error: "Not found" }); | ||
|
|
||
| expect(() => handleAssetRegistryApiError("getting asset type 'UNKNOWN'", errorBody)) | ||
| .toThrow(new FatalError(`Problem getting asset type 'UNKNOWN': ${errorBody}`)); | ||
| }); | ||
|
|
||
| it("Should preserve generic errors for 500 responses", () => { | ||
| const errorBody = "Backend responded with status code 500"; | ||
|
|
||
| expect(() => handleAssetRegistryApiError("getting schema for asset type 'BOARD_V2'", errorBody)) | ||
| .toThrow(new FatalError(`Problem getting schema for asset type 'BOARD_V2': ${errorBody}`)); | ||
| }); | ||
| }); | ||
|
|
||
| describe("AssetRegistryService integration", () => { | ||
| it("Should surface the friendly message when listing types and the feature flag is disabled", async () => { | ||
| mockAxiosGetError(TYPES_URL, 403, { error: ASSET_REGISTRY_DISABLED_ERROR }); | ||
|
|
||
| await expect(new AssetRegistryService(testContext).listTypes(false)) | ||
| .rejects.toThrow(new FatalError(ASSET_REGISTRY_DISABLED_USER_MESSAGE)); | ||
| }); | ||
|
|
||
| it("Should surface the friendly message when listing skills and the feature flag is disabled", async () => { | ||
| mockAxiosGetError(SKILLS_URL, 403, { error: ASSET_REGISTRY_DISABLED_ERROR }); | ||
|
|
||
| await expect(new AssetRegistryService(testContext).listSkills(false)) | ||
| .rejects.toThrow(new FatalError(ASSET_REGISTRY_DISABLED_USER_MESSAGE)); | ||
| }); | ||
|
|
||
| it("Should surface a generic error for other 403 responses", async () => { | ||
| const errorBody = { error: "Access denied" }; | ||
| mockAxiosGetError(TYPES_URL, 403, errorBody); | ||
|
|
||
| await expect(new AssetRegistryService(testContext).listTypes(false)) | ||
| .rejects.toThrow(/Problem listing asset registry 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
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.
I don't think it's useful information for the user to know the feature flag string.