handleException method

GenkitException handleException(
  1. Object e,
  2. StackTrace stack
)

Implementation

GenkitException handleException(Object e, StackTrace stack) {
  if (e is GenkitException) return e;

  int? httpStatus;
  String? message;

  try {
    if ((e as dynamic).status != null) {
      httpStatus = (e as dynamic).status as int?;
    } else if ((e as dynamic).code != null) {
      httpStatus = (e as dynamic).code as int?;
    }
    if ((e as dynamic).message != null) {
      message = (e as dynamic).message as String?;
    }
  } catch (_) {}

  if (httpStatus != null) {
    return GenkitException(
      message ?? 'Google AI API Error: $httpStatus',
      status: StatusCodes.fromHttpStatus(httpStatus),
      underlyingException: e,
      stackTrace: stack,
    );
  }

  return GenkitException(
    'Google AI Error: $e',
    status: StatusCodes.INTERNAL,
    underlyingException: e,
    stackTrace: stack,
  );
}