1
- import { Status } from '@grpc/grpc-js/build/src/constants ' ;
1
+ import { status as grpcStatus } from '@grpc/grpc-js' ;
2
2
import { ensureTemporalFailure } from '@temporalio/common' ;
3
+ import type { temporal } from '@temporalio/proto' ;
3
4
import {
4
5
encodeErrorToFailure ,
5
6
encodeToPayloads ,
@@ -12,8 +13,9 @@ import {
12
13
LoadedWithDefaults ,
13
14
WithDefaults ,
14
15
} from './base-client' ;
15
- import { isServerErrorResponse } from './errors' ;
16
+ import { isGrpcServiceError } from './errors' ;
16
17
import { WorkflowService } from './types' ;
18
+ import { rethrowKnownErrorTypes } from './helpers' ;
17
19
18
20
/**
19
21
* Thrown by {@link AsyncCompletionClient} when trying to complete or heartbeat an Activity that does not exist in the
@@ -95,10 +97,13 @@ export class AsyncCompletionClient extends BaseClient {
95
97
* Transforms grpc errors into well defined TS errors.
96
98
*/
97
99
protected handleError ( err : unknown ) : never {
98
- if ( isServerErrorResponse ( err ) ) {
99
- if ( err . code === Status . NOT_FOUND ) {
100
+ if ( isGrpcServiceError ( err ) ) {
101
+ rethrowKnownErrorTypes ( err ) ;
102
+
103
+ if ( err . code === grpcStatus . NOT_FOUND ) {
100
104
throw new ActivityNotFoundError ( 'Not found' ) ;
101
105
}
106
+
102
107
throw new ActivityCompletionError ( err . details || err . message ) ;
103
108
}
104
109
throw new ActivityCompletionError ( 'Unexpected failure' ) ;
@@ -114,20 +119,21 @@ export class AsyncCompletionClient extends BaseClient {
114
119
async complete ( fullActivityId : FullActivityId , result : unknown ) : Promise < void > ;
115
120
116
121
async complete ( taskTokenOrFullActivityId : Uint8Array | FullActivityId , result : unknown ) : Promise < void > {
122
+ const payloads = await encodeToPayloads ( this . dataConverter , result ) ;
117
123
try {
118
124
if ( taskTokenOrFullActivityId instanceof Uint8Array ) {
119
125
await this . workflowService . respondActivityTaskCompleted ( {
120
126
identity : this . options . identity ,
121
127
namespace : this . options . namespace ,
122
128
taskToken : taskTokenOrFullActivityId ,
123
- result : { payloads : await encodeToPayloads ( this . dataConverter , result ) } ,
129
+ result : { payloads } ,
124
130
} ) ;
125
131
} else {
126
132
await this . workflowService . respondActivityTaskCompletedById ( {
127
133
identity : this . options . identity ,
128
134
namespace : this . options . namespace ,
129
135
...taskTokenOrFullActivityId ,
130
- result : { payloads : await encodeToPayloads ( this . dataConverter , result ) } ,
136
+ result : { payloads } ,
131
137
} ) ;
132
138
}
133
139
} catch ( err ) {
@@ -145,20 +151,21 @@ export class AsyncCompletionClient extends BaseClient {
145
151
async fail ( fullActivityId : FullActivityId , err : unknown ) : Promise < void > ;
146
152
147
153
async fail ( taskTokenOrFullActivityId : Uint8Array | FullActivityId , err : unknown ) : Promise < void > {
154
+ const failure = await encodeErrorToFailure ( this . dataConverter , ensureTemporalFailure ( err ) ) ;
148
155
try {
149
156
if ( taskTokenOrFullActivityId instanceof Uint8Array ) {
150
157
await this . workflowService . respondActivityTaskFailed ( {
151
158
identity : this . options . identity ,
152
159
namespace : this . options . namespace ,
153
160
taskToken : taskTokenOrFullActivityId ,
154
- failure : await encodeErrorToFailure ( this . dataConverter , ensureTemporalFailure ( err ) ) ,
161
+ failure,
155
162
} ) ;
156
163
} else {
157
164
await this . workflowService . respondActivityTaskFailedById ( {
158
165
identity : this . options . identity ,
159
166
namespace : this . options . namespace ,
160
167
...taskTokenOrFullActivityId ,
161
- failure : await encodeErrorToFailure ( this . dataConverter , err ) ,
168
+ failure,
162
169
} ) ;
163
170
}
164
171
} catch ( err ) {
@@ -176,20 +183,21 @@ export class AsyncCompletionClient extends BaseClient {
176
183
reportCancellation ( fullActivityId : FullActivityId , details ?: unknown ) : Promise < void > ;
177
184
178
185
async reportCancellation ( taskTokenOrFullActivityId : Uint8Array | FullActivityId , details ?: unknown ) : Promise < void > {
186
+ const payloads = await encodeToPayloads ( this . dataConverter , details ) ;
179
187
try {
180
188
if ( taskTokenOrFullActivityId instanceof Uint8Array ) {
181
189
await this . workflowService . respondActivityTaskCanceled ( {
182
190
identity : this . options . identity ,
183
191
namespace : this . options . namespace ,
184
192
taskToken : taskTokenOrFullActivityId ,
185
- details : { payloads : await encodeToPayloads ( this . dataConverter , details ) } ,
193
+ details : { payloads } ,
186
194
} ) ;
187
195
} else {
188
196
await this . workflowService . respondActivityTaskCanceledById ( {
189
197
identity : this . options . identity ,
190
198
namespace : this . options . namespace ,
191
199
...taskTokenOrFullActivityId ,
192
- details : { payloads : await encodeToPayloads ( this . dataConverter , details ) } ,
200
+ details : { payloads } ,
193
201
} ) ;
194
202
}
195
203
} catch ( err ) {
@@ -207,36 +215,31 @@ export class AsyncCompletionClient extends BaseClient {
207
215
heartbeat ( fullActivityId : FullActivityId , details ?: unknown ) : Promise < void > ;
208
216
209
217
async heartbeat ( taskTokenOrFullActivityId : Uint8Array | FullActivityId , details ?: unknown ) : Promise < void > {
218
+ const payloads = await encodeToPayloads ( this . dataConverter , details ) ;
210
219
let cancelRequested = false ;
211
220
try {
212
- const response = await this . _sendHeartbeat ( taskTokenOrFullActivityId , details ) ;
213
- cancelRequested = response . cancelRequested ;
221
+ let response : temporal . api . workflowservice . v1 . RecordActivityTaskHeartbeatResponse ;
222
+ if ( taskTokenOrFullActivityId instanceof Uint8Array ) {
223
+ response = await this . workflowService . recordActivityTaskHeartbeat ( {
224
+ identity : this . options . identity ,
225
+ namespace : this . options . namespace ,
226
+ taskToken : taskTokenOrFullActivityId ,
227
+ details : { payloads } ,
228
+ } ) ;
229
+ } else {
230
+ response = await this . workflowService . recordActivityTaskHeartbeatById ( {
231
+ identity : this . options . identity ,
232
+ namespace : this . options . namespace ,
233
+ ...taskTokenOrFullActivityId ,
234
+ details : { payloads } ,
235
+ } ) ;
236
+ }
237
+ cancelRequested = ! ! response . cancelRequested ;
214
238
} catch ( err ) {
215
239
this . handleError ( err ) ;
216
240
}
217
241
if ( cancelRequested ) {
218
242
throw new ActivityCancelledError ( 'cancelled' ) ;
219
243
}
220
244
}
221
-
222
- private async _sendHeartbeat (
223
- taskTokenOrFullActivityId : Uint8Array | FullActivityId ,
224
- details ?: unknown
225
- ) : Promise < { cancelRequested : boolean } > {
226
- if ( taskTokenOrFullActivityId instanceof Uint8Array ) {
227
- return await this . workflowService . recordActivityTaskHeartbeat ( {
228
- identity : this . options . identity ,
229
- namespace : this . options . namespace ,
230
- taskToken : taskTokenOrFullActivityId ,
231
- details : { payloads : await encodeToPayloads ( this . dataConverter , details ) } ,
232
- } ) ;
233
- } else {
234
- return await this . workflowService . recordActivityTaskHeartbeatById ( {
235
- identity : this . options . identity ,
236
- namespace : this . options . namespace ,
237
- ...taskTokenOrFullActivityId ,
238
- details : { payloads : await encodeToPayloads ( this . dataConverter , details ) } ,
239
- } ) ;
240
- }
241
- }
242
245
}
0 commit comments