| 147 | } |
| 148 | |
| 149 | public WebDriverException decode(Map<String, Object> response) { |
| 150 | if (!(response.get("value") instanceof Map)) { |
| 151 | throw new InvalidResponseException("missing \"value\" field", response); |
| 152 | } |
| 153 | |
| 154 | Map<?, ?> value = (Map<?, ?>) response.get("value"); |
| 155 | |
| 156 | Object error = requireNonNullElse(value.get("error"), ""); |
| 157 | Object message = requireNonNullElseGet(value.get("message"), response::toString); |
| 158 | |
| 159 | if (!(error instanceof String)) { |
| 160 | throw new InvalidResponseException("\"error\" field must be a string", response); |
| 161 | } |
| 162 | if (!(message instanceof String)) { |
| 163 | throw new InvalidResponseException("\"message\" field must be a string", response); |
| 164 | } |
| 165 | |
| 166 | Optional<W3CError<? extends WebDriverException>> w3CError = |
| 167 | ERRORS.stream().filter(err -> error.equals(err.w3cErrorString)).findFirst(); |
| 168 | if (w3CError.isPresent()) { |
| 169 | return w3CError.get().exceptionConstructor.apply((String) message); |
| 170 | } |
| 171 | String extendedMessage = String.format("%s (error code: \"%s\")", message, error); |
| 172 | return DEFAULT_ERROR.exceptionConstructor.apply(extendedMessage); |
| 173 | } |
| 174 | |
| 175 | private W3CError<? extends WebDriverException> fromThrowable(Throwable throwable) { |
| 176 | return ERRORS.stream() |