responseWrapper<T> method

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)) {
      print(payload);
      final List components = payload['components'];

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

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

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

    throw HttpException('${response.statusCode} : ${response.body}');
  }

  return response;
}