Skip to content

Commit

Permalink
quick_setup: handle background jobs errors
Browse files Browse the repository at this point in the history
Change-Id: I4e4bef39b53a147586f0eda96528b8d3a8f76930
  • Loading branch information
lpetrora committed Dec 27, 2024
1 parent 6338bc0 commit e2627e6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
21 changes: 17 additions & 4 deletions packages/cmk-frontend-vue/src/quick-setup/rest-api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { StageData } from '../components/quick-setup/widgets/widget_types'
import {
BACKGROUND_JOB_CHECK_INTERVAL,
EDIT_QUICK_SETUP_URL,
FETCH_BACKGROUND_JOB_RESULT_URL,
FETCH_STAGE_BACKGROUND_JOB_RESULT_URL,
FETCH_QUICK_SETUP_OVERVIEW_URL,
FETCH_QUICK_SETUP_STAGE_STRUCTURE_URL,
GET_BACKGROUND_JOB_STATUS_URL,
Expand Down Expand Up @@ -214,10 +214,13 @@ const _waitForBackgroundJobToFinish = async (id: string): Promise<void> => {
*/
const _getDataFromBackgroundJob = async (jobId: string): Promise<unknown> => {
await _waitForBackgroundJobToFinish(jobId)
const jobResultUrl = FETCH_BACKGROUND_JOB_RESULT_URL.replace('{JOB_ID}', jobId)
const jobResultUrl = FETCH_STAGE_BACKGROUND_JOB_RESULT_URL.replace('{JOB_ID}', jobId)
const result = await axios.get(jobResultUrl)

//TODO: It is possible that a 200 response carries error messages. Should be handled here
//It is possible that a 200 response carries error messages. This will raise a CmkError
if (result.data?.background_job_exception) {
throw result.data.background_job_exception
}

return result.data
}
Expand All @@ -231,7 +234,17 @@ const _saveQuickSetupAndFetchRedirect = async (
let { data } = await axios.request({ url, method, data: payload })

if (data?.domainType === 'background_job') {
data = await _getDataFromBackgroundJob(data.id)
const jobId = data.id
await _waitForBackgroundJobToFinish(jobId)
const jobResultUrl = FETCH_STAGE_BACKGROUND_JOB_RESULT_URL.replace('{JOB_ID}', jobId)
const result = await axios.get(jobResultUrl)

//It is possible that a 200 response carries error messages. This will raise a CmkError
if (result.data?.background_job_exception) {
throw result.data.background_job_exception
}

data = result.data
}

return data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,8 @@ export const GET_BACKGROUND_JOB_STATUS_URL = `${API_ROOT}/objects/background_job
/** @constant {number} BACKGROUND_JOB_CHECK_INTERVAL - Wait time in milliseconds */
export const BACKGROUND_JOB_CHECK_INTERVAL = 5000

/** @constant {string} FETCH_STAGE_BACKGROUND_JOB_RESULT_URL - Get background job result */
export const FETCH_STAGE_BACKGROUND_JOB_RESULT_URL = `${API_ROOT}/objects/quick_setup_stage_action_result/{JOB_ID}`

/** @constant {string} FETCH_BACKGROUND_JOB_RESULT_URL - Get background job result */
export const FETCH_BACKGROUND_JOB_RESULT_URL = `${API_ROOT}/objects/quick_setup_stage_action_result/{JOB_ID}`
export const FETCH_BACKGROUND_JOB_RESULT_URL = `${API_ROOT}/objects/quick_setup_action_result/{JOB_ID}`
6 changes: 3 additions & 3 deletions packages/cmk-frontend-vue/src/quick-setup/rest-api/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ export interface AllStagesValidationError extends RestApiError, OrUndefined<Stag
/**
* Returns a record representation of an error to be shown to the user
* @param err unknown
* @returns ValidationError | AllStagesValidationError
* @returns ValidationError | AllStagesValidationError | CmkError<unknown> | QuickSetupAxiosError
*/
export const processError = (
err: unknown
): ValidationError | AllStagesValidationError | CmkError<unknown> | QuickSetupAxiosError => {
if (axios.isAxiosError(err)) {
if (err.response?.status === 400) {
if (err.response.data?.errors) {
const responseErrors = err.response.data?.errors
if (err.response.data?.validation_errors) {
const responseErrors = err.response.data?.validation_errors
const responseDetail = err.response.data?.detail
return {
type: 'validation',
Expand Down

0 comments on commit e2627e6

Please sign in to comment.