Skip to content
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

chore: Improve error handling #1275

Merged
merged 5 commits into from
Jun 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions src/api/che-api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import axios, { AxiosInstance } from 'axios'
import { cli } from 'cli-ux'
import * as https from 'https'

import { sleep } from '../util'
import { newError, sleep } from '../util'

/**
* Singleton responsible for calls to Che API.
Expand Down Expand Up @@ -344,28 +344,27 @@ export class CheApiClient {
if (error.response) {
const status = error.response.status
if (status === 403) {
return new Error(`E_CHE_API_FORBIDDEN - Endpoint: ${endpoint} - Message: ${JSON.stringify(error.response.data.message)}`)
return newError(`E_CHE_API_FORBIDDEN - Endpoint: ${endpoint} - Message: ${JSON.stringify(error.response.data.message)}`, error)
} else if (status === 401) {
return new Error(`E_CHE_API_UNAUTHORIZED - Endpoint: ${endpoint} - Message: ${JSON.stringify(error.response.data)}`)
return newError(`E_CHE_API_UNAUTHORIZED - Endpoint: ${endpoint} - Message: ${JSON.stringify(error.response.data)}`, error)
} else if (status === 404) {
return new Error(`E_CHE_API_NOTFOUND - Endpoint: ${endpoint} - Message: ${JSON.stringify(error.response.data)}`)
return newError(`E_CHE_API_NOTFOUND - Endpoint: ${endpoint} - Message: ${JSON.stringify(error.response.data)}`, error)
} else if (status === 503) {
return new Error(`E_CHE_API_UNAVAIL - Endpoint: ${endpoint} returned 503 code`)
return newError(`E_CHE_API_UNAVAIL - Endpoint: ${endpoint} returned 503 code`, error)
} else {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
return new Error(`E_CHE_API_UNKNOWN_ERROR - Endpoint: ${endpoint} -Status: ${error.response.status}`)
return newError(`E_CHE_API_UNKNOWN_ERROR - Endpoint: ${endpoint} -Status: ${error.response.status}`, error)
}

} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
return new Error(`E_CHE_API_NO_RESPONSE - Endpoint: ${endpoint} - Error message: ${error.message}`)
return newError(`E_CHE_API_NO_RESPONSE - Endpoint: ${endpoint} - Error message: ${error.message}`, error)
} else {
// Something happened in setting up the request that triggered an Error
return new Error(`E_CHECTL_UNKNOWN_ERROR - Endpoint: ${endpoint} - Message: ${error.message}`)
return newError(`E_CHECTL_UNKNOWN_ERROR - Endpoint: ${endpoint} - Message: ${error.message}`, error)
}
}

}
14 changes: 7 additions & 7 deletions src/api/kube.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import * as net from 'net'
import { Writable } from 'stream'

import { CHE_CLUSTER_API_GROUP, CHE_CLUSTER_API_VERSION, CHE_CLUSTER_KIND_PLURAL, DEFAULT_K8S_POD_ERROR_RECHECK_TIMEOUT, DEFAULT_K8S_POD_WAIT_TIMEOUT, OLM_STABLE_CHANNEL_NAME } from '../constants'
import { getClusterClientCommand, isKubernetesPlatformFamily, safeLoadFromYamlFile } from '../util'
import { getClusterClientCommand, isKubernetesPlatformFamily, newError, safeLoadFromYamlFile } from '../util'

import { V1Certificate } from './typings/cert-manager'
import { CatalogSource, ClusterServiceVersion, ClusterServiceVersionList, InstallPlan, OperatorGroup, PackageManifest, Subscription } from './typings/olm'
Expand Down Expand Up @@ -1646,11 +1646,9 @@ export class KubeHelper {

return crs[0]
} catch (e) {
if (e.response.statusCode === 404) {
// There is no CRD
return
if (e.response && e.response.statusCode !== 404) {
throw this.wrapK8sClientError(e)
}
throw this.wrapK8sClientError(e)
}
}

Expand Down Expand Up @@ -2635,8 +2633,10 @@ export class KubeHelper {
* @param e k8s error to wrap
*/
private wrapK8sClientError(e: any): Error {
if (e.response && e.response.body && e.response.body.message) return new Error(e.response.body.message)
else return new Error(e)
if (e.response && e.response.body) {
return newError(e.response.body, e)
}
return e
}

public safeLoadFromYamlFile(filePath: string): any {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/auth/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { ChectlContext } from '../../api/context'
import { KubeHelper } from '../../api/kube'
import { cheNamespace, CHE_API_ENDPOINT_KEY, CHE_TELEMETRY, username, USERNAME_KEY } from '../../common-flags'
import { DEFAULT_ANALYTIC_HOOK_NAME } from '../../constants'
import { getCommandErrorMessage, OPENSHIFT_CLI } from '../../util'
import { OPENSHIFT_CLI, wrapCommandError } from '../../util'

const REFRESH_TOKEN_KEY = 'refresh-token'
const PASSWORD_KEY = 'password'
Expand Down Expand Up @@ -161,7 +161,7 @@ export default class Login extends Command {
const username = await loginManager.setLoginContext(cheApiEndpoint, loginData)
cli.info(`Successfully logged into ${cheApiEndpoint} as ${username}`)
} catch (err) {
this.error(getCommandErrorMessage(err))
this.error(wrapCommandError(err))
}
}
}
4 changes: 2 additions & 2 deletions src/commands/cacert/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { ChectlContext } from '../../api/context'
import { KubeHelper } from '../../api/kube'
import { cheNamespace, CHE_TELEMETRY, skipKubeHealthzCheck } from '../../common-flags'
import { DEFAULT_ANALYTIC_HOOK_NAME, DEFAULT_CA_CERT_FILE_NAME } from '../../constants'
import { findWorkingNamespace, getCommandErrorMessage } from '../../util'
import { findWorkingNamespace, wrapCommandError } from '../../util'

export default class Export extends Command {
static description = 'Retrieves Eclipse Che self-signed certificate'
Expand Down Expand Up @@ -62,7 +62,7 @@ export default class Export extends Command {
this.log('Self signed certificate secret not found. Is commonly trusted certificate used?')
}
} catch (err) {
this.error(getCommandErrorMessage(err))
this.error(wrapCommandError(err))
}
}
}
6 changes: 3 additions & 3 deletions src/commands/server/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { cheNamespace, CHE_TELEMETRY, listrRenderer, skipKubeHealthzCheck } from
import { DEFAULT_ANALYTIC_HOOK_NAME } from '../../constants'
import { CheTasks } from '../../tasks/che'
import { ApiTasks } from '../../tasks/platforms/api'
import { findWorkingNamespace, getCommandErrorMessage } from '../../util'
import { findWorkingNamespace, wrapCommandError } from '../../util'

export default class Debug extends Command {
static description = 'Enable local debug of Eclipse Che server'
Expand All @@ -44,7 +44,7 @@ export default class Debug extends Command {
const apiTasks = new ApiTasks()
const tasks = new Listr([], { renderer: flags['listr-renderer'] as any })

tasks.add(apiTasks.testApiTasks(flags, this))
tasks.add(apiTasks.testApiTasks(flags))
tasks.add(cheTasks.verifyCheNamespaceExistsTask(flags, this))
tasks.add(cheTasks.debugTask(flags))

Expand All @@ -53,7 +53,7 @@ export default class Debug extends Command {
this.log(`Eclipse Che server debug is available on localhost:${flags['debug-port']}.`)
this.log('The program keeps running to enable port forwarding.')
} catch (err) {
this.error(getCommandErrorMessage(err))
this.error(wrapCommandError(err))
}
}
}
6 changes: 3 additions & 3 deletions src/commands/server/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { HelmTasks } from '../../tasks/installers/helm'
import { OLMTasks } from '../../tasks/installers/olm'
import { OperatorTasks } from '../../tasks/installers/operator'
import { ApiTasks } from '../../tasks/platforms/api'
import { findWorkingNamespace, getCommandErrorMessage, getCommandSuccessMessage, notifyCommandCompletedSuccessfully } from '../../util'
import { findWorkingNamespace, getCommandSuccessMessage, notifyCommandCompletedSuccessfully, wrapCommandError } from '../../util'

export default class Delete extends Command {
static description = 'delete any Eclipse Che related resource: Kubernetes/OpenShift/Helm'
Expand Down Expand Up @@ -71,7 +71,7 @@ export default class Delete extends Command {
const devWorkspaceTasks = new DevWorkspaceTasks(flags)

const tasks = new Listrq([], ctx.listrOptions)
tasks.add(apiTasks.testApiTasks(flags, this))
tasks.add(apiTasks.testApiTasks(flags))
tasks.add(operatorTasks.deleteTasks(flags))
tasks.add(olmTasks.deleteTasks(flags))
tasks.add(cheTasks.deleteTasks(flags))
Expand All @@ -98,7 +98,7 @@ export default class Delete extends Command {
await tasks.run()
cli.log(getCommandSuccessMessage())
} catch (err) {
this.error(getCommandErrorMessage(err))
this.error(wrapCommandError(err))
}
} else {
this.exit(0)
Expand Down
8 changes: 4 additions & 4 deletions src/commands/server/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { checkChectlAndCheVersionCompatibility, downloadTemplates, getPrintHighl
import { InstallerTasks } from '../../tasks/installers/installer'
import { ApiTasks } from '../../tasks/platforms/api'
import { PlatformTasks } from '../../tasks/platforms/platform'
import { askForChectlUpdateIfNeeded, getCommandErrorMessage, getCommandSuccessMessage, getEmbeddedTemplatesDirectory, getProjectName, isKubernetesPlatformFamily, isOpenshiftPlatformFamily, notifyCommandCompletedSuccessfully } from '../../util'
import { askForChectlUpdateIfNeeded, getCommandSuccessMessage, getEmbeddedTemplatesDirectory, getProjectName, isKubernetesPlatformFamily, isOpenshiftPlatformFamily, notifyCommandCompletedSuccessfully, wrapCommandError } from '../../util'

export default class Deploy extends Command {
static description = 'Deploy Eclipse Che server'
Expand Down Expand Up @@ -395,10 +395,10 @@ export default class Deploy extends Command {

// Checks if Eclipse Che is already deployed
let preInstallTasks = new Listr(undefined, ctx.listrOptions)
preInstallTasks.add(apiTasks.testApiTasks(flags, this))
preInstallTasks.add(apiTasks.testApiTasks(flags))
preInstallTasks.add({
title: '👀 Looking for an already existing Eclipse Che instance',
task: () => new Listr(cheTasks.checkIfCheIsInstalledTasks(flags, this))
task: () => new Listr(cheTasks.checkIfCheIsInstalledTasks(flags))
})
preInstallTasks.add(checkChectlAndCheVersionCompatibility(flags))
preInstallTasks.add(downloadTemplates(flags))
Expand Down Expand Up @@ -444,7 +444,7 @@ export default class Deploy extends Command {
this.log(getCommandSuccessMessage())
}
} catch (err) {
this.error(getCommandErrorMessage(err))
this.error(wrapCommandError(err))
}

notifyCommandCompletedSuccessfully()
Expand Down
6 changes: 3 additions & 3 deletions src/commands/server/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { cheDeployment, cheNamespace, CHE_TELEMETRY, listrRenderer, skipKubeHeal
import { DEFAULT_ANALYTIC_HOOK_NAME } from '../../constants'
import { CheTasks } from '../../tasks/che'
import { ApiTasks } from '../../tasks/platforms/api'
import { findWorkingNamespace, getCommandErrorMessage, getCommandSuccessMessage } from '../../util'
import { findWorkingNamespace, getCommandSuccessMessage, wrapCommandError } from '../../util'

export default class Logs extends Command {
static description = 'Collect Eclipse Che logs'
Expand Down Expand Up @@ -46,7 +46,7 @@ export default class Logs extends Command {
const tasks = new Listr([], { renderer: flags['listr-renderer'] as any })

await this.config.runHook(DEFAULT_ANALYTIC_HOOK_NAME, { command: Logs.id, flags })
tasks.add(apiTasks.testApiTasks(flags, this))
tasks.add(apiTasks.testApiTasks(flags))
tasks.add(cheTasks.verifyCheNamespaceExistsTask(flags, this))
tasks.add(cheTasks.serverLogsTasks(flags, false))

Expand All @@ -55,7 +55,7 @@ export default class Logs extends Command {
await tasks.run(ctx)
this.log(getCommandSuccessMessage())
} catch (err) {
this.error(getCommandErrorMessage(err))
this.error(wrapCommandError(err))
}

this.exit(0)
Expand Down
8 changes: 4 additions & 4 deletions src/commands/server/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { ChectlContext } from '../../api/context'
import { cheDeployment, cheNamespace, k8sPodDownloadImageTimeout, K8SPODDOWNLOADIMAGETIMEOUT_KEY, k8sPodErrorRecheckTimeout, K8SPODERRORRECHECKTIMEOUT_KEY, k8sPodReadyTimeout, K8SPODREADYTIMEOUT_KEY, k8sPodWaitTimeout, K8SPODWAITTIMEOUT_KEY, listrRenderer, logsDirectory, LOG_DIRECTORY_KEY, skipKubeHealthzCheck } from '../../common-flags'
import { CheTasks } from '../../tasks/che'
import { ApiTasks } from '../../tasks/platforms/api'
import { findWorkingNamespace, getCommandErrorMessage, getCommandSuccessMessage, notifyCommandCompletedSuccessfully } from '../../util'
import { findWorkingNamespace, getCommandSuccessMessage, notifyCommandCompletedSuccessfully, wrapCommandError } from '../../util'

export default class Start extends Command {
static description = 'Start Eclipse Che server'
Expand Down Expand Up @@ -44,10 +44,10 @@ export default class Start extends Command {

// Checks if Eclipse Che is already deployed
const preInstallTasks = new Listr([
apiTasks.testApiTasks(flags, this),
apiTasks.testApiTasks(flags),
{
title: '👀 Looking for an already existing Eclipse Che instance',
task: () => new Listr(cheTasks.checkIfCheIsInstalledTasks(flags, this))
task: () => new Listr(cheTasks.checkIfCheIsInstalledTasks(flags))
}], ctx.listrOptions)

const logsTasks = new Listr([{
Expand All @@ -73,7 +73,7 @@ export default class Start extends Command {
this.log(getCommandSuccessMessage())
}
} catch (err) {
this.error(getCommandErrorMessage(err))
this.error(wrapCommandError(err))
}

notifyCommandCompletedSuccessfully()
Expand Down
10 changes: 5 additions & 5 deletions src/commands/server/stop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { accessToken, cheDeployment, cheNamespace, CHE_TELEMETRY, listrRenderer,
import { DEFAULT_ANALYTIC_HOOK_NAME } from '../../constants'
import { CheTasks } from '../../tasks/che'
import { ApiTasks } from '../../tasks/platforms/api'
import { findWorkingNamespace, getCommandErrorMessage, getCommandSuccessMessage, notifyCommandCompletedSuccessfully } from '../../util'
import { findWorkingNamespace, getCommandSuccessMessage, notifyCommandCompletedSuccessfully, wrapCommandError } from '../../util'

export default class Stop extends Command {
static description = 'stop Eclipse Che server'
Expand Down Expand Up @@ -55,8 +55,8 @@ export default class Stop extends Command {
}
)

tasks.add(apiTasks.testApiTasks(flags, this))
tasks.add(cheTasks.checkIfCheIsInstalledTasks(flags, this))
tasks.add(apiTasks.testApiTasks(flags))
tasks.add(cheTasks.checkIfCheIsInstalledTasks(flags))
tasks.add([
{
title: 'Deployment doesn\'t exist',
Expand All @@ -68,13 +68,13 @@ export default class Stop extends Command {
],
{ renderer: flags['listr-renderer'] as any }
)
tasks.add(cheTasks.scaleCheDownTasks(this))
tasks.add(cheTasks.scaleCheDownTasks())
tasks.add(cheTasks.waitPodsDeletedTasks())
try {
await tasks.run()
cli.log(getCommandSuccessMessage())
} catch (err) {
this.error(getCommandErrorMessage(err))
this.error(wrapCommandError(err))
}

notifyCommandCompletedSuccessfully()
Expand Down
6 changes: 3 additions & 3 deletions src/commands/server/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { DEFAULT_ANALYTIC_HOOK_NAME, DEFAULT_CHE_OPERATOR_IMAGE_NAME, MIN_CHE_OP
import { checkChectlAndCheVersionCompatibility, downloadTemplates, getPrintHighlightedMessagesTask } from '../../tasks/installers/common-tasks'
import { InstallerTasks } from '../../tasks/installers/installer'
import { ApiTasks } from '../../tasks/platforms/api'
import { askForChectlUpdateIfNeeded, findWorkingNamespace, getCommandErrorMessage, getCommandSuccessMessage, getEmbeddedTemplatesDirectory, getProjectName, getProjectVersion, notifyCommandCompletedSuccessfully } from '../../util'
import { askForChectlUpdateIfNeeded, findWorkingNamespace, getCommandSuccessMessage, getEmbeddedTemplatesDirectory, getProjectName, getProjectVersion, notifyCommandCompletedSuccessfully, wrapCommandError } from '../../util'

export default class Update extends Command {
static description = 'Update Eclipse Che server.'
Expand Down Expand Up @@ -126,7 +126,7 @@ export default class Update extends Command {
// pre update tasks
const apiTasks = new ApiTasks()
const preUpdateTasks = new Listr([], ctx.listrOptions)
preUpdateTasks.add(apiTasks.testApiTasks(flags, this))
preUpdateTasks.add(apiTasks.testApiTasks(flags))
preUpdateTasks.add(checkChectlAndCheVersionCompatibility(flags))
preUpdateTasks.add(downloadTemplates(flags))
preUpdateTasks.add(installerTasks.preUpdateTasks(flags, this))
Expand Down Expand Up @@ -158,7 +158,7 @@ export default class Update extends Command {

this.log(getCommandSuccessMessage())
} catch (err) {
this.error(getCommandErrorMessage(err))
this.error(wrapCommandError(err))
}

notifyCommandCompletedSuccessfully()
Expand Down
4 changes: 2 additions & 2 deletions src/commands/workspace/inject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { ChectlContext } from '../../api/context'
import { KubeHelper } from '../../api/kube'
import { accessToken, ACCESS_TOKEN_KEY, cheApiEndpoint, cheNamespace, CHE_API_ENDPOINT_KEY, CHE_TELEMETRY, skipKubeHealthzCheck } from '../../common-flags'
import { DEFAULT_ANALYTIC_HOOK_NAME } from '../../constants'
import { getClusterClientCommand, getCommandErrorMessage, OPENSHIFT_CLI } from '../../util'
import { getClusterClientCommand, OPENSHIFT_CLI, wrapCommandError } from '../../util'

export default class Inject extends Command {
static description = 'Inject configurations and tokens in a workspace'
Expand Down Expand Up @@ -100,7 +100,7 @@ export default class Inject extends Command {
try {
await this.injectKubeconfig(flags, workspaceNamespace, workspacePodName, workspaceId!)
} catch (err) {
this.error(getCommandErrorMessage(err))
this.error(wrapCommandError(err))
}
}

Expand Down
Loading