Shaka Packager SDK
status.cc
1 // Copyright 2014 Google Inc. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #include "packager/status.h"
8 
9 #include "packager/base/logging.h"
10 #include "packager/base/strings/stringprintf.h"
11 
12 namespace shaka {
13 
14 namespace error {
15 namespace {
16 std::string ErrorCodeToString(Code error_code) {
17  switch (error_code) {
18  case OK:
19  return "OK";
20  case UNKNOWN:
21  return "UNKNOWN";
22  case CANCELLED:
23  return "CANCELLED";
24  case INVALID_ARGUMENT:
25  return "INVALID_ARGUMENT";
26  case UNIMPLEMENTED:
27  return "UNIMPLEMENTED";
28  case FILE_FAILURE:
29  return "FILE_FAILURE";
30  case END_OF_STREAM:
31  return "END_OF_STREAM";
32  case HTTP_FAILURE:
33  return "HTTP_FAILURE";
34  case PARSER_FAILURE:
35  return "PARSER_FAILURE";
36  case ENCRYPTION_FAILURE:
37  return "ENCRYPTION_FAILURE";
38  case CHUNKING_ERROR:
39  return "CHUNKING_ERROR";
40  case MUXER_FAILURE:
41  return "MUXER_FAILURE";
42  case FRAGMENT_FINALIZED:
43  return "FRAGMENT_FINALIZED";
44  case SERVER_ERROR:
45  return "SERVER_ERROR";
46  case INTERNAL_ERROR:
47  return "INTERNAL_ERROR";
48  case STOPPED:
49  return "STOPPED";
50  case TIME_OUT:
51  return "TIME_OUT";
52  case NOT_FOUND:
53  return "NOT_FOUND";
54  case ALREADY_EXISTS:
55  return "ALREADY_EXISTS";
56  default:
57  NOTIMPLEMENTED() << "Unknown Status Code: " << error_code;
58  return "UNKNOWN_STATUS";
59  }
60 }
61 } // namespace
62 } // namespace error
63 
64 const Status Status::OK = Status(error::OK, "");
65 const Status Status::UNKNOWN = Status(error::UNKNOWN, "");
66 
67 Status::Status(error::Code error_code, const std::string& error_message)
68  : error_code_(error_code) {
69  if (!ok()) {
70  error_message_ = error_message;
71  if (!error_message.empty())
72  VLOG(1) << ToString();
73  }
74 }
75 
76 void Status::Update(Status new_status) {
77  if (ok())
78  *this = std::move(new_status);
79 }
80 
81 std::string Status::ToString() const {
82  if (error_code_ == error::OK)
83  return "OK";
84 
85  return base::StringPrintf("%d (%s): %s",
86  error_code_,
87  error::ErrorCodeToString(error_code_).c_str(),
88  error_message_.c_str());
89 }
90 
91 std::ostream& operator<<(std::ostream& os, const Status& x) {
92  os << x.ToString();
93  return os;
94 }
95 
96 } // namespace shaka
std::string ToString() const
Definition: status.cc:81
All the methods that are virtual are virtual for mocking.
Status()
Creates a "successful" status.
Definition: status.h:113
void Update(Status new_status)
Definition: status.cc:76