Error.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 /* DEBUG: section 04 Error Management */
10 
11 #include "squid.h"
12 #include "base/IoManip.h"
13 #include "debug/Stream.h"
14 #include "error/Error.h"
15 
16 void
17 Error::update(const err_type recentCategory)
18 {
19  if (recentCategory == ERR_NONE)
20  return; // no category given
21 
22  if (category == recentCategory)
23  return; // no new category given
24 
25  if (category != ERR_NONE) {
26  debugs(4, 5, "ignoring: " << errorTypeName(recentCategory) << "; keeping " << *this);
27  return; // the category given earlier has won
28  }
29 
30  category = recentCategory;
31  debugs(4, 3, "new: " << errorTypeName(category));
32 }
33 
34 void
35 Error::update(const ErrorDetail::Pointer &recentDetail)
36 {
37  if (!recentDetail)
38  return; // no new detail given
39 
40  // an error can only have a few details so linear search is faster than indexing
41  for (const auto &oldDetail: details) {
42  if (recentDetail->equals(*oldDetail))
43  return; // the given detail is already present
44  }
45 
46  details.push_back(recentDetail);
47  debugs(4, 3, "new: " << recentDetail);
48 }
49 
50 void
51 Error::update(const Error &recent)
52 {
53  // checking category and detail separately may cause inconsistency, but
54  // may result in more details available if they only become available later
55  update(recent.category);
56  for (const auto &recentDetail: recent.details)
57  update(recentDetail);
58 }
59 
60 void
61 Error::update(const err_type recentCategory, const ErrorDetail::Pointer &recentDetail)
62 {
63  // Optimization: Do not simply call update(Error(...)) here because that
64  // would require allocating and freeing heap memory for storing the detail.
65  update(recentCategory);
66  update(recentDetail);
67 }
68 
69 std::ostream &
70 operator <<(std::ostream &os, const ErrorDetails &details)
71 {
72  os << AsList(details).delimitedBy("+");
73  return os;
74 }
75 
76 std::ostream &
77 operator <<(std::ostream &os, const Error &error)
78 {
79  os << errorTypeName(error.category);
80  os << AsList(error.details).prefixedBy("/").delimitedBy("+");
81  return os;
82 }
83 
std::ostream & operator<<(std::ostream &os, const ErrorDetails &details)
Definition: Error.cc:70
void error(char *format,...)
@ ERR_NONE
Definition: forward.h:15
err_type
Definition: forward.h:14
a transaction problem
Definition: Error.h:27
const char * errorTypeName(err_type err)
Definition: Error.h:77
ErrorDetails details
Definition: Error.h:60
bool equals(const ErrorDetail &other) const
Definition: Detail.h:44
auto & delimitedBy(const char *const d)
a c-string to print between consecutive items (if any). Caller must ensure lifetime.
Definition: IoManip.h:188
std::ostream manipulator to print containers as flat lists
Definition: IoManip.h:176
std::vector< ErrorDetailPointer, PoolingAllocator< ErrorDetailPointer > > ErrorDetails
zero or more details of a single error
Definition: Error.h:20
auto & prefixedBy(const char *const p)
a c-string to print before the first item (if any). Caller must ensure lifetime.
Definition: IoManip.h:182
void update(const Error &)
if necessary, stores the given error information (if any)
Definition: Error.cc:51
err_type category
primary error classification (or ERR_NONE)
Definition: Error.h:55
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:192

 

Introduction

Documentation

Support

Miscellaneous