Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / chrome / test / chromedriver / chrome / status.cc
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/test/chromedriver/chrome/status.h"
6
7 #include "base/strings/stringprintf.h"
8
9 namespace {
10
11 // Returns the string equivalent of the given |ErrorCode|.
12 const char* DefaultMessageForStatusCode(StatusCode code) {
13   switch (code) {
14     case kOk:
15       return "ok";
16     case kNoSuchSession:
17       return "no such session";
18     case kNoSuchElement:
19       return "no such element";
20     case kNoSuchFrame:
21       return "no such frame";
22     case kUnknownCommand:
23       return "unknown command";
24     case kStaleElementReference:
25       return "stale element reference";
26     case kElementNotVisible:
27       return "element not visible";
28     case kInvalidElementState:
29       return "invalid element state";
30     case kUnknownError:
31       return "unknown error";
32     case kJavaScriptError:
33       return "javascript error";
34     case kXPathLookupError:
35       return "xpath lookup error";
36     case kTimeout:
37       return "timeout";
38     case kNoSuchWindow:
39       return "no such window";
40     case kInvalidCookieDomain:
41       return "invalid cookie domain";
42     case kUnexpectedAlertOpen:
43       return "unexpected alert open";
44     case kNoAlertOpen:
45       return "no alert open";
46     case kScriptTimeout:
47       return "asynchronous script timeout";
48     case kInvalidSelector:
49       return "invalid selector";
50     case kSessionNotCreatedException:
51       return "session not created exception";
52     case kNoSuchExecutionContext:
53       return "no such execution context";
54     case kChromeNotReachable:
55       return "chrome not reachable";
56     case kDisconnected:
57       return "disconnected";
58     case kForbidden:
59       return "forbidden";
60     case kTabCrashed:
61       return "tab crashed";
62     default:
63       return "<unknown>";
64   }
65 }
66
67 }  // namespace
68
69 Status::Status(StatusCode code)
70     : code_(code), msg_(DefaultMessageForStatusCode(code)) {}
71
72 Status::Status(StatusCode code, const std::string& details)
73     : code_(code),
74       msg_(DefaultMessageForStatusCode(code) + std::string(": ") + details) {
75 }
76
77 Status::Status(StatusCode code, const Status& cause)
78     : code_(code),
79       msg_(DefaultMessageForStatusCode(code) + std::string("\nfrom ") +
80            cause.message()) {}
81
82 Status::Status(StatusCode code,
83                const std::string& details,
84                const Status& cause)
85     : code_(code),
86       msg_(DefaultMessageForStatusCode(code) + std::string(": ") + details +
87            "\nfrom " + cause.message()) {
88 }
89
90 Status::~Status() {}
91
92 void Status::AddDetails(const std::string& details) {
93   msg_ += base::StringPrintf("\n  (%s)", details.c_str());
94 }
95
96 bool Status::IsOk() const {
97   return code_ == kOk;
98 }
99
100 bool Status::IsError() const {
101   return code_ != kOk;
102 }
103
104 StatusCode Status::code() const {
105   return code_;
106 }
107
108 const std::string& Status::message() const {
109   return msg_;
110 }