1717
1818package org .openqa .selenium .remote ;
1919
20- import com .google .common .base .Throwables ;
21- import com .google .common .collect .ImmutableList ;
22- import com .google .common .collect .ImmutableMap ;
20+ import java .io .PrintWriter ;
21+ import java .io .StringWriter ;
2322import java .lang .reflect .Constructor ;
2423import java .util .List ;
2524import java .util .Map ;
@@ -61,38 +60,35 @@ public class ErrorCodec {
6160 // there is no guarantee a Set will keep the order (and we have no .equals / .hashCode
6261 // implementation too).
6362 private static final List <W3CError > ERRORS =
64- ImmutableList .<W3CError >builder ()
65- .add (new W3CError ("script timeout" , ScriptTimeoutException .class , 500 ))
66- .add (new W3CError ("detached shadow root" , DetachedShadowRootException .class , 404 ))
67- .add (
68- new W3CError (
69- "element click intercepted" , ElementClickInterceptedException .class , 400 ))
70- .add (new W3CError ("element not interactable" , ElementNotInteractableException .class , 400 ))
71- .add (new W3CError ("invalid argument" , InvalidArgumentException .class , 400 ))
72- .add (new W3CError ("invalid cookie domain" , InvalidCookieDomainException .class , 400 ))
73- .add (new W3CError ("invalid element state" , InvalidElementStateException .class , 400 ))
74- .add (new W3CError ("invalid selector" , InvalidSelectorException .class , 400 ))
75- .add (new W3CError ("invalid session id" , NoSuchSessionException .class , 404 ))
76- .add (new W3CError ("insecure certificate" , InsecureCertificateException .class , 400 ))
77- .add (new W3CError ("javascript error" , JavascriptException .class , 500 ))
78- .add (new W3CError ("move target out of bounds" , MoveTargetOutOfBoundsException .class , 500 ))
79- .add (new W3CError ("no such alert" , NoAlertPresentException .class , 404 ))
80- .add (new W3CError ("no such cookie" , NoSuchCookieException .class , 404 ))
81- .add (new W3CError ("no such element" , NoSuchElementException .class , 404 ))
82- .add (new W3CError ("no such frame" , NoSuchFrameException .class , 404 ))
83- .add (new W3CError ("no such shadow root" , NoSuchShadowRootException .class , 404 ))
84- .add (new W3CError ("no such window" , NoSuchWindowException .class , 404 ))
85- .add (new W3CError ("session not created" , SessionNotCreatedException .class , 500 ))
86- .add (new W3CError ("stale element reference" , StaleElementReferenceException .class , 404 ))
87- .add (new W3CError ("timeout" , TimeoutException .class , 500 ))
88- .add (new W3CError ("unable to capture screen" , ScreenshotException .class , 500 ))
89- .add (new W3CError ("unable to set cookie" , UnableToSetCookieException .class , 500 ))
90- .add (new W3CError ("unexpected alert open" , UnhandledAlertException .class , 500 ))
91- .add (new W3CError ("unsupported operation" , UnsupportedCommandException .class , 404 ))
92- .add (new W3CError ("unknown command" , UnsupportedCommandException .class , 404 ))
93- .add (new W3CError ("unknown method" , UnsupportedCommandException .class , 405 ))
94- .add (new W3CError ("unknown error" , WebDriverException .class , 500 ))
95- .build ();
63+ List .of (
64+ new W3CError ("script timeout" , ScriptTimeoutException .class , 500 ),
65+ new W3CError ("detached shadow root" , DetachedShadowRootException .class , 404 ),
66+ new W3CError ("element click intercepted" , ElementClickInterceptedException .class , 400 ),
67+ new W3CError ("element not interactable" , ElementNotInteractableException .class , 400 ),
68+ new W3CError ("invalid argument" , InvalidArgumentException .class , 400 ),
69+ new W3CError ("invalid cookie domain" , InvalidCookieDomainException .class , 400 ),
70+ new W3CError ("invalid element state" , InvalidElementStateException .class , 400 ),
71+ new W3CError ("invalid selector" , InvalidSelectorException .class , 400 ),
72+ new W3CError ("invalid session id" , NoSuchSessionException .class , 404 ),
73+ new W3CError ("insecure certificate" , InsecureCertificateException .class , 400 ),
74+ new W3CError ("javascript error" , JavascriptException .class , 500 ),
75+ new W3CError ("move target out of bounds" , MoveTargetOutOfBoundsException .class , 500 ),
76+ new W3CError ("no such alert" , NoAlertPresentException .class , 404 ),
77+ new W3CError ("no such cookie" , NoSuchCookieException .class , 404 ),
78+ new W3CError ("no such element" , NoSuchElementException .class , 404 ),
79+ new W3CError ("no such frame" , NoSuchFrameException .class , 404 ),
80+ new W3CError ("no such shadow root" , NoSuchShadowRootException .class , 404 ),
81+ new W3CError ("no such window" , NoSuchWindowException .class , 404 ),
82+ new W3CError ("session not created" , SessionNotCreatedException .class , 500 ),
83+ new W3CError ("stale element reference" , StaleElementReferenceException .class , 404 ),
84+ new W3CError ("timeout" , TimeoutException .class , 500 ),
85+ new W3CError ("unable to capture screen" , ScreenshotException .class , 500 ),
86+ new W3CError ("unable to set cookie" , UnableToSetCookieException .class , 500 ),
87+ new W3CError ("unexpected alert open" , UnhandledAlertException .class , 500 ),
88+ new W3CError ("unsupported operation" , UnsupportedCommandException .class , 404 ),
89+ new W3CError ("unknown command" , UnsupportedCommandException .class , 404 ),
90+ new W3CError ("unknown method" , UnsupportedCommandException .class , 405 ),
91+ new W3CError ("unknown error" , WebDriverException .class , 500 ));
9692
9793 private ErrorCodec () {
9894 // This will switch to being an interface at some point. Use `createDefault`
@@ -112,15 +108,15 @@ public Map<String, Object> encode(Throwable throwable) {
112108 ? "<no message present in throwable>"
113109 : throwable .getMessage ();
114110
115- return ImmutableMap .of (
111+ StringWriter stacktrace = new StringWriter ();
112+ try (PrintWriter printWriter = new PrintWriter (stacktrace )) {
113+ throwable .printStackTrace (printWriter );
114+ }
115+
116+ return Map .of (
116117 "value" ,
117- ImmutableMap .of (
118- "error" ,
119- err .w3cErrorString ,
120- "message" ,
121- message ,
122- "stacktrace" ,
123- Throwables .getStackTraceAsString (throwable )));
118+ Map .of (
119+ "error" , err .w3cErrorString , "message" , message , "stacktrace" , stacktrace .toString ()));
124120 }
125121
126122 public int getHttpStatusCode (Throwable throwable ) {
0 commit comments