/src/node/deps/inspector_protocol/crdtp/status.h
Line | Count | Source |
1 | | // Copyright 2019 The Chromium Authors |
2 | | // Use of this source code is governed by a BSD-style license that can be |
3 | | // found in the LICENSE file. |
4 | | |
5 | | #ifndef CRDTP_STATUS_H_ |
6 | | #define CRDTP_STATUS_H_ |
7 | | |
8 | | #include <cassert> |
9 | | #include <cstddef> |
10 | | #include <limits> |
11 | | #include <string> |
12 | | |
13 | | #include "export.h" |
14 | | |
15 | | namespace crdtp { |
16 | | // ============================================================================= |
17 | | // Status and Error codes |
18 | | // ============================================================================= |
19 | | |
20 | | enum class Error { |
21 | | OK = 0, |
22 | | |
23 | | // JSON parsing errors; checked when parsing / converting from JSON. |
24 | | // See json.{h,cc}. |
25 | | JSON_PARSER_UNPROCESSED_INPUT_REMAINS = 0x01, |
26 | | JSON_PARSER_STACK_LIMIT_EXCEEDED = 0x02, |
27 | | JSON_PARSER_NO_INPUT = 0x03, |
28 | | JSON_PARSER_INVALID_TOKEN = 0x04, |
29 | | JSON_PARSER_INVALID_NUMBER = 0x05, |
30 | | JSON_PARSER_INVALID_STRING = 0x06, |
31 | | JSON_PARSER_UNEXPECTED_ARRAY_END = 0x07, |
32 | | JSON_PARSER_COMMA_OR_ARRAY_END_EXPECTED = 0x08, |
33 | | JSON_PARSER_STRING_LITERAL_EXPECTED = 0x09, |
34 | | JSON_PARSER_COLON_EXPECTED = 0x0a, |
35 | | JSON_PARSER_UNEXPECTED_MAP_END = 0x0b, |
36 | | JSON_PARSER_COMMA_OR_MAP_END_EXPECTED = 0x0c, |
37 | | JSON_PARSER_VALUE_EXPECTED = 0x0d, |
38 | | |
39 | | // CBOR parsing errors; checked when parsing / converting from CBOR. |
40 | | CBOR_INVALID_INT32 = 0x0e, |
41 | | CBOR_INVALID_DOUBLE = 0x0f, |
42 | | CBOR_INVALID_ENVELOPE = 0x10, |
43 | | CBOR_ENVELOPE_CONTENTS_LENGTH_MISMATCH = 0x11, |
44 | | CBOR_MAP_OR_ARRAY_EXPECTED_IN_ENVELOPE = 0x12, |
45 | | CBOR_INVALID_STRING8 = 0x13, |
46 | | CBOR_INVALID_STRING16 = 0x14, |
47 | | CBOR_INVALID_BINARY = 0x15, |
48 | | CBOR_UNSUPPORTED_VALUE = 0x16, |
49 | | CBOR_UNEXPECTED_EOF_IN_ENVELOPE = 0x17, |
50 | | CBOR_INVALID_START_BYTE = 0x18, |
51 | | CBOR_UNEXPECTED_EOF_EXPECTED_VALUE = 0x19, |
52 | | CBOR_UNEXPECTED_EOF_IN_ARRAY = 0x1a, |
53 | | CBOR_UNEXPECTED_EOF_IN_MAP = 0x1b, |
54 | | CBOR_INVALID_MAP_KEY = 0x1c, |
55 | | CBOR_DUPLICATE_MAP_KEY = 0x1d, |
56 | | CBOR_STACK_LIMIT_EXCEEDED = 0x1e, |
57 | | CBOR_TRAILING_JUNK = 0x1f, |
58 | | CBOR_MAP_START_EXPECTED = 0x20, |
59 | | CBOR_MAP_STOP_EXPECTED = 0x21, |
60 | | CBOR_ARRAY_START_EXPECTED = 0x22, |
61 | | CBOR_ENVELOPE_SIZE_LIMIT_EXCEEDED = 0x23, |
62 | | |
63 | | // Message errors are constraints we place on protocol messages coming |
64 | | // from a protocol client; these are checked in crdtp::Dispatchable |
65 | | // (see dispatch.h) as it performs a shallow parse. |
66 | | MESSAGE_MUST_BE_AN_OBJECT = 0x24, |
67 | | MESSAGE_MUST_HAVE_INTEGER_ID_PROPERTY = 0x25, |
68 | | MESSAGE_MUST_HAVE_STRING_METHOD_PROPERTY = 0x26, |
69 | | MESSAGE_MAY_HAVE_STRING_SESSION_ID_PROPERTY = 0x27, |
70 | | MESSAGE_MAY_HAVE_OBJECT_PARAMS_PROPERTY = 0x28, |
71 | | MESSAGE_HAS_UNKNOWN_PROPERTY = 0x29, |
72 | | |
73 | | BINDINGS_MANDATORY_FIELD_MISSING = 0x30, |
74 | | BINDINGS_BOOL_VALUE_EXPECTED = 0x31, |
75 | | BINDINGS_INT32_VALUE_EXPECTED = 0x32, |
76 | | BINDINGS_DOUBLE_VALUE_EXPECTED = 0x33, |
77 | | BINDINGS_STRING_VALUE_EXPECTED = 0x34, |
78 | | BINDINGS_STRING8_VALUE_EXPECTED = 0x35, |
79 | | BINDINGS_BINARY_VALUE_EXPECTED = 0x36, |
80 | | BINDINGS_DICTIONARY_VALUE_EXPECTED = 0x37, |
81 | | BINDINGS_INVALID_BASE64_STRING = 0x38, |
82 | | }; |
83 | | |
84 | | // A status value with position that can be copied. The default status |
85 | | // is OK. Usually, error status values should come with a valid position. |
86 | | struct CRDTP_EXPORT Status { |
87 | 0 | static constexpr size_t npos() { return std::numeric_limits<size_t>::max(); } |
88 | | |
89 | 0 | bool ok() const { return error == Error::OK; } |
90 | | |
91 | | Error error = Error::OK; |
92 | | size_t pos = npos(); |
93 | 0 | Status(Error error, size_t pos) : error(error), pos(pos) {} |
94 | 0 | Status() = default; |
95 | | |
96 | 0 | bool IsMessageError() const { |
97 | 0 | return error >= Error::MESSAGE_MUST_BE_AN_OBJECT && |
98 | 0 | error <= Error::MESSAGE_HAS_UNKNOWN_PROPERTY; |
99 | 0 | } |
100 | | |
101 | | // Returns 7 bit US-ASCII string, either "OK" or an error message without |
102 | | // position. |
103 | | std::string Message() const; |
104 | | |
105 | | // Returns a 7 bit US-ASCII string, either "OK" or an error message that |
106 | | // includes the position. |
107 | | std::string ToASCIIString() const; |
108 | | }; |
109 | | |
110 | | template <typename T> |
111 | | class StatusOr { |
112 | | public: |
113 | | explicit StatusOr(const T& value) : value_(value) {} |
114 | | explicit StatusOr(T&& value) : value_(std::move(value)) {} |
115 | | explicit StatusOr(const Status& status) : status_(status) {} |
116 | | |
117 | | bool ok() const { return status_.ok(); } |
118 | | const Status& status() const { return status_; } |
119 | | T& operator*() & { return value(); } |
120 | | const T& operator*() const& { return value(); } |
121 | | T&& operator*() && { return value(); } |
122 | | |
123 | | T& value() & { |
124 | | assert(ok()); |
125 | | return value_; |
126 | | } |
127 | | T&& value() && { |
128 | | assert(ok()); |
129 | | return std::move(value_); |
130 | | } |
131 | | const T& value() const& { |
132 | | assert(ok()); |
133 | | return value_; |
134 | | } |
135 | | |
136 | | private: |
137 | | Status status_; |
138 | | T value_; |
139 | | }; |
140 | | |
141 | | } // namespace crdtp |
142 | | |
143 | | #endif // CRDTP_STATUS_H_ |