1 // Copyright 2020 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 "error_support.h" 6 7 #include <cassert> 8 9 namespace v8_crdtp { 10 Push()11void ErrorSupport::Push() { 12 stack_.emplace_back(); 13 } 14 Pop()15void ErrorSupport::Pop() { 16 stack_.pop_back(); 17 } 18 SetName(const char * name)19void ErrorSupport::SetName(const char* name) { 20 assert(!stack_.empty()); 21 stack_.back().type = NAME; 22 stack_.back().name = name; 23 } 24 SetIndex(size_t index)25void ErrorSupport::SetIndex(size_t index) { 26 assert(!stack_.empty()); 27 stack_.back().type = INDEX; 28 stack_.back().index = index; 29 } 30 AddError(const char * error)31void ErrorSupport::AddError(const char* error) { 32 assert(!stack_.empty()); 33 if (!errors_.empty()) 34 errors_ += "; "; 35 for (size_t ii = 0; ii < stack_.size(); ++ii) { 36 if (ii) 37 errors_ += "."; 38 const Segment& s = stack_[ii]; 39 switch (s.type) { 40 case NAME: 41 errors_ += s.name; 42 continue; 43 case INDEX: 44 errors_ += std::to_string(s.index); 45 continue; 46 default: 47 assert(s.type != EMPTY); 48 continue; 49 } 50 } 51 errors_ += ": "; 52 errors_ += error; 53 } 54 Errors() const55span<uint8_t> ErrorSupport::Errors() const { 56 return SpanFrom(errors_); 57 } 58 59 } // namespace v8_crdtp 60