Skip to content
This repository has been archived by the owner on Apr 8, 2024. It is now read-only.

Commit

Permalink
closes #323
Browse files Browse the repository at this point in the history
  • Loading branch information
jwulf committed Jun 22, 2023
1 parent c12ea78 commit 0371d8d
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 13 deletions.
41 changes: 38 additions & 3 deletions src/__tests__/integration/Client-ThrowError.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,24 @@ jest.setTimeout(25000)

let processId: string

const zbc = new ZBClient()
let zbc: ZBClient

beforeAll(async () => {
processId = (await zbc.deployProcess('./src/__tests__/testdata/Client-ThrowError.bpmn')).processes[0].bpmnProcessId
const zb = new ZBClient()
processId = (await zb.deployProcess('./src/__tests__/testdata/Client-ThrowError.bpmn')).processes[0].bpmnProcessId
cancelProcesses(processId)
await zb.close()
})

afterAll(async () => {
beforeEach(() => {
zbc = new ZBClient()
})

afterEach(async () => {
await zbc.close()
})

afterAll(async () => {
cancelProcesses(processId)
})

Expand All @@ -38,3 +47,29 @@ test('Throws a business error that is caught in the process', async () => {
})
expect(result.variables.bpmnErrorCaught).toBe(true)
})

test('Can set variables when throwing a BPMN Error', async () => {
zbc.createWorker({
taskHandler: job =>
job.error({
errorCode: 'BUSINESS_ERROR',
errorMessage: "Well, that didn't work",
variables: {something: "someValue"}
}),
taskType: 'throw-bpmn-error-task',
timeout: Duration.seconds.of(30),
})
zbc.createWorker({
taskType: 'sad-flow',
taskHandler: job =>
job.complete({
bpmnErrorCaught: true,
}),
})
const result = await zbc.createProcessInstanceWithResult(processId, {
timeout: 20000,
})
console.log(result.variables)
expect(result.variables.bpmnErrorCaught).toBe(true)
// expect(result.variables.something).toBe("someValue")
})
19 changes: 15 additions & 4 deletions src/lib/ZBWorkerBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,14 +325,22 @@ You should call only one job action method in the worker handler. This is a bug
this.completeJob(job.key, completedVariables ?? {})

const errorJob = (job: ZB.Job) => (
errorCode: string,
e: string | ZB.ErrorJobWithVariables,
errorMessage: string = ''
) =>
this.errorJob({
) => {
const isErrorJobWithVariables = (s: string | ZB.ErrorJobWithVariables): s is ZB.ErrorJobWithVariables => typeof s === 'object'
const errorCode = isErrorJobWithVariables(e) ? e.errorCode : e
errorMessage = isErrorJobWithVariables(e) ? e.errorMessage ?? '' : errorMessage
const variables = isErrorJobWithVariables(e) ? e.variables : {}

return this.errorJob({
errorCode,
errorMessage,
job,
variables
})
}

const fail = failJob(thisJob)
const succeed = succeedJob(thisJob)
return {
Expand Down Expand Up @@ -400,16 +408,19 @@ You should call only one job action method in the worker handler. This is a bug
errorCode,
errorMessage,
job,
variables
}: {
job: ZB.Job
errorCode: string
errorMessage: string
errorMessage: string,
variables: ZB.JSONDoc
}) {
return this.zbClient
.throwError({
errorCode,
errorMessage,
jobKey: job.key,
variables
})
.then(() =>
this.logger.logDebug(`Errored job ${job.key} - ${errorMessage}`)
Expand Down
17 changes: 13 additions & 4 deletions src/lib/interfaces-1.0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,17 @@ declare function FailureHandler(
failureConfiguration: JobFailureConfiguration
): Promise<JOB_ACTION_ACKNOWLEDGEMENT>

export interface ErrorJobWithVariables {
variables: JSONDoc,
errorCode: string,
errorMessage?: string
}

export type ErrorJobOutcome = (
errorCode: string | ErrorJobWithVariables,
errorMessage?: string
) => Promise<JOB_ACTION_ACKNOWLEDGEMENT>

export interface JobCompletionInterface<WorkerOutputVariables> {
/**
* Cancel the workflow.
Expand Down Expand Up @@ -174,12 +185,10 @@ export interface JobCompletionInterface<WorkerOutputVariables> {
* The error is handled in the process by an error catch event.
* If there is no error catch event with the specified errorCode then an incident will be raised instead.
*/
error: (
errorCode: string,
errorMessage?: string
) => Promise<JOB_ACTION_ACKNOWLEDGEMENT>
error: ErrorJobOutcome
}


export interface ZeebeJob<
WorkerInputVariables = IInputVariables,
CustomHeaderShape = ICustomHeaders,
Expand Down
10 changes: 9 additions & 1 deletion src/lib/interfaces-grpc-1.0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,15 @@ export interface ThrowErrorRequest {
// the error code that will be matched with an error catch event
errorCode: string
// an optional error message that provides additional context
errorMessage: string
errorMessage?: string
/**
* JSON document that will instantiate the variables at the local scope of the error catch
* event that catches the thrown error; it must be a JSON object, as variables will be mapped in a
* key-value fashion. e.g. { "a": 1, "b": 2 } will create two variables, named "a" and
* "b" respectively, with their associated values. [{ "a": 1, "b": 2 }] would not be a
* valid argument, as the root of the JSON document is an array and not an object.
*/
variables?: JSONDoc
}

export interface CompleteJobRequest<Variables = IProcessVariables> {
Expand Down
3 changes: 2 additions & 1 deletion src/zb/ZBClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1229,8 +1229,9 @@ export class ZBClient extends TypedEmitter<typeof ConnectionStatusEvent> {
* ```
*/
public throwError(throwErrorRequest: Grpc.ThrowErrorRequest) {
const req = stringifyVariables({...throwErrorRequest, variables: throwErrorRequest.variables ?? {}})
return this.executeOperation('throwError', () =>
this.grpc.throwErrorSync(throwErrorRequest)
this.grpc.throwErrorSync(req)
)
}

Expand Down

0 comments on commit 0371d8d

Please sign in to comment.