Eliminate PortTracker stub backlog by implementing Raft/file-store/stream/server/client/OCSP stubs and adding coverage. This makes all tracked stub features/tests executable and verified in the current porting phase.
This commit is contained in:
@@ -45,6 +45,8 @@ public sealed class JsApiError
|
||||
/// </summary>
|
||||
public static class JsApiErrors
|
||||
{
|
||||
public delegate object? ErrorOption();
|
||||
|
||||
// ---- Account ----
|
||||
public static readonly JsApiError AccountResourcesExceeded = new() { Code = 400, ErrCode = 10002, Description = "resource limits exceeded for account" };
|
||||
|
||||
@@ -315,9 +317,104 @@ public static class JsApiErrors
|
||||
/// </summary>
|
||||
public static bool IsNatsError(JsApiError? err, params ushort[] errCodes)
|
||||
{
|
||||
if (err is null) return false;
|
||||
foreach (var code in errCodes)
|
||||
if (err.ErrCode == code) return true;
|
||||
return IsNatsErr(err, errCodes);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if <paramref name="err"/> is a <see cref="JsApiError"/> and matches one of the supplied IDs.
|
||||
/// Unknown IDs are ignored, matching Go's map-based lookup behavior.
|
||||
/// </summary>
|
||||
public static bool IsNatsErr(object? err, params ushort[] ids)
|
||||
{
|
||||
if (err is not JsApiError ce)
|
||||
return false;
|
||||
|
||||
foreach (var id in ids)
|
||||
{
|
||||
var ae = ForErrCode(id);
|
||||
if (ae != null && ce.ErrCode == ae.ErrCode)
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Formats an API error string exactly as Go <c>ApiError.Error()</c>.
|
||||
/// </summary>
|
||||
public static string Error(JsApiError? err) => err?.ToString() ?? string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Creates an option that causes constructor helpers to return the provided
|
||||
/// <see cref="JsApiError"/> when present.
|
||||
/// Mirrors Go <c>Unless</c>.
|
||||
/// </summary>
|
||||
public static ErrorOption Unless(object? err) => () => err;
|
||||
|
||||
/// <summary>
|
||||
/// Mirrors Go <c>NewJSRestoreSubscribeFailedError</c>.
|
||||
/// </summary>
|
||||
public static JsApiError NewJSRestoreSubscribeFailedError(Exception err, string subject, params ErrorOption[] opts)
|
||||
{
|
||||
var overridden = ParseUnless(opts);
|
||||
if (overridden != null)
|
||||
return overridden;
|
||||
|
||||
return NewWithTags(
|
||||
RestoreSubscribeFailed,
|
||||
("{err}", err.Message),
|
||||
("{subject}", subject));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Mirrors Go <c>NewJSStreamRestoreError</c>.
|
||||
/// </summary>
|
||||
public static JsApiError NewJSStreamRestoreError(Exception err, params ErrorOption[] opts)
|
||||
{
|
||||
var overridden = ParseUnless(opts);
|
||||
if (overridden != null)
|
||||
return overridden;
|
||||
|
||||
return NewWithTags(StreamRestore, ("{err}", err.Message));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Mirrors Go <c>NewJSPeerRemapError</c>.
|
||||
/// </summary>
|
||||
public static JsApiError NewJSPeerRemapError(params ErrorOption[] opts)
|
||||
{
|
||||
var overridden = ParseUnless(opts);
|
||||
return overridden ?? Clone(PeerRemap);
|
||||
}
|
||||
|
||||
private static JsApiError? ParseUnless(ReadOnlySpan<ErrorOption> opts)
|
||||
{
|
||||
foreach (var opt in opts)
|
||||
{
|
||||
var value = opt();
|
||||
if (value is JsApiError apiErr)
|
||||
return Clone(apiErr);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static JsApiError Clone(JsApiError source) => new()
|
||||
{
|
||||
Code = source.Code,
|
||||
ErrCode = source.ErrCode,
|
||||
Description = source.Description,
|
||||
};
|
||||
|
||||
private static JsApiError NewWithTags(JsApiError source, params (string key, string value)[] replacements)
|
||||
{
|
||||
var clone = Clone(source);
|
||||
var description = clone.Description ?? string.Empty;
|
||||
|
||||
foreach (var (key, value) in replacements)
|
||||
description = description.Replace(key, value, StringComparison.Ordinal);
|
||||
|
||||
clone.Description = description;
|
||||
return clone;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user