@@ -60,8 +60,9 @@ export type Log = {
6060 group : string | null ;
6161 target : string | null ;
6262 metadata : {
63- result : unknown ;
64- payload : unknown ;
63+ result : string ;
64+ payload : string ;
65+ error : { name : string ; message : string } ;
6566 } ;
6667} ;
6768
@@ -167,10 +168,29 @@ export class Engine extends DurableObject<Env> {
167168 } ;
168169 }
169170
170- async getStatus (
171- _accountId : number ,
172- _instanceId : string
173- ) : Promise < InstanceStatus > {
171+ readLogsFromEvent ( eventType : InstanceEvent ) : EngineLogs {
172+ const logs = [
173+ ...this . ctx . storage . sql . exec < {
174+ event : InstanceEvent ;
175+ groupKey : string | null ;
176+ target : string | null ;
177+ metadata : string ;
178+ } > (
179+ "SELECT event, groupKey, target, metadata FROM states WHERE event = ?" ,
180+ eventType
181+ ) ,
182+ ] ;
183+
184+ return {
185+ logs : logs . map ( ( log ) => ( {
186+ ...log ,
187+ metadata : JSON . parse ( log . metadata ) ,
188+ group : log . groupKey ,
189+ } ) ) ,
190+ } ;
191+ }
192+
193+ async getStatus ( ) : Promise < InstanceStatus > {
174194 if ( this . accountId === undefined ) {
175195 // Engine could have restarted, so we try to restore from its state
176196 const metadata =
@@ -353,6 +373,34 @@ export class Engine extends DurableObject<Env> {
353373 }
354374 }
355375
376+ async getOutputOrError ( isOutput : boolean ) : Promise < unknown > {
377+ const status = await this . getStatus ( ) ;
378+
379+ if ( isOutput ) {
380+ if ( status !== InstanceStatus . Complete ) {
381+ throw new Error (
382+ `Cannot retrieve output: Workflow instance is in status "${ instanceStatusName ( status ) } " but must be "complete" to have an output available`
383+ ) ;
384+ }
385+ const logs = this . readLogsFromEvent ( InstanceEvent . WORKFLOW_SUCCESS ) . logs ;
386+ return logs . at ( 0 ) ?. metadata . result ;
387+ } else {
388+ if ( status !== InstanceStatus . Errored ) {
389+ throw new Error (
390+ `Cannot retrieve error: Workflow instance is in status "${ instanceStatusName ( status ) } " but must be "errored" to have error information available`
391+ ) ;
392+ }
393+ const logs = this . readLogsFromEvent ( InstanceEvent . WORKFLOW_FAILURE ) . logs ;
394+ const log = logs . at ( 0 ) ;
395+ if ( ! log ?. metadata . error ) {
396+ throw new Error (
397+ "Cannot retrieve error: No workflow instance failure log found"
398+ ) ;
399+ }
400+ return log . metadata . error ;
401+ }
402+ }
403+
356404 async abort ( _reason : string ) {
357405 // TODO: Maybe don't actually kill but instead check a flag and return early if true
358406 }
@@ -497,7 +545,7 @@ export class Engine extends DurableObject<Env> {
497545 this . instanceId = instance . id ;
498546 this . workflowName = workflow . name ;
499547
500- const status = await this . getStatus ( accountId , instance . id ) ;
548+ const status = await this . getStatus ( ) ;
501549 if (
502550 [
503551 InstanceStatus . Errored , // TODO (WOR-85): Remove this once upgrade story is done
0 commit comments