<?xml version="1.0" encoding="UTF-8"?>
<scxml xmlns="http://www.w3.org/2005/07/scxml" xmlns:editor="http://vscxml.com/editor" version="1.0" datamodel="ecmascript" initial="idle" name="ErrorHandling">
  <datamodel>
    <data id="retryCount" expr="0"/>
    <data id="maxRetries" expr="3"/>
    <data id="lastError" expr="&apos;&apos;"/>
    <data id="result" expr="null"/>
  </datamodel>
  <state id="idle" editor:x="50" editor:y="50" editor:width="120" editor:height="80" editor:label="idle" editor:color="#ffffff" editor:zIndex="0">
    <onentry>
      <log label="Status" expr="'Ready. Send \&quot;process\&quot; to begin.'"/>
      <assign location="retryCount" expr="0"/>
      <assign location="lastError" expr="''"/>
    </onentry>
    <transition target="processing" event="process" editor:id="t_01KEFTD704DHT2AHBAHX7P23PW"/>
  </state>
  <state id="processing" editor:x="504" editor:y="119" editor:width="120" editor:height="80" editor:label="processing" editor:color="#ffffff" editor:zIndex="75">
    <onentry>
      <log label="Status" expr="'Processing... (attempt ' + (retryCount + 1) + '/' + maxRetries + ')'"/>
      <send event="operation_start" delay="500ms"/>
    </onentry>
    <transition target="success" event="success" editor:id="t_01KEFTD704FFYWJX9CP2282PE2">
      <assign location="result" expr="_event.data ? _event.data.result : 'completed'"/>
      <log label="Success" expr="'Operation succeeded: ' + result"/>
    </transition>
    <transition target="error_handler" event="failure" editor:id="t_01KEFTD704T2X4EXB8EJE1ZP15">
      <assign location="lastError" expr="_event.data ? _event.data.message : 'Unknown error'"/>
    </transition>
    <transition target="error_handler" event="error.*" editor:id="t_01KEFTD7040E1XHDA1WRXKVQKW">
      <assign location="lastError" expr="'System error: ' + _event.name"/>
    </transition>
    <transition target="error_handler" event="timeout" editor:id="t_01KEFTD7045HQ2VH5PZ703N34T">
      <assign location="lastError" expr="'Operation timed out'"/>
    </transition>
  </state>
  <state id="error_handler" editor:x="504" editor:y="325" editor:width="120" editor:height="80" editor:label="error_handler" editor:color="#ffffff" editor:zIndex="66">
    <onentry>
      <log label="Error" expr="lastError"/>
      <assign location="retryCount" expr="retryCount + 1"/>
    </onentry>
    <transition target="retry_wait" cond="retryCount &lt; maxRetries" editor:id="t_01KEFTD704VP55WR27DNZRHDPB">
      <log label="Retry" expr="'Will retry (' + retryCount + '/' + maxRetries + ')'"/>
    </transition>
    <transition target="failed" cond="retryCount &gt;= maxRetries" editor:id="t_01KEFTD704YSXGNEQJAH8GT73P">
      <log label="Failed" expr="'Max retries exceeded'"/>
    </transition>
  </state>
  <state id="retry_wait" editor:x="894" editor:y="325" editor:width="120" editor:height="80" editor:label="retry_wait" editor:color="#ffffff" editor:zIndex="47">
    <onentry>
      <log label="Status" expr="'Waiting before retry...'"/>
      <send event="retry" delay="1s"/>
    </onentry>
    <transition target="processing" event="retry" editor:id="t_01KEFTD7049VM2YNTY2HZ0H99P"/>
    <transition target="cancelled" event="cancel" editor:id="t_01KEFTD7048TXJK851HP9ENZWM"/>
  </state>
  <state id="success" editor:x="213" editor:y="138" editor:width="120" editor:height="80" editor:label="success" editor:color="#ffffff" editor:zIndex="83">
    <onentry>
      <log label="Complete" expr="'Operation completed successfully'"/>
    </onentry>
    <transition target="idle" event="reset" editor:id="t_01KEFTD704QVK2YE3CM116M5A9"/>
  </state>
  <state id="failed" editor:x="50" editor:y="325" editor:width="120" editor:height="80" editor:label="failed" editor:color="#ffffff" editor:zIndex="62">
    <onentry>
      <log label="Failed" expr="'Operation failed after ' + retryCount + ' attempts. Last error: ' + lastError"/>
    </onentry>
    <transition target="idle" event="reset" editor:id="t_01KEFTD704P4CPBPVY39Q1PBRA"/>
    <transition target="processing" event="force_retry" editor:id="t_01KEFTD7043FHGRF14VE47TE79">
      <assign location="retryCount" expr="0"/>
    </transition>
  </state>
  <state id="cancelled" editor:x="894" editor:y="50" editor:width="120" editor:height="80" editor:label="cancelled" editor:color="#ffffff" editor:zIndex="36">
    <onentry>
      <log label="Cancelled" expr="'Operation cancelled by user'"/>
    </onentry>
    <transition target="idle" event="reset" editor:id="t_01KEFTD705Y4EBXAA9V5RWVJV6"/>
  </state>
</scxml>