Skip to content

Commit

Permalink
Merge pull request #346 from prezly/fix/dev-18563-api-errors-handling
Browse files Browse the repository at this point in the history
[DEV-18563] Fix API error catching&throwing @ DeferredJobsApiClient
  • Loading branch information
e1himself authored Nov 21, 2024
2 parents 15de064 + d7f873f commit ff9c42e
Showing 1 changed file with 30 additions and 25 deletions.
55 changes: 30 additions & 25 deletions src/api/DeferredJobsApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,37 @@ function handleDeferredJob<V = any, P = any>(
request: Promise<ApiResponse<V>>,
): ProgressPromise<V, P> {
return new ProgressPromise<V, P>(async (resolve, reject, update) => {
const response = await request;

if (response.status === HttpCodes.ACCEPTED && isDeferredJobResponse(response.payload)) {
const id = response.payload.progress.id;
do {
const response = await api.get<{ job: Job<V, P> }>(`${routing.jobsUrl}/${id}`, {
fetch,
});
const state = response.payload.job.state;

if (state.status === JobStatus.RESOLVED) {
resolve(state.value);
return;
}
if (state.status === JobStatus.REJECTED) {
reject(state.value);
return;
}

update(state.progress, state.value);

await sleep(JOB_STATUS_POLLING_INTERVAL);
} while (true); // eslint-disable-line no-constant-condition
try {
const response = await request;

if (response.status === HttpCodes.ACCEPTED && isDeferredJobResponse(response.payload)) {
const id = response.payload.progress.id;
do {
const response = await api.get<{ job: Job<V, P> }>(`${routing.jobsUrl}/${id}`, {
fetch,
});

const state = response.payload.job.state;

if (state.status === JobStatus.RESOLVED) {
resolve(state.value);
return;
}
if (state.status === JobStatus.REJECTED) {
reject(state.value);
return;
}

update(state.progress, state.value);

await sleep(JOB_STATUS_POLLING_INTERVAL);
} while (true); // eslint-disable-line no-constant-condition
}

resolve(response.payload);
} catch (error) {
reject(error);
}

resolve(response.payload);
});
}

Expand Down

0 comments on commit ff9c42e

Please sign in to comment.