sendErrorPayload method

Future<Exception> sendErrorPayload(
  1. String url,
  2. Payload payload
)

Implementation

Future<Exception> sendErrorPayload(String url, Payload payload) async {
  final request = http.Request("POST", Uri.parse(url));
  request.headers.addAll({
    'Content-Type': 'application/json; charset=UTF-8',
  });
  if (referer != "") {
    request.headers.addAll({'referer': '$referer/', 'origin': referer});
  }
  String body = jsonEncode(payload.toJson());
  request.body = body;
  final response = await _client.send(request);
  final responseBody = await response.stream.bytesToString();

  if (response.statusCode >= 200 && response.statusCode < 300) {
    return Exception(payload.message);
  } else if (response.statusCode == 429) {
    // HTTP 429 responses are returned by Stackdriver when API quota
    // is exceeded. We should not try to reject these as unhandled errors
    // or we may cause an infinite loop with 'reportUncaughtExceptions'.
    return Exception('quota or rate limiting error on StackDriver report');
  } else {
    var condition = '${response.statusCode} http response';
    return Exception('$condition on StackDriver report\n$responseBody');
  }
}