responseWrapper<T> method Null safety

Response responseWrapper<T>(
  1. Response response
)

Implementation

http.Response responseWrapper<T> (http.Response response) {
  if (response.statusCode == 400) {
    final dynamic payload = jsonDecode(response.body);

    if (Helper.hasKey('components', payload)) {
      final List<int> components = payload['components'];

      throw ApiException(
        code: response.statusCode,
        cause: payload['components'].length > 1
          ? 'Components at ${components.join(', ')} positions are invalid'
          : 'The component at position ${components.first} is invalid'
      );
    }

    if (Helper.hasKey('embeds', payload)) {
      final List<int> components = payload['embeds'];

      throw ApiException(
        code: response.statusCode,
        cause: payload['embeds'].length > 1
          ? 'Embeds at ${components.join(', ')} positions are invalid'
          : 'The embed at position ${components.first} is invalid'
      );
    }

    throw HttpException(
      code: response.statusCode,
      cause: response.body,
    );
  }

  return response;
}