status.cc 5.01 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "status.h"

namespace v8_crdtp {
// =============================================================================
// Status and Error codes
// =============================================================================

12
std::string Status::Message() const {
13 14 15 16
  switch (error) {
    case Error::OK:
      return "OK";
    case Error::JSON_PARSER_UNPROCESSED_INPUT_REMAINS:
17
      return "JSON: unprocessed input remains";
18
    case Error::JSON_PARSER_STACK_LIMIT_EXCEEDED:
19
      return "JSON: stack limit exceeded";
20
    case Error::JSON_PARSER_NO_INPUT:
21
      return "JSON: no input";
22
    case Error::JSON_PARSER_INVALID_TOKEN:
23
      return "JSON: invalid token";
24
    case Error::JSON_PARSER_INVALID_NUMBER:
25
      return "JSON: invalid number";
26
    case Error::JSON_PARSER_INVALID_STRING:
27
      return "JSON: invalid string";
28
    case Error::JSON_PARSER_UNEXPECTED_ARRAY_END:
29
      return "JSON: unexpected array end";
30
    case Error::JSON_PARSER_COMMA_OR_ARRAY_END_EXPECTED:
31
      return "JSON: comma or array end expected";
32
    case Error::JSON_PARSER_STRING_LITERAL_EXPECTED:
33
      return "JSON: string literal expected";
34
    case Error::JSON_PARSER_COLON_EXPECTED:
35
      return "JSON: colon expected";
36
    case Error::JSON_PARSER_UNEXPECTED_MAP_END:
37
      return "JSON: unexpected map end";
38
    case Error::JSON_PARSER_COMMA_OR_MAP_END_EXPECTED:
39
      return "JSON: comma or map end expected";
40
    case Error::JSON_PARSER_VALUE_EXPECTED:
41
      return "JSON: value expected";
42 43

    case Error::CBOR_INVALID_INT32:
44
      return "CBOR: invalid int32";
45
    case Error::CBOR_INVALID_DOUBLE:
46
      return "CBOR: invalid double";
47
    case Error::CBOR_INVALID_ENVELOPE:
48
      return "CBOR: invalid envelope";
49
    case Error::CBOR_ENVELOPE_CONTENTS_LENGTH_MISMATCH:
50
      return "CBOR: envelope contents length mismatch";
51
    case Error::CBOR_MAP_OR_ARRAY_EXPECTED_IN_ENVELOPE:
52
      return "CBOR: map or array expected in envelope";
53
    case Error::CBOR_INVALID_STRING8:
54
      return "CBOR: invalid string8";
55
    case Error::CBOR_INVALID_STRING16:
56
      return "CBOR: invalid string16";
57
    case Error::CBOR_INVALID_BINARY:
58
      return "CBOR: invalid binary";
59
    case Error::CBOR_UNSUPPORTED_VALUE:
60
      return "CBOR: unsupported value";
61
    case Error::CBOR_NO_INPUT:
62
      return "CBOR: no input";
63
    case Error::CBOR_INVALID_START_BYTE:
64
      return "CBOR: invalid start byte";
65
    case Error::CBOR_UNEXPECTED_EOF_EXPECTED_VALUE:
66
      return "CBOR: unexpected eof expected value";
67
    case Error::CBOR_UNEXPECTED_EOF_IN_ARRAY:
68
      return "CBOR: unexpected eof in array";
69
    case Error::CBOR_UNEXPECTED_EOF_IN_MAP:
70
      return "CBOR: unexpected eof in map";
71
    case Error::CBOR_INVALID_MAP_KEY:
72 73 74
      return "CBOR: invalid map key";
    case Error::CBOR_DUPLICATE_MAP_KEY:
      return "CBOR: duplicate map key";
75
    case Error::CBOR_STACK_LIMIT_EXCEEDED:
76
      return "CBOR: stack limit exceeded";
77
    case Error::CBOR_TRAILING_JUNK:
78
      return "CBOR: trailing junk";
79
    case Error::CBOR_MAP_START_EXPECTED:
80
      return "CBOR: map start expected";
81
    case Error::CBOR_MAP_STOP_EXPECTED:
82
      return "CBOR: map stop expected";
83
    case Error::CBOR_ARRAY_START_EXPECTED:
84
      return "CBOR: array start expected";
85
    case Error::CBOR_ENVELOPE_SIZE_LIMIT_EXCEEDED:
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
      return "CBOR: envelope size limit exceeded";

    case Error::MESSAGE_MUST_BE_AN_OBJECT:
      return "Message must be an object";
    case Error::MESSAGE_MUST_HAVE_INTEGER_ID_PROPERTY:
      return "Message must have integer 'id' property";
    case Error::MESSAGE_MUST_HAVE_STRING_METHOD_PROPERTY:
      return "Message must have string 'method' property";
    case Error::MESSAGE_MAY_HAVE_STRING_SESSION_ID_PROPERTY:
      return "Message may have string 'sessionId' property";
    case Error::MESSAGE_MAY_HAVE_OBJECT_PARAMS_PROPERTY:
      return "Message may have object 'params' property";
    case Error::MESSAGE_HAS_UNKNOWN_PROPERTY:
      return "Message has property other than "
             "'id', 'method', 'sessionId', 'params'";
101 102

    case Error::BINDINGS_MANDATORY_FIELD_MISSING:
103
      return "BINDINGS: mandatory field missing";
104
    case Error::BINDINGS_BOOL_VALUE_EXPECTED:
105
      return "BINDINGS: bool value expected";
106
    case Error::BINDINGS_INT32_VALUE_EXPECTED:
107
      return "BINDINGS: int32 value expected";
108
    case Error::BINDINGS_DOUBLE_VALUE_EXPECTED:
109
      return "BINDINGS: double value expected";
110
    case Error::BINDINGS_STRING_VALUE_EXPECTED:
111
      return "BINDINGS: string value expected";
112
    case Error::BINDINGS_STRING8_VALUE_EXPECTED:
113
      return "BINDINGS: string8 value expected";
114
    case Error::BINDINGS_BINARY_VALUE_EXPECTED:
115
      return "BINDINGS: binary value expected";
116 117 118 119 120
  }
  // Some compilers can't figure out that we can't get here.
  return "INVALID ERROR CODE";
}

121 122 123 124
std::string Status::ToASCIIString() const {
  if (ok())
    return "OK";
  return Message() + " at position " + std::to_string(pos);
125 126
}
}  // namespace v8_crdtp