@@ -3,16 +3,12 @@ import { ref, watch } from "vue";
3
3
import { SagaHistory , SagaMessage } from "@/resources/SagaHistory" ;
4
4
import { useFetchFromServiceControl } from "@/composables/serviceServiceControlUrls" ;
5
5
import Message from "@/resources/Message" ;
6
- import { parse } from "lossless-json" ;
7
6
8
- const StandardKeys = [ "$type" , "Id" , "Originator" , "OriginalMessageId" ] ;
9
- export interface SagaMessageDataItem {
10
- key : string ;
11
- value : string ;
12
- }
13
7
export interface SagaMessageData {
14
8
message_id : string ;
15
- data : SagaMessageDataItem [ ] ;
9
+ data : string ;
10
+ type : "json" | "xml" ;
11
+ error : boolean ;
16
12
}
17
13
export const useSagaDiagramStore = defineStore ( "SagaDiagramStore" , ( ) => {
18
14
const sagaHistory = ref < SagaHistory | null > ( null ) ;
@@ -73,44 +69,68 @@ export const useSagaDiagramStore = defineStore("SagaDiagramStore", () => {
73
69
}
74
70
}
75
71
76
- function createEmptyMessageData ( message_id : string ) : SagaMessageData {
77
- return {
78
- message_id,
79
- data : [ ] ,
80
- } ;
81
- }
82
-
83
72
async function fetchSagaMessageData ( message : SagaMessage ) : Promise < SagaMessageData > {
84
73
const bodyUrl = ( message . body_url ?? formatUrl ( MessageBodyEndpoint , message . message_id ) ) . replace ( / ^ \/ / , "" ) ;
85
74
86
75
try {
87
76
const response = await useFetchFromServiceControl ( bodyUrl , { cache : "no-store" } ) ;
77
+
78
+ // Treat 404 as empty data, not as an error
79
+ if ( response . status === 404 ) {
80
+ return {
81
+ message_id : message . message_id ,
82
+ data : "" ,
83
+ type : "json" ,
84
+ error : false ,
85
+ } ;
86
+ }
87
+
88
+ // Handle other non-OK responses as errors
88
89
if ( ! response . ok ) {
89
- throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
90
+ error . value = `HTTP error! status: ${ response . status } ` ;
91
+ return {
92
+ message_id : message . message_id ,
93
+ data : "" ,
94
+ type : "json" ,
95
+ error : true ,
96
+ } ;
90
97
}
91
- const body = await response . json ( ) ;
98
+
99
+ const body = await response . text ( ) ;
92
100
93
101
if ( ! body ) {
94
- return createEmptyMessageData ( message . message_id ) ;
102
+ return {
103
+ message_id : message . message_id ,
104
+ data : "" ,
105
+ type : "json" ,
106
+ error : false ,
107
+ } ;
95
108
}
96
109
97
- let data : SagaMessageDataItem [ ] ;
98
- if ( typeof body === "string" && body . trim ( ) . startsWith ( "<?xml" ) ) {
99
- data = getXmlData ( body ) ;
110
+ // Determine the content type
111
+ if ( body . trim ( ) . startsWith ( "<?xml" ) ) {
112
+ return {
113
+ message_id : message . message_id ,
114
+ data : body ,
115
+ type : "xml" ,
116
+ error : false ,
117
+ } ;
100
118
} else {
101
- data = processJsonValues ( body ) ;
102
- }
103
- // Check if parsed data is empty
104
- if ( ! data || data . length === 0 ) {
105
- return createEmptyMessageData ( message . message_id ) ;
119
+ return {
120
+ message_id : message . message_id ,
121
+ data : body ,
122
+ type : "json" ,
123
+ error : false ,
124
+ } ;
106
125
}
126
+ } catch ( e ) {
127
+ error . value = e instanceof Error ? e . message : "Unknown error occurred" ;
107
128
return {
108
129
message_id : message . message_id ,
109
- data,
130
+ data : "" ,
131
+ type : "json" ,
132
+ error : true ,
110
133
} ;
111
- } catch ( e ) {
112
- error . value = e instanceof Error ? e . message : "Unknown error occurred" ;
113
- return createEmptyMessageData ( message . message_id ) ;
114
134
}
115
135
}
116
136
@@ -127,62 +147,6 @@ export const useSagaDiagramStore = defineStore("SagaDiagramStore", () => {
127
147
}
128
148
}
129
149
130
- function getXmlData ( xmlString : string ) : SagaMessageDataItem [ ] {
131
- try {
132
- const parser = new DOMParser ( ) ;
133
- const xmlDoc = parser . parseFromString ( xmlString , "application/xml" ) ;
134
-
135
- // Get the root element
136
- const rootElement = xmlDoc . documentElement ;
137
- if ( ! rootElement ) {
138
- return [ ] ;
139
- }
140
-
141
- // Handle both v5 and pre-v5 message formats
142
- const messageRoot = rootElement . nodeName === "Messages" ? Array . from ( rootElement . children ) [ 0 ] : rootElement ;
143
-
144
- if ( ! messageRoot ) {
145
- return [ ] ;
146
- }
147
-
148
- // Convert child elements to SagaMessageDataItems
149
- return Array . from ( messageRoot . children ) . map ( ( node ) => ( {
150
- key : node . nodeName ,
151
- value : node . textContent ?. trim ( ) || "" ,
152
- } ) ) ;
153
- } catch ( error ) {
154
- console . error ( "Error parsing message data:" , error ) ;
155
- return [ ] ;
156
- }
157
- }
158
-
159
- function processJsonValues ( jsonBody : string | Record < string , unknown > ) : SagaMessageDataItem [ ] {
160
- let parsedBody : Record < string , unknown > ;
161
- if ( typeof jsonBody === "string" ) {
162
- try {
163
- parsedBody = parse ( jsonBody ) as Record < string , unknown > ;
164
- } catch ( e ) {
165
- console . error ( "Error parsing JSON:" , e ) ;
166
- return [ ] ;
167
- }
168
- } else {
169
- parsedBody = jsonBody ;
170
- }
171
-
172
- const items : SagaMessageDataItem [ ] = [ ] ;
173
-
174
- for ( const key in parsedBody ) {
175
- if ( ! StandardKeys . includes ( key ) ) {
176
- items . push ( {
177
- key : key ,
178
- value : String ( parsedBody [ key ] ?? "" ) ,
179
- } ) ;
180
- }
181
- }
182
-
183
- return items ;
184
- }
185
-
186
150
function clearSagaHistory ( ) {
187
151
sagaHistory . value = null ;
188
152
sagaId . value = null ;
0 commit comments