TsgcWSCircuitBreakerMethods › RecordFailure

RecordFailure Method

Records a failure against a key; applies Classification, updates counters and may open the circuit.

Overloads

Overload 1

Syntax

procedure RecordFailure(const aKey: string; const aException: string = '');

Parameters

NameTypeDescription
aKeyconst stringCircuit key the failure belongs to — usually the hostname of the upstream service.
aExceptionconst stringOptional exception text in the form ClassName: Message, passed to Classification to decide whether to record or ignore. When empty the failure is always recorded.

Remarks

Runs aException through the Classification rules first: matches in IgnoreExceptions are dropped (neither success nor failure), everything else increments the rolling-window failure counter and Metrics.TotalFailures / Metrics.TotalCalls. The OnFailureRecorded event fires for every recorded failure. EvaluateTransition is then called, which may move Closed -> Open when Thresholds.FailureCount / FailureRatePercent is reached, or HalfOpen -> Open when a trial call fails. Call RecordFailure explicitly only when you manage the call yourself (not through Execute); the HTTP API client integration already records failures on any raised exception.

Example

try
  vResponse := MyHTTPClient.Get(vURL);
  sgcWSCircuitBreaker1.RecordSuccess(vHost);
except
  on E: Exception do
  begin
    sgcWSCircuitBreaker1.RecordFailure(vHost,
      Format('%s: %s', [E.ClassName, E.Message]));
    raise;
  end;
end;

Overload 2

Syntax

procedure RecordFailure(const aException: string = '');

Parameters

NameTypeDescription
aExceptionconst stringOptional exception text in the form ClassName: Message, passed to Classification to decide whether to record or ignore.

Remarks

Shortcut that records a failure against DefaultKey. Use when a single breaker protects exactly one logical dependency. Set DefaultKey to a meaningful label before calling so Metrics and OnFailureRecorded report a readable key.

Example

sgcWSCircuitBreaker1.DefaultKey := 'payment-gateway';
try
  ChargeCustomer;
  sgcWSCircuitBreaker1.RecordSuccess;
except
  on E: Exception do
  begin
    sgcWSCircuitBreaker1.RecordFailure(
      Format('%s: %s', [E.ClassName, E.Message]));
    raise;
  end;
end;

Back to Methods