Skip to content

Commit ce28add

Browse files
Merge pull request #190 from codacy/setup-edge-case
chore: Add logs and error handling CF-2239
2 parents 59f3022 + e6e49a0 commit ce28add

File tree

7 files changed

+136
-55
lines changed

7 files changed

+136
-55
lines changed

.codacy/codacy.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ tools:
66
- eslint@9.38.0
77
- lizard@1.17.31
88
- pmd@6.55.0
9-
- semgrep@1.78.0
10-
- trivy@0.66.0
9+
- semgrep@1.16.1
10+
- trivy@0.69.2

media/scripts/setupScript.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
let userInfo = null
3333
let organizationInfo = null
3434
let repositoryInfo = null
35+
let orgError = null
3536
/**
3637
* Escapes HTML special characters to prevent XSS attacks.
3738
* @param {string | undefined | null} str - The string to escape
@@ -50,7 +51,7 @@
5051
/**
5152
* Safely sets cloud description with highlighted text using DOM methods.
5253
* @param {HTMLElement} element - The element to update
53-
* @param {{type: 'organization' | 'repository' | 'user', params: {organization?: string, provider?: string, repository?: string, user?: string}}} context - The context to use
54+
* @param {{type: 'organization' | 'repository' | 'user' | 'organizationNotFound', params: {organization?: string, provider?: string, repository?: string, user?: string}}} context - The context to use
5455
*/
5556
function setCloudDescription(element, { type, params }) {
5657
element.textContent = ''
@@ -61,6 +62,11 @@
6162
<p class="loading-text">Connecting to Codacy...</p>
6263
</div>`
6364
switch (type) {
65+
case 'organizationNotFound':
66+
textNode.textContent = 'The organization you\'re trying to connect to is not found in Codacy. '
67+
linkNode.textContent = `Check your available organizations`
68+
linkNode.href = `https://app.codacy.com/organizations`
69+
break
6470
case 'organization':
6571
if (!params.provider || !params.organization) {
6672
element.innerHTML = loadingHtml
@@ -134,7 +140,8 @@
134140
userInfo = message.userInfo
135141
organizationInfo = message.organizationInfo
136142
repositoryInfo = message.repositoryInfo
137-
handleLoginStateChange(isLoggedIn, isOrgInCodacy, isRepoInCodacy, userInfo, organizationInfo, repositoryInfo)
143+
orgError = message.orgError
144+
handleLoginStateChange(isLoggedIn, isOrgInCodacy, isRepoInCodacy, userInfo, organizationInfo, repositoryInfo, orgError)
138145
break
139146
case 'mcpStatusChanged':
140147
isMCPInstalled = message.isMCPInstalled
@@ -236,6 +243,14 @@
236243
} = elements
237244
const { organizationName, organizationProvider, repositoryName, userName, billing } = connectionInfo
238245
switch (state) {
246+
case 'organizationNotFound':
247+
cloudIcon.src = iconUris.warning
248+
addOrgButton.style.display = 'none'
249+
addRepoButton.style.display = 'none'
250+
noOrgDescription.style.display = 'none'
251+
upgradeBox.style.display = 'none'
252+
setCloudDescription(cloudDescription, { type: 'organizationNotFound', params: { } })
253+
break
239254
case 'needsToAddOrganization':
240255
addOrgButton.style.display = 'inline-block'
241256
noOrgDescription.style.display = 'inline-block'
@@ -293,8 +308,9 @@
293308
* @param {Object|null} userInfo
294309
* @param {Object|null} organizationInfo
295310
* @param {Object|null} repositoryInfo
311+
* @param {string|null} orgError
296312
*/
297-
function handleLoginStateChange(loggedIn, isOrgInCodacy, isRepoInCodacy, userInfo, organizationInfo, repositoryInfo) {
313+
function handleLoginStateChange(loggedIn, isOrgInCodacy, isRepoInCodacy, userInfo, organizationInfo, repositoryInfo, orgError) {
298314
const upgradeBox = document.getElementById('upgrade-box')
299315
const upgradeButton = document.getElementById('upgrade-button')
300316
/** @type {HTMLImageElement | null} */
@@ -335,6 +351,9 @@
335351
} else {
336352
handleLoginOrgStates(elements, connectionInfo, 'needsToAddOrganization')
337353
}
354+
if (orgError) {
355+
handleLoginOrgStates(elements, connectionInfo, 'organizationNotFound')
356+
}
338357
} else {
339358
showLoggedOutState(elements)
340359
}

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,11 @@
194194
"contents": "We failed to connect this repository to Codacy. Please restart the IDE and try again.",
195195
"when": "Codacy:CodacyCloudStateContext == NoRepository"
196196
},
197+
{
198+
"view": "codacy:cloud-status",
199+
"contents": "The organization you're trying to connect to is not found in Codacy. Please check the organization name and try again.",
200+
"when": "Codacy:CodacyCloudStateContext == OrganizationNotFound"
201+
},
197202
{
198203
"view": "codacy:cloud-status",
199204
"contents": "You are not part of this organization in Codacy.\n[Join organization](command:codacy.joinOrganization)",

src/common/utils.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ export class CodacyError extends Error {
1616
}
1717
}
1818

19-
export const handleError = (e: Error, showPopup: boolean = true): void => {
19+
export const handleError = (e: Error, showPopup: boolean = true, contextMessage?: string): void => {
20+
const message = contextMessage ? `${contextMessage} - ` : ''
2021
const showErrorMessage = async (message: string) => {
2122
if (!showPopup) return
2223
const choice = await vscode.window.showErrorMessage(message, 'Show Logs')
@@ -31,21 +32,21 @@ export const handleError = (e: Error, showPopup: boolean = true): void => {
3132
if (err.body && err.body.message && err.body.actions) {
3233
const apiError = err.body as ApiError
3334
Logger.error(
34-
`${err.statusText} - ${apiError.message} ${apiError.innerMessage ? `(${apiError.innerMessage})` : ''}`
35+
`${message} ${err.statusText} - ${apiError.message} ${apiError.innerMessage ? `(${apiError.innerMessage})` : ''}`
3536
)
3637
showErrorMessage(apiError.message).catch(console.error)
3738
} else {
38-
Logger.error(`${err.statusText} - ${err.message}`)
39+
Logger.error(`${message} ${err.statusText} - ${err.message}`)
3940
showErrorMessage(err.message).catch(console.error)
4041
}
4142
} else if (e instanceof CodacyError) {
4243
const err = e as CodacyError
43-
Logger.error(`${err.name} - ${err.message}`)
44+
Logger.error(`${message} ${err.name} - ${err.message}`)
4445
if (err.innerException) Logger.debug(`Inner Exception: ${err.innerException.message}`, err.component)
4546
showErrorMessage(err.message).catch(console.error)
4647
} else {
4748
const err = e as Error
48-
Logger.error(err.message)
49+
Logger.error(`${message} ${err.message}`)
4950
}
5051
}
5152

0 commit comments

Comments
 (0)