Skip to content

Commit

Permalink
improvement(cloud): better API errors
Browse files Browse the repository at this point in the history
We now throw an error when a null client auth token / jwt is received
from the backend during login.

Previously, we were only logging an error, without indicating that the
command had failed. The error message was also a bit cryptic.

Also, when workflow run registration fails due to CLI/backend API
incompatibility (i.e. if the shape of the registration request isn't
what the API expects), we now throw a better error, and include the
failed request payload in the error detail.
  • Loading branch information
thsig committed Jun 8, 2021
1 parent 7d64bdc commit 5b95958
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
11 changes: 10 additions & 1 deletion core/src/enterprise/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@ export class EnterpriseApi {
}

static async saveAuthToken(log: LogEntry, tokenResponse: AuthTokenResponse) {
if (!tokenResponse.token) {
throw new EnterpriseApiError(
`Received a null/empty client auth token while logging in. Please contact your system administrator.`,
{ tokenResponse }
)
}
try {
const manager = ClientAuthToken.getConnection().manager
await manager.transaction(async (transactionalEntityManager) => {
Expand All @@ -201,7 +207,10 @@ export class EnterpriseApi {
})
log.debug("Saved client auth token to local config db")
} catch (error) {
log.error(`An error occurred while saving client auth token to local config db:\n${error.message}`)
throw new EnterpriseApiError(
`An error occurred while saving client auth token to local config db:\n${error.message}`,
{ tokenResponse }
)
}
}

Expand Down
18 changes: 15 additions & 3 deletions core/src/enterprise/workflow-lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import { LogEntry } from "../logger/log-entry"
import { EnterpriseApiError } from "../exceptions"
import { gardenEnv } from "../constants"
import { Garden } from "../garden"
import { ApiFetchResponse } from "./api"
import { ApiFetchResponse, isGotError } from "./api"
import { RegisterWorkflowRunResponse } from "@garden-io/platform-api-types"
import { deline } from "../util/string"

export interface RegisterWorkflowRunParams {
workflowConfig: WorkflowConfig
Expand Down Expand Up @@ -52,8 +53,19 @@ export async function registerWorkflowRun({
retryDescription: "Registering workflow run",
})
} catch (err) {
log.error(`An error occurred while registering workflow run: ${err.message}`)
throw err
if (isGotError(err, 422)) {
const errMsg = deline`
Workflow run registration failed due to mismatch between CLI and API versions. Please make sure your Garden
CLI version is compatible with your version of Garden Enterprise. See error.log for details
on the failed registration request payload.
`
throw new EnterpriseApiError(errMsg, {
requestData,
})
} else {
log.error(`An error occurred while registering workflow run: ${err.message}`)
throw err
}
}

if (res?.workflowRunUid && res?.status === "success") {
Expand Down
1 change: 0 additions & 1 deletion core/test/unit/src/config/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ const enterpriseDomain = "https://garden.mydomain.com"
const commandInfo = { name: "test", args: {}, opts: {} }

describe("resolveProjectConfig", () => {

it("should pass through a canonical project config", async () => {
const defaultEnvironment = "default"
const config: ProjectConfig = {
Expand Down
4 changes: 3 additions & 1 deletion core/test/unit/src/enterprise/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ describe("EnterpriseApi", () => {
},
],
async (token) => {
await EnterpriseApi.saveAuthToken(log, token)
try {
await EnterpriseApi.saveAuthToken(log, token)
} catch (err) {}
}
)
const count = await ClientAuthToken.count()
Expand Down

0 comments on commit 5b95958

Please sign in to comment.