| @@ -1,5 +1,5 @@ | ||
| 1 | -import { MessageType, PluginMessages } from '../iframe/integratedMessages'; | |
| 1 | +import { MessageType, PluginMessages } from './integratedMessages'; | |
| 2 | 2 | import { |
| 3 | 3 | fetchDisableInternalTracking, |
| 4 | 4 | trackConsent, |
| 5 | 5 | disableInternalTracking, |
| @@ -5,8 +5,11 @@ | ||
| 5 | 5 | disableInternalTracking, |
| 6 | 6 | getBusinessUnitId, |
| 7 | 7 | setBusinessUnitId, |
| 8 | 8 | skipReview, |
| 9 | + refreshProxyMappingsCache, | |
| 10 | + fetchProxyMappingsEnabled, | |
| 11 | + toggleProxyMappingsEnabled, | |
| 9 | 12 | } from '../api/wordpressApiClient'; |
| 10 | 13 | import { removeQueryParamFromLocation } from '../utils/queryParams'; |
| 11 | 14 | import { startActivation, startInstall } from '../utils/contentEmbedInstaller'; |
| 12 | 15 | |
| @@ -11,8 +14,33 @@ | ||
| 11 | 14 | import { startActivation, startInstall } from '../utils/contentEmbedInstaller'; |
| 12 | 15 | |
| 13 | 16 | export type Message = { key: MessageType; payload?: any }; |
| 14 | 17 | |
| 18 | +/* | |
| 19 | + * We cannot postMessage error objects. We will run into serialization errors | |
| 20 | + * Extract some properties we care about from the errors and create an error object from these | |
| 21 | + */ | |
| 22 | +function createSafeErrorPayload( | |
| 23 | + error: any, | |
| 24 | + defaultMessage: string = 'An error occurred' | |
| 25 | +) { | |
| 26 | + const safePayload: any = { | |
| 27 | + status: (error && error.status) || 500, | |
| 28 | + statusText: (error && error.statusText) || 'Error', | |
| 29 | + message: | |
| 30 | + (error && error.responseJSON && error.responseJSON.message) || | |
| 31 | + (error && error.message) || | |
| 32 | + defaultMessage, | |
| 33 | + code: error && error.responseJSON && error.responseJSON.code, | |
| 34 | + }; | |
| 35 | + | |
| 36 | + if (error && error.responseJSON && error.responseJSON.data) { | |
| 37 | + safePayload.data = error.responseJSON.data; | |
| 38 | + } | |
| 39 | + | |
| 40 | + return safePayload; | |
| 41 | +} | |
| 42 | + | |
| 15 | 43 | const messageMapper: Map<MessageType, Function> = new Map([ |
| 16 | 44 | [ |
| 17 | 45 | PluginMessages.TrackConsent, |
| 18 | 46 | (message: Message) => { |
| @@ -28,12 +56,13 @@ | ||
| 28 | 56 | key: PluginMessages.InternalTrackingFetchResponse, |
| 29 | 57 | payload: message.payload, |
| 30 | 58 | }); |
| 31 | 59 | }) |
| 32 | - .catch(payload => { | |
| 60 | + .catch(error => { | |
| 61 | + // Extract only serializable properties from error. You cannot postMessage raw error obj with prototype methods | |
| 33 | 62 | embedder.postMessage({ |
| 34 | 63 | key: PluginMessages.InternalTrackingChangeError, |
| 35 | - payload, | |
| 64 | + payload: createSafeErrorPayload(error), | |
| 36 | 65 | }); |
| 37 | 66 | }); |
| 38 | 67 | }, |
| 39 | 68 | ], |
| @@ -46,12 +75,12 @@ | ||
| 46 | 75 | key: PluginMessages.InternalTrackingFetchResponse, |
| 47 | 76 | payload, |
| 48 | 77 | }); |
| 49 | 78 | }) |
| 50 | - .catch(payload => { | |
| 79 | + .catch(error => { | |
| 51 | 80 | embedder.postMessage({ |
| 52 | 81 | key: PluginMessages.InternalTrackingFetchError, |
| 53 | - payload, | |
| 82 | + payload: createSafeErrorPayload(error, 'Fetch error occurred'), | |
| 54 | 83 | }); |
| 55 | 84 | }); |
| 56 | 85 | }, |
| 57 | 86 | ], |
| @@ -64,12 +93,12 @@ | ||
| 64 | 93 | key: PluginMessages.BusinessUnitFetchResponse, |
| 65 | 94 | payload, |
| 66 | 95 | }); |
| 67 | 96 | }) |
| 68 | - .catch(payload => { | |
| 97 | + .catch(error => { | |
| 69 | 98 | embedder.postMessage({ |
| 70 | 99 | key: PluginMessages.BusinessUnitFetchError, |
| 71 | - payload, | |
| 100 | + payload: createSafeErrorPayload(error, 'Business unit fetch error'), | |
| 72 | 101 | }); |
| 73 | 102 | }); |
| 74 | 103 | }, |
| 75 | 104 | ], |
| @@ -82,12 +111,15 @@ | ||
| 82 | 111 | key: PluginMessages.BusinessUnitFetchResponse, |
| 83 | 112 | payload, |
| 84 | 113 | }); |
| 85 | 114 | }) |
| 86 | - .catch(payload => { | |
| 115 | + .catch(error => { | |
| 87 | 116 | embedder.postMessage({ |
| 88 | 117 | key: PluginMessages.BusinessUnitChangeError, |
| 89 | - payload, | |
| 118 | + payload: createSafeErrorPayload( | |
| 119 | + error, | |
| 120 | + 'Business unit change error' | |
| 121 | + ), | |
| 90 | 122 | }); |
| 91 | 123 | }); |
| 92 | 124 | }, |
| 93 | 125 | ], |
| @@ -100,12 +132,12 @@ | ||
| 100 | 132 | key: PluginMessages.SkipReviewResponse, |
| 101 | 133 | payload, |
| 102 | 134 | }); |
| 103 | 135 | }) |
| 104 | - .catch(payload => { | |
| 136 | + .catch(error => { | |
| 105 | 137 | embedder.postMessage({ |
| 106 | 138 | key: PluginMessages.SkipReviewError, |
| 107 | - payload, | |
| 139 | + payload: createSafeErrorPayload(error, 'Skip review error'), | |
| 108 | 140 | }); |
| 109 | 141 | }); |
| 110 | 142 | }, |
| 111 | 143 | ], |
| @@ -124,12 +156,15 @@ | ||
| 124 | 156 | key: PluginMessages.ContentEmbedInstallResponse, |
| 125 | 157 | payload: payload, |
| 126 | 158 | }); |
| 127 | 159 | }) |
| 128 | - .catch(payload => { | |
| 160 | + .catch(error => { | |
| 129 | 161 | embedder.postMessage({ |
| 130 | 162 | key: PluginMessages.ContentEmbedInstallError, |
| 131 | - payload, | |
| 163 | + payload: createSafeErrorPayload( | |
| 164 | + error, | |
| 165 | + 'Content embed install error' | |
| 166 | + ), | |
| 132 | 167 | }); |
| 133 | 168 | }); |
| 134 | 169 | }, |
| 135 | 170 | ], |
| @@ -142,12 +177,78 @@ | ||
| 142 | 177 | key: PluginMessages.ContentEmbedActivationResponse, |
| 143 | 178 | payload: payload, |
| 144 | 179 | }); |
| 145 | 180 | }) |
| 146 | - .catch(payload => { | |
| 181 | + .catch(error => { | |
| 147 | 182 | embedder.postMessage({ |
| 148 | 183 | key: PluginMessages.ContentEmbedActivationError, |
| 184 | + payload: createSafeErrorPayload( | |
| 185 | + error, | |
| 186 | + 'Content embed activation error' | |
| 187 | + ), | |
| 188 | + }); | |
| 189 | + }); | |
| 190 | + }, | |
| 191 | + ], | |
| 192 | + [ | |
| 193 | + PluginMessages.RefreshProxyMappingsRequest, | |
| 194 | + (__message: Message, embedder: any) => { | |
| 195 | + refreshProxyMappingsCache() | |
| 196 | + .then(() => { | |
| 197 | + embedder.postMessage({ | |
| 198 | + key: PluginMessages.RefreshProxyMappingsResponse, | |
| 199 | + payload: {}, | |
| 200 | + }); | |
| 201 | + }) | |
| 202 | + .catch(error => { | |
| 203 | + embedder.postMessage({ | |
| 204 | + key: PluginMessages.RefreshProxyMappingsError, | |
| 205 | + payload: createSafeErrorPayload( | |
| 206 | + error, | |
| 207 | + 'Refresh proxy mappings error' | |
| 208 | + ), | |
| 209 | + }); | |
| 210 | + }); | |
| 211 | + }, | |
| 212 | + ], | |
| 213 | + [ | |
| 214 | + PluginMessages.ProxyMappingsEnabledRequest, | |
| 215 | + (__message: Message, embedder: any) => { | |
| 216 | + fetchProxyMappingsEnabled() | |
| 217 | + .then(payload => { | |
| 218 | + embedder.postMessage({ | |
| 219 | + key: PluginMessages.ProxyMappingsEnabledResponse, | |
| 149 | 220 | payload, |
| 221 | + }); | |
| 222 | + }) | |
| 223 | + .catch(error => { | |
| 224 | + embedder.postMessage({ | |
| 225 | + key: PluginMessages.ProxyMappingsEnabledError, | |
| 226 | + payload: createSafeErrorPayload( | |
| 227 | + error, | |
| 228 | + 'Proxy mappings enabled fetch error' | |
| 229 | + ), | |
| 230 | + }); | |
| 231 | + }); | |
| 232 | + }, | |
| 233 | + ], | |
| 234 | + [ | |
| 235 | + PluginMessages.ProxyMappingsEnabledChangeRequest, | |
| 236 | + ({ payload }: Message, embedder: any) => { | |
| 237 | + toggleProxyMappingsEnabled(payload) | |
| 238 | + .then(() => { | |
| 239 | + embedder.postMessage({ | |
| 240 | + key: PluginMessages.ProxyMappingsEnabledResponse, | |
| 241 | + payload, | |
| 242 | + }); | |
| 243 | + }) | |
| 244 | + .catch(error => { | |
| 245 | + embedder.postMessage({ | |
| 246 | + key: PluginMessages.ProxyMappingsEnabledChangeError, | |
| 247 | + payload: createSafeErrorPayload( | |
| 248 | + error, | |
| 249 | + 'Proxy mappings enabled change error' | |
| 250 | + ), | |
| 150 | 251 | }); |
| 151 | 252 | }); |
| 152 | 253 | }, |
| 153 | 254 | ], |