Skip to content

Commit

Permalink
feat(api-client, react-api-client): Add client support for `/runs/:ru…
Browse files Browse the repository at this point in the history
…nId/errorRecoveryPolicy` (#15851)

Works towards EXEC-560
  • Loading branch information
mjhuff authored Jul 31, 2024
1 parent ec7298a commit 567dd5e
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 0 deletions.
2 changes: 2 additions & 0 deletions api-client/src/runs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ export { createRunAction } from './createRunAction'
export * from './createLabwareOffset'
export * from './createLabwareDefinition'
export * from './constants'
export * from './updateErrorRecoveryPolicy'

export * from './types'
export type { CreateRunData } from './createRun'
26 changes: 26 additions & 0 deletions api-client/src/runs/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,29 @@ export interface CommandData {
// Although run errors are semantically different from command errors,
// the server currently happens to use the exact same model for both.
export type RunError = RunCommandError

/**
* Error Policy
*/

export type IfMatchType = 'ignoreAndContinue' | 'failRun' | 'waitForRecovery'

export interface ErrorRecoveryPolicy {
policyRules: Array<{
matchCriteria: {
command: {
commandType: RunTimeCommand['commandType']
error: {
errorType: RunCommandError['errorType']
}
}
}
ifMatch: IfMatchType
}>
}

export interface UpdateErrorRecoveryPolicyRequest {
data: ErrorRecoveryPolicy
}

export type UpdateErrorRecoveryPolicyResponse = Record<string, never>
48 changes: 48 additions & 0 deletions api-client/src/runs/updateErrorRecoveryPolicy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { PUT, request } from '../request'

import type { HostConfig } from '../types'
import type { ResponsePromise } from '../request'
import type {
ErrorRecoveryPolicy,
IfMatchType,
UpdateErrorRecoveryPolicyRequest,
UpdateErrorRecoveryPolicyResponse,
} from './types'
import type { RunCommandError, RunTimeCommand } from '@opentrons/shared-data'

export type RecoveryPolicyRulesParams = Array<{
commandType: RunTimeCommand['commandType']
errorType: RunCommandError['errorType']
ifMatch: IfMatchType
}>

export function updateErrorRecoveryPolicy(
config: HostConfig,
runId: string,
policyRules: RecoveryPolicyRulesParams
): ResponsePromise<UpdateErrorRecoveryPolicyResponse> {
const policy = buildErrorRecoveryPolicyBody(policyRules)

return request<
UpdateErrorRecoveryPolicyResponse,
UpdateErrorRecoveryPolicyRequest
>(PUT, `/runs/${runId}/errorRecoveryPolicy`, { data: policy }, config)
}

function buildErrorRecoveryPolicyBody(
policyRules: RecoveryPolicyRulesParams
): ErrorRecoveryPolicy {
return {
policyRules: policyRules.map(rule => ({
matchCriteria: {
command: {
commandType: rule.commandType,
error: {
errorType: rule.errorType,
},
},
},
ifMatch: rule.ifMatch,
})),
}
}
1 change: 1 addition & 0 deletions react-api-client/src/runs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export { useAllCommandsAsPreSerializedList } from './useAllCommandsAsPreSerializ
export { useCommandQuery } from './useCommandQuery'
export * from './useCreateLabwareOffsetMutation'
export * from './useCreateLabwareDefinitionMutation'
export * from './useUpdateErrorRecoveryPolicy'

export type { UsePlayRunMutationResult } from './usePlayRunMutation'
export type { UsePauseRunMutationResult } from './usePauseRunMutation'
Expand Down
62 changes: 62 additions & 0 deletions react-api-client/src/runs/useUpdateErrorRecoveryPolicy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { useMutation } from 'react-query'

import { updateErrorRecoveryPolicy } from '@opentrons/api-client'

import { useHost } from '../api'

import type {
UseMutationOptions,
UseMutationResult,
UseMutateFunction,
} from 'react-query'
import type { AxiosError } from 'axios'
import type {
RecoveryPolicyRulesParams,
UpdateErrorRecoveryPolicyResponse,
HostConfig,
} from '@opentrons/api-client'

export type UseUpdateErrorRecoveryPolicyResponse = UseMutationResult<
UpdateErrorRecoveryPolicyResponse,
AxiosError,
RecoveryPolicyRulesParams
> & {
updateErrorRecoveryPolicy: UseMutateFunction<
UpdateErrorRecoveryPolicyResponse,
AxiosError,
RecoveryPolicyRulesParams
>
}

export type UseUpdateErrorRecoveryPolicyOptions = UseMutationOptions<
UpdateErrorRecoveryPolicyResponse,
AxiosError,
RecoveryPolicyRulesParams
>

export function useUpdateErrorRecoveryPolicy(
runId: string,
options: UseUpdateErrorRecoveryPolicyOptions = {}
): UseUpdateErrorRecoveryPolicyResponse {
const host = useHost()

const mutation = useMutation<
UpdateErrorRecoveryPolicyResponse,
AxiosError,
RecoveryPolicyRulesParams
>(
[host, 'runs', runId, 'errorRecoveryPolicy'],
(policyRules: RecoveryPolicyRulesParams) =>
updateErrorRecoveryPolicy(host as HostConfig, runId, policyRules)
.then(response => response.data)
.catch(e => {
throw e
}),
options
)

return {
...mutation,
updateErrorRecoveryPolicy: mutation.mutate,
}
}

0 comments on commit 567dd5e

Please sign in to comment.