getExceptionFromW3cResponse function

WebDriverException getExceptionFromW3cResponse({
  1. int? httpStatusCode,
  2. String? httpReasonPhrase,
  3. Object? jsonResp,
})

Temporary method to emulate the original w3c exception parsing logic.

Implementation

WebDriverException getExceptionFromW3cResponse({
  int? httpStatusCode,
  String? httpReasonPhrase,
  Object? jsonResp,
}) {
  if (jsonResp is Map && jsonResp.keys.contains('value')) {
    final value = jsonResp['value'] as Map<String, Object?>;

    return switch (value['error']) {
      'invalid argument' => InvalidArgumentException(
          httpStatusCode,
          value['message'] as String?,
        ),
      'no such element' => NoSuchElementException(
          httpStatusCode,
          value['message'] as String?,
        ),
      _ => WebDriverException(httpStatusCode, value['message'] as String?),
    };
  }

  return InvalidResponseException(httpStatusCode, jsonResp.toString());
}