1 // Copyright 2019 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 #ifndef V8_CRDTP_STATUS_H_
6 #define V8_CRDTP_STATUS_H_
7 
8 #include <cstddef>
9 #include <limits>
10 #include <string>
11 
12 #include "export.h"
13 
14 namespace v8_crdtp {
15 // =============================================================================
16 // Status and Error codes
17 // =============================================================================
18 
19 enum class Error {
20   OK = 0,
21 
22   // JSON parsing errors; checked when parsing / converting from JSON.
23   // See json.{h,cc}.
24   JSON_PARSER_UNPROCESSED_INPUT_REMAINS = 0x01,
25   JSON_PARSER_STACK_LIMIT_EXCEEDED = 0x02,
26   JSON_PARSER_NO_INPUT = 0x03,
27   JSON_PARSER_INVALID_TOKEN = 0x04,
28   JSON_PARSER_INVALID_NUMBER = 0x05,
29   JSON_PARSER_INVALID_STRING = 0x06,
30   JSON_PARSER_UNEXPECTED_ARRAY_END = 0x07,
31   JSON_PARSER_COMMA_OR_ARRAY_END_EXPECTED = 0x08,
32   JSON_PARSER_STRING_LITERAL_EXPECTED = 0x09,
33   JSON_PARSER_COLON_EXPECTED = 0x0a,
34   JSON_PARSER_UNEXPECTED_MAP_END = 0x0b,
35   JSON_PARSER_COMMA_OR_MAP_END_EXPECTED = 0x0c,
36   JSON_PARSER_VALUE_EXPECTED = 0x0d,
37 
38   // CBOR parsing errors; checked when parsing / converting from CBOR.
39   CBOR_INVALID_INT32 = 0x0e,
40   CBOR_INVALID_DOUBLE = 0x0f,
41   CBOR_INVALID_ENVELOPE = 0x10,
42   CBOR_ENVELOPE_CONTENTS_LENGTH_MISMATCH = 0x11,
43   CBOR_MAP_OR_ARRAY_EXPECTED_IN_ENVELOPE = 0x12,
44   CBOR_INVALID_STRING8 = 0x13,
45   CBOR_INVALID_STRING16 = 0x14,
46   CBOR_INVALID_BINARY = 0x15,
47   CBOR_UNSUPPORTED_VALUE = 0x16,
48   CBOR_NO_INPUT = 0x17,
49   CBOR_INVALID_START_BYTE = 0x18,
50   CBOR_UNEXPECTED_EOF_EXPECTED_VALUE = 0x19,
51   CBOR_UNEXPECTED_EOF_IN_ARRAY = 0x1a,
52   CBOR_UNEXPECTED_EOF_IN_MAP = 0x1b,
53   CBOR_INVALID_MAP_KEY = 0x1c,
54   CBOR_DUPLICATE_MAP_KEY = 0x1d,
55   CBOR_STACK_LIMIT_EXCEEDED = 0x1e,
56   CBOR_TRAILING_JUNK = 0x1f,
57   CBOR_MAP_START_EXPECTED = 0x20,
58   CBOR_MAP_STOP_EXPECTED = 0x21,
59   CBOR_ARRAY_START_EXPECTED = 0x22,
60   CBOR_ENVELOPE_SIZE_LIMIT_EXCEEDED = 0x23,
61 
62   // Message errors are constraints we place on protocol messages coming
63   // from a protocol client; these are checked in crdtp::Dispatchable
64   // (see dispatch.h) as it performs a shallow parse.
65   MESSAGE_MUST_BE_AN_OBJECT = 0x24,
66   MESSAGE_MUST_HAVE_INTEGER_ID_PROPERTY = 0x25,
67   MESSAGE_MUST_HAVE_STRING_METHOD_PROPERTY = 0x26,
68   MESSAGE_MAY_HAVE_STRING_SESSION_ID_PROPERTY = 0x27,
69   MESSAGE_MAY_HAVE_OBJECT_PARAMS_PROPERTY = 0x28,
70   MESSAGE_HAS_UNKNOWN_PROPERTY = 0x29,
71 
72   BINDINGS_MANDATORY_FIELD_MISSING = 0x30,
73   BINDINGS_BOOL_VALUE_EXPECTED = 0x31,
74   BINDINGS_INT32_VALUE_EXPECTED = 0x32,
75   BINDINGS_DOUBLE_VALUE_EXPECTED = 0x33,
76   BINDINGS_STRING_VALUE_EXPECTED = 0x34,
77   BINDINGS_STRING8_VALUE_EXPECTED = 0x35,
78   BINDINGS_BINARY_VALUE_EXPECTED = 0x36,
79 };
80 
81 // A status value with position that can be copied. The default status
82 // is OK. Usually, error status values should come with a valid position.
83 struct Status {
nposStatus84   static constexpr size_t npos() { return std::numeric_limits<size_t>::max(); }
85 
okStatus86   bool ok() const { return error == Error::OK; }
87 
88   Error error = Error::OK;
89   size_t pos = npos();
StatusStatus90   Status(Error error, size_t pos) : error(error), pos(pos) {}
91   Status() = default;
92 
IsMessageErrorStatus93   bool IsMessageError() const {
94     return error >= Error::MESSAGE_MUST_BE_AN_OBJECT &&
95            error <= Error::MESSAGE_HAS_UNKNOWN_PROPERTY;
96   }
97 
98   // Returns 7 bit US-ASCII string, either "OK" or an error message without
99   // position.
100   std::string Message() const;
101 
102   // Returns a 7 bit US-ASCII string, either "OK" or an error message that
103   // includes the position.
104   std::string ToASCIIString() const;
105 };
106 }  // namespace v8_crdtp
107 
108 #endif  // V8_CRDTP_STATUS_H_
109