OpenRouterManagementApiException.fromResponse constructor

OpenRouterManagementApiException.fromResponse(
  1. Response response
)

Implementation

factory OpenRouterManagementApiException.fromResponse(
  http.Response response,
) {
  Map<String, dynamic>? json;

  if (response.body.isNotEmpty) {
    try {
      final decoded = jsonDecode(response.body);
      if (decoded is Map<String, dynamic>) {
        json = decoded;
      }
    } catch (_) {
      // Leave json null when the API does not respond with JSON.
    }
  }

  final message =
      _asString(
        json?['error'] is Map<String, dynamic>
            ? (json!['error'] as Map<String, dynamic>)['message']
            : json?['message'],
      ) ??
      'OpenRouter Management API request failed with status ${response.statusCode}.';

  return OpenRouterManagementApiException(
    statusCode: response.statusCode,
    message: message,
    responseBody: response.body,
    responseJson: json,
  );
}