-
Notifications
You must be signed in to change notification settings - Fork 13
Use JSON.stringify for object interpolation in log statements to avoi… #311
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
rhopp
wants to merge
2
commits into
redhat-appstudio:main
Choose a base branch
from
rhopp:stringifyObjects
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.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,24 @@ | ||
| import { BaseHttpClient } from '../../common/http/base-http.client'; | ||
| import { AxiosError } from 'axios'; | ||
| import { ApiError } from '../../common/errors/api.errors'; | ||
| import { AzureApiError } from '../errors/azure.errors'; | ||
| import { LoggerFactory, Logger } from '../../../logger/logger'; | ||
|
|
||
| function sanitizeErrorData(data: unknown): string { | ||
| if (data == null) return 'no data'; | ||
| if (typeof data === 'string') return data.slice(0, 2000); | ||
| if (typeof data === 'object') { | ||
| const { message, typeKey, errorCode, statusCode } = data as Record<string, unknown>; | ||
| const parts = [ | ||
| message != null ? `message=${message}` : null, | ||
| typeKey != null ? `typeKey=${typeKey}` : null, | ||
| errorCode != null ? `errorCode=${errorCode}` : null, | ||
| statusCode != null ? `statusCode=${statusCode}` : null, | ||
| ].filter(Boolean); | ||
| return parts.length > 0 ? parts.join(', ') : '[response data omitted]'; | ||
| } | ||
| return String(data); | ||
| } | ||
|
|
||
| export interface AzureHttpClientConfig { | ||
| organization: string; | ||
| host: string; | ||
|
|
@@ -43,21 +59,36 @@ export class AzureHttpClient extends BaseHttpClient { | |
|
|
||
| this.client.interceptors.response.use( | ||
| response => response, | ||
| (error: AxiosError) => { | ||
| if (error.response) { | ||
| this.logger.error(`Azure DevOps API Error: ${error.response.status} ${error.response.statusText} - ${error.response.data}`); | ||
| (error) => { | ||
| // Build the AzureApiError first, so wrapping always succeeds regardless of logging | ||
| let azureError: AzureApiError; | ||
|
|
||
| // BaseHttpClient already transforms AxiosError → ApiError | ||
| // Map ApiError to Azure-specific errors first | ||
| if (error instanceof ApiError) { | ||
| azureError = new AzureApiError(error.message, error.status, error.data, error); | ||
| try { | ||
| this.logger.error(`Azure DevOps API Error: ${error.status} - ${sanitizeErrorData(error.data)}`); | ||
| } catch { /* logging must not break error propagation */ } | ||
| } else if (error.response) { | ||
| // Fallback: Handle raw AxiosError if BaseHttpClient interceptor didn't catch it | ||
| azureError = new AzureApiError(error.message, error.response.status, error.response.data, error); | ||
| try { | ||
| this.logger.error(`Azure DevOps API Error: ${error.response.status} ${error.response.statusText} - ${sanitizeErrorData(error.response.data)}`); | ||
| } catch { /* logging must not break error propagation */ } | ||
| } else if (error.request) { | ||
| this.logger.error(`Azure DevOps API Error: No response received - ${error.request}`); | ||
| azureError = new AzureApiError('No response received from Azure DevOps', undefined, undefined, error); | ||
| try { | ||
| const { method, baseURL, url, timeout } = error.request?.config ?? error.config ?? {}; | ||
| this.logger.error(`Azure DevOps API Error: No response received - ${method?.toUpperCase()} ${baseURL ?? ''}${url ?? ''} (timeout: ${timeout})`); | ||
| } catch { /* logging must not break error propagation */ } | ||
| } else { | ||
| this.logger.error(`Azure DevOps API Error: Request setup failed - ${error}`); | ||
| azureError = new AzureApiError('Request setup failed', undefined, undefined, error); | ||
| try { | ||
| this.logger.error(`Azure DevOps API Error: Request setup failed - ${error}`); | ||
| } catch { /* logging must not break error propagation */ } | ||
|
Comment on lines
+86
to
+89
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. Avoid raw object interpolation in setup-failure logging. This branch can still emit Suggested fix } else {
azureError = new AzureApiError('Request setup failed', undefined, undefined, error);
try {
- this.logger.error(`Azure DevOps API Error: Request setup failed - ${error}`);
+ const detail = error instanceof Error ? error.message : sanitizeErrorData(error);
+ this.logger.error(`Azure DevOps API Error: Request setup failed - ${detail}`);
} catch { /* logging must not break error propagation */ }
}🤖 Prompt for AI Agents |
||
| } | ||
| // Wrap AxiosError in a custom AzureApiError | ||
| const azureError = new AzureApiError( | ||
| error.message, | ||
| error.response?.status, | ||
| error.response?.data, | ||
| error | ||
| ); | ||
|
|
||
| return Promise.reject(azureError); | ||
| } | ||
| ); | ||
|
|
||
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
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.
🧩 Analysis chain
🏁 Script executed:
Repository: redhat-appstudio/tssc-test
Length of output: 1415
🏁 Script executed:
rg -n "error\.request" src/api/azure/http/azure-http.client.tsRepository: redhat-appstudio/tssc-test
Length of output: 210
🏁 Script executed:
rg -n "error\.request\?*\.config|error\.request\.config" --type ts src/Repository: redhat-appstudio/tssc-test
Length of output: 211
🏁 Script executed:
rg -n "import.*axios|import.*AxiosError" src/api/azure/http/azure-http.client.tsRepository: redhat-appstudio/tssc-test
Length of output: 52
🏁 Script executed:
rg -n "AxiosError|error:" src/api/azure/http/azure-http.client.ts -B 2 -A 2Repository: redhat-appstudio/tssc-test
Length of output: 626
🏁 Script executed:
Repository: redhat-appstudio/tssc-test
Length of output: 52
🏁 Script executed:
# Look for axios client setup to understand error handling context head -50 src/api/azure/http/azure-http.client.tsRepository: redhat-appstudio/tssc-test
Length of output: 1754
🏁 Script executed:
Repository: redhat-appstudio/tssc-test
Length of output: 52
🏁 Script executed:
# Check BaseHttpClient to understand what error types propagate head -100 src/api/common/http/base-http.client.tsRepository: redhat-appstudio/tssc-test
Length of output: 3206
🏁 Script executed:
Repository: redhat-appstudio/tssc-test
Length of output: 52
🏁 Script executed:
Repository: redhat-appstudio/tssc-test
Length of output: 435
🏁 Script executed:
Repository: redhat-appstudio/tssc-test
Length of output: 2065
🏁 Script executed:
# Check the full ApiError class structure cat -n src/api/common/errors/api.errors.tsRepository: redhat-appstudio/tssc-test
Length of output: 3862
🏁 Script executed:
Repository: redhat-appstudio/tssc-test
Length of output: 520
Remove dead code path—
error.requestdoesn't have aconfigproperty.In AxiosError,
error.requestis the Node.jshttp.ClientRequestobject, which has noconfigproperty. The expressionerror.request?.configwill always evaluate toundefined, making it unnecessary dead code. Simplify to access onlyerror.config:Suggested fix
🤖 Prompt for AI Agents