createThrowMissingArgument function

Expression? createThrowMissingArgument(
  1. BaseParameterAnnotation annotation,
  2. ServerParam param, {
  3. String? location,
})

Implementation

Expression? createThrowMissingArgument(
  BaseParameterAnnotation annotation,
  ServerParam param, {
  String? location,
}) {
  final at = '@${annotation.type.name}';
  final atLocation = switch (location) {
    null => at,
    final location => '$at#$location',
  };
  final throwException = createMissingArgumentException(
    key: annotation.name ?? param.name,
    location: atLocation,
  ).thrown.parenthesized;

  if (annotation case HasPipe(:final pipe?)) {
    if (param.hasDefaultValue) {
      return null;
    }

    if (pipe.convertFrom.isNullable) {
      return null;
    }

    return throwException;
  }

  if (param.type.isNullable) {
    return null;
  }

  if (param.hasDefaultValue) {
    return null;
  }

  return throwException;
}