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