| | |
| | | (()=>{class t{constructor(t={}){this.options={apiUrl:"",logToServer:!0,displayNotifications:!0,notificationDuration:5e3,retryEnabled:!0,maxRetries:3,...t},this.retryCount=0}async log(t,e={},r=null){console.error("API Error:",t,e);const o=this.getErrorType(t),n=this.getErrorMessage(t,o);switch(this.options.logToServer&&await this.logErrorToServer(o,n,e),o){case"network":case"server":if(this.options.retryEnabled&&this.retryCount<this.options.maxRetries&&r)return this.retryCount++,this.retryWithBackoff(r);break;case"auth":this.handleAuthError();break;case"rate_limit":return this.handleRateLimitError(r)}return this.options.displayNotifications&&this.displayErrorNotification(n,o,r),r&&this.options.retryEnabled||(this.retryCount=0),{success:!1,error:o,message:n,context:e}}getErrorType(t){if("AbortError"===t.name)return"timeout";if(!navigator.onLine)return"offline";if(t.response){const e=t.response.status;if(e>=400&&e<500)return 401===e||403===e?"auth":429===e?"rate_limit":"client";if(e>=500)return"server"}return"network"}getErrorMessage(t,e){const r={network:"We couldn't connect to the server. Please check your connection and try again.",timeout:"The request took too long to complete. Please try again.",offline:"You appear to be offline. Please check your internet connection.",auth:"Your session may have expired. Please log in again.",rate_limit:"You've made too many requests. Please wait a moment and try again.",server:"We're experiencing technical difficulties. Please try again later.",client:"Something went wrong with your request. Please try again.",unknown:"An unexpected error occurred. Please try again."};return t.response&&t.response.data&&t.response.data.message?t.response.data.message:t.message?t.message:r[e]||r.unknown}async logErrorToServer(t,e,r){try{if(!this.options.apiUrl)return;const o={...r,url:window.location.href,pathname:window.location.pathname,userAgent:navigator.userAgent,timestamp:(new Date).toISOString(),viewport:`${window.innerWidth}x${window.innerHeight}`,component:r.component||this.extractComponentFromStack(r.stack),method:r.method||this.extractMethodFromStack(r.stack),stack:r.stack||r.error?.stack,isLoggedIn:window.auth.isAuthenticated(),source:"frontend"},n=new FormData;n.append("error_type",t),n.append("message",e),n.append("context",JSON.stringify(o)),await fetch(`${this.options.apiUrl}errors/log`,{method:"POST",headers:{"X-WP-Nonce":window.auth.getNonce()},body:n})}catch(t){console.warn("Failed to log error to server",t)}}extractComponentFromStack(t){if(!t)return"Unknown";const e=t.match(/at\s+(\w+)\./);return e?e[1]:"Unknown"}extractMethodFromStack(t){if(!t)return null;const e=t.match(/at\s+\w+\.(\w+)\s+/);return e?e[1]:null}displayErrorNotification(t,e,r){if(window.jvbNotifications){const e=[];return r&&e.push({label:"Try Again",icon:"refresh",action:r}),void window.jvbNotifications.queuePopupNotification({type:"error",message:t,icon:"alert",priority:"high",displayDuration:this.options.notificationDuration,actions:e})}alert(t)}handleAuthError(){window.jvbSettings&&window.jvbSettings.loginUrl?window.location.href=window.jvbSettings.loginUrl:window.location.reload()}async handleRateLimitError(t){const e=2e3*(this.retryCount+1);if(await new Promise((t=>setTimeout(t,e))),t)return this.retryCount++,t()}async retryWithBackoff(t){const e=Math.min(1e3*Math.pow(2,this.retryCount),1e4);return await new Promise((t=>setTimeout(t,e))),t()}resetRetryCount(){this.retryCount=0}}document.addEventListener("DOMContentLoaded",(async function(){window.auth.subscribe((e=>{"auth-loaded"===e&&(window.jvbError=new t({api:jvbSettings.api,logToServer:!0,displayNotifications:!0,notificationDuration:5e3,retryEnabled:!0,maxRetries:3}))}))}))})(); |
| | | (()=>{class t{constructor(t={}){this.options={apiUrl:"",logToServer:!0,displayNotifications:!0,notificationDuration:5e3,retryEnabled:!0,maxRetries:3,...t},this.retryCount=0}async log(t,e={},r=null){console.error("API Error:",t,e);const o=this.getErrorType(t),n=this.getErrorMessage(t,o);switch(this.options.logToServer&&await this.logErrorToServer(o,n,e),o){case"network":case"server":if(this.options.retryEnabled&&this.retryCount<this.options.maxRetries&&r)return this.retryCount++,this.retryWithBackoff(r);break;case"auth":this.handleAuthError();break;case"rate_limit":return this.handleRateLimitError(r)}return this.options.displayNotifications&&this.displayErrorNotification(n,o,r),r&&this.options.retryEnabled||(this.retryCount=0),{success:!1,error:o,message:n,context:e}}getErrorType(t){if("AbortError"===t.name)return"timeout";if(!navigator.onLine)return"offline";if(t.response){const e=t.response.status;if(e>=400&&e<500)return 401===e||403===e?"auth":429===e?"rate_limit":"client";if(e>=500)return"server"}return"network"}getErrorMessage(t,e){const r={network:"We couldn't connect to the server. Please check your connection and try again.",timeout:"The request took too long to complete. Please try again.",offline:"You appear to be offline. Please check your internet connection.",auth:"Your session may have expired. Please log in again.",rate_limit:"You've made too many requests. Please wait a moment and try again.",server:"We're experiencing technical difficulties. Please try again later.",client:"Something went wrong with your request. Please try again.",unknown:"An unexpected error occurred. Please try again."};return t.response&&t.response.data&&t.response.data.message?t.response.data.message:t.message?t.message:r[e]||r.unknown}async logErrorToServer(t,e,r){try{if(!this.options.apiUrl)return;const o={...r,url:window.location.href,pathname:window.location.pathname,userAgent:navigator.userAgent,timestamp:(new Date).toISOString(),viewport:`${window.innerWidth}x${window.innerHeight}`,component:r.component||this.extractComponentFromStack(r.stack),method:r.method||this.extractMethodFromStack(r.stack),stack:r.stack||r.error?.stack,isLoggedIn:window.auth.isAuthenticated(),source:"frontend"},n=new FormData;n.append("error_type",t),n.append("message",e),n.append("context",JSON.stringify(o)),await fetch(`${this.options.apiUrl}errors/log`,{method:"POST",headers:{"X-WP-Nonce":window.auth.getNonce()},body:n})}catch(t){console.warn("Failed to log error to server",t)}}extractComponentFromStack(t){if(!t)return"Unknown";const e=t.match(/at\s+(\w+)\./);return e?e[1]:"Unknown"}extractMethodFromStack(t){if(!t)return null;const e=t.match(/at\s+\w+\.(\w+)\s+/);return e?e[1]:null}displayErrorNotification(t,e,r){if(window.jvbNotifications){const e=[];return r&&e.push({label:"Try Again",icon:"refresh",action:r}),void window.jvbNotifications.queuePopupNotification({type:"error",message:t,icon:"alert",priority:"high",displayDuration:this.options.notificationDuration,actions:e})}alert(t)}handleAuthError(){window.jvbSettings&&window.jvbSettings.loginUrl?window.location.href=window.jvbSettings.loginUrl:window.location.reload()}async handleRateLimitError(t){const e=2e3*(this.retryCount+1);if(await new Promise(t=>setTimeout(t,e)),t)return this.retryCount++,t()}async retryWithBackoff(t){const e=Math.min(1e3*Math.pow(2,this.retryCount),1e4);return await new Promise(t=>setTimeout(t,e)),t()}resetRetryCount(){this.retryCount=0}}document.addEventListener("DOMContentLoaded",async function(){window.auth.subscribe(e=>{"auth-loaded"===e&&(window.jvbError=new t({api:jvbSettings.api,logToServer:!0,displayNotifications:!0,notificationDuration:5e3,retryEnabled:!0,maxRetries:3}))})})})(); |