Error.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Vector.h>
  7. #include <LibWeb/WebDriver/Error.h>
  8. namespace Web::WebDriver {
  9. struct ErrorCodeData {
  10. ErrorCode error_code;
  11. unsigned http_status;
  12. DeprecatedString json_error_code;
  13. };
  14. // https://w3c.github.io/webdriver/#dfn-error-code
  15. static Vector<ErrorCodeData> const s_error_code_data = {
  16. { ErrorCode::ElementClickIntercepted, 400, "element click intercepted" },
  17. { ErrorCode::ElementNotInteractable, 400, "element not interactable" },
  18. { ErrorCode::InsecureCertificate, 400, "insecure certificate" },
  19. { ErrorCode::InvalidArgument, 400, "invalid argument" },
  20. { ErrorCode::InvalidCookieDomain, 400, "invalid cookie domain" },
  21. { ErrorCode::InvalidElementState, 400, "invalid element state" },
  22. { ErrorCode::InvalidSelector, 400, "invalid selector" },
  23. { ErrorCode::InvalidSessionId, 404, "invalid session id" },
  24. { ErrorCode::JavascriptError, 500, "javascript error" },
  25. { ErrorCode::MoveTargetOutOfBounds, 500, "move target out of bounds" },
  26. { ErrorCode::NoSuchAlert, 404, "no such alert" },
  27. { ErrorCode::NoSuchCookie, 404, "no such cookie" },
  28. { ErrorCode::NoSuchElement, 404, "no such element" },
  29. { ErrorCode::NoSuchFrame, 404, "no such frame" },
  30. { ErrorCode::NoSuchWindow, 404, "no such window" },
  31. { ErrorCode::NoSuchShadowRoot, 404, "no such shadow root" },
  32. { ErrorCode::ScriptTimeoutError, 500, "script timeout" },
  33. { ErrorCode::SessionNotCreated, 500, "session not created" },
  34. { ErrorCode::StaleElementReference, 404, "stale element reference" },
  35. { ErrorCode::DetachedShadowRoot, 404, "detached shadow root" },
  36. { ErrorCode::Timeout, 500, "timeout" },
  37. { ErrorCode::UnableToSetCookie, 500, "unable to set cookie" },
  38. { ErrorCode::UnableToCaptureScreen, 500, "unable to capture screen" },
  39. { ErrorCode::UnexpectedAlertOpen, 500, "unexpected alert open" },
  40. { ErrorCode::UnknownCommand, 404, "unknown command" },
  41. { ErrorCode::UnknownError, 500, "unknown error" },
  42. { ErrorCode::UnknownMethod, 405, "unknown method" },
  43. { ErrorCode::UnsupportedOperation, 500, "unsupported operation" },
  44. };
  45. Error Error::from_code(ErrorCode code, DeprecatedString message, Optional<JsonValue> data)
  46. {
  47. auto const& error_code_data = s_error_code_data[to_underlying(code)];
  48. return {
  49. .http_status = error_code_data.http_status,
  50. .error = error_code_data.json_error_code,
  51. .message = move(message),
  52. .data = move(data)
  53. };
  54. }
  55. }