Error.cpp 2.9 KB

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