using Nexus.Api.Models; using Nexus.Api.Services; namespace Nexus.Api.Controllers; internal static class OpenClawContentReadEndpoint { public static async Task ExecuteAsync(Func> action) { try { return await action(); } catch (OpenClawAgentConfigurationValidationException exception) { return Results.ValidationProblem( new Dictionary { [exception.Field] = [exception.Message] }); } catch (OpenClawAgentConfigurationUnavailableException exception) { var status = exception.State switch { "forbidden" or "management_disabled" => StatusCodes.Status403Forbidden, "disconnected" => StatusCodes.Status503ServiceUnavailable, _ => StatusCodes.Status409Conflict }; return Results.Json( new OpenClawAgentConfigurationErrorDto( exception.State, exception.Message, exception.Method, exception.RequiredScope), statusCode: status); } catch (OpenClawAgentConfigurationVerificationException) { return Results.Json( new OpenClawAgentConfigurationErrorDto( "verification_failed", "OpenClaw-Antwort konnte nicht sicher verifiziert werden."), statusCode: StatusCodes.Status502BadGateway); } catch (OpenClawGatewayRpcException exception) { var code = exception.Code.ToUpperInvariant(); var status = code switch { "FORBIDDEN" or "AUTH_SCOPE_MISMATCH" => StatusCodes.Status403Forbidden, "METHOD_UNAVAILABLE" or "METHOD_NOT_FOUND" or "NOT_IMPLEMENTED" => StatusCodes.Status409Conflict, "GATEWAY_DISCONNECTED" or "UNAVAILABLE" => StatusCodes.Status503ServiceUnavailable, "GATEWAY_TIMEOUT" or "TIMEOUT" => StatusCodes.Status504GatewayTimeout, _ => StatusCodes.Status502BadGateway }; return Results.Json( new OpenClawAgentConfigurationErrorDto( code.ToLowerInvariant(), status switch { StatusCodes.Status403Forbidden => "OpenClaw hat Nexus nicht die erforderliche Leseberechtigung gewährt.", StatusCodes.Status409Conflict => "Die verbundene OpenClaw-Version unterstützt diese Leseoperation nicht.", StatusCodes.Status503ServiceUnavailable => "OpenClaw Gateway ist nicht verfügbar.", StatusCodes.Status504GatewayTimeout => "OpenClaw hat nicht rechtzeitig geantwortet.", _ => "OpenClaw-Leseoperation ist fehlgeschlagen." }), statusCode: status); } } }