@@ -5245,6 +5245,41 @@ export function createAdeRpcRequestHandler(args: {
52455245 throw new JsonRpcError ( JsonRpcErrorCode . methodNotFound , `Unsupported ADE action: ${ actionName } ` ) ;
52465246 } ;
52475247
5248+ const failedActionPayloadMessage = ( value : unknown ) : string | null => {
5249+ if ( ! isRecord ( value ) ) return null ;
5250+ const failed = value . ok === false || value . success === false ;
5251+ if ( ! failed ) return null ;
5252+ const error = value . error ;
5253+ if ( typeof error === "string" && error . trim ( ) ) return error . trim ( ) ;
5254+ if ( isRecord ( error ) && typeof error . message === "string" && error . message . trim ( ) ) {
5255+ return error . message . trim ( ) ;
5256+ }
5257+ return "Action returned a failed result." ;
5258+ } ;
5259+
5260+ const actionResultFailureMessage = ( value : unknown ) : string | null => {
5261+ if ( ! isRecord ( value ) ) return null ;
5262+ return failedActionPayloadMessage ( value ) ?? failedActionPayloadMessage ( value . result ) ;
5263+ } ;
5264+
5265+ const finishActionAudit = (
5266+ actionName : string ,
5267+ operationId : string ,
5268+ status : "succeeded" | "failed" ,
5269+ metadataPatch : Record < string , unknown > ,
5270+ ) : void => {
5271+ try {
5272+ runtime . operationService . finish ( { operationId, status, metadataPatch } ) ;
5273+ } catch ( error ) {
5274+ runtime . logger . warn ( "ade_rpc.action_audit_finish_failed" , {
5275+ actionName,
5276+ operationId,
5277+ status,
5278+ error : error instanceof Error ? error . message : String ( error ) ,
5279+ } ) ;
5280+ }
5281+ } ;
5282+
52485283 const callActionWithAudit = async (
52495284 actionName : string ,
52505285 actionArgs : Record < string , unknown > ,
@@ -5261,21 +5296,21 @@ export function createAdeRpcRequestHandler(args: {
52615296 } ) ;
52625297 try {
52635298 const result = await callAction ( actionName , actionArgs ) ;
5264- runtime . operationService . finish ( {
5265- operationId : operation . operationId ,
5266- status : "succeeded" ,
5267- metadataPatch : { resultStatus : "success" } ,
5268- } ) ;
5299+ const failureMessage = actionResultFailureMessage ( result ) ;
5300+ if ( failureMessage ) {
5301+ finishActionAudit ( actionName , operation . operationId , "failed" , {
5302+ resultStatus : "error" ,
5303+ error : failureMessage ,
5304+ } ) ;
5305+ return result ;
5306+ }
5307+ finishActionAudit ( actionName , operation . operationId , "succeeded" , { resultStatus : "success" } ) ;
52695308 return result ;
52705309 } catch ( error ) {
52715310 const message = error instanceof Error ? error . message : String ( error ) ;
5272- runtime . operationService . finish ( {
5273- operationId : operation . operationId ,
5274- status : "failed" ,
5275- metadataPatch : {
5276- resultStatus : "error" ,
5277- error : message ,
5278- } ,
5311+ finishActionAudit ( actionName , operation . operationId , "failed" , {
5312+ resultStatus : "error" ,
5313+ error : message ,
52795314 } ) ;
52805315 throw error ;
52815316 }
0 commit comments