summaryrefslogtreecommitdiff
path: root/subversion/bindings/cxxhl/src/exception.cpp
blob: 0f0ee3fc2b93b6250f05044b408479b74f4f743e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/**
 * @copyright
 * ====================================================================
 *    Licensed to the Apache Software Foundation (ASF) under one
 *    or more contributor license agreements.  See the NOTICE file
 *    distributed with this work for additional information
 *    regarding copyright ownership.  The ASF licenses this file
 *    to you under the Apache License, Version 2.0 (the
 *    "License"); you may not use this file except in compliance
 *    with the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing,
 *    software distributed under the License is distributed on an
 *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *    KIND, either express or implied.  See the License for the
 *    specific language governing permissions and limitations
 *    under the License.
 * ====================================================================
 * @endcopyright
 */

#include <algorithm>
#include <cstddef>
#include <cstring>
#include <new>
#include <sstream>

#include "svncxxhl/exception.hpp"
#include "private.hpp"
#include "aprwrap.hpp"

#include "svn_error.h"
#include "svn_utf.h"
#include "private/svn_atomic.h"
#include "private/svn_error_private.h"
#include "svn_private_config.h"
#undef TRUE
#undef FALSE

namespace apache {
namespace subversion {
namespace cxxhl {

namespace detail {

class ErrorDescription
{
public:
  typedef compat::shared_ptr<ErrorDescription> shared_ptr;

  static shared_ptr create(const char* message, int error_code,
                           const char *loc_file, long loc_line,
                           bool trace_link)
    {
      const bool empty_message = (message == NULL);
      const std::size_t length = (empty_message ? 0 : std::strlen(message));
      void* memblock = ::operator new(length + sizeof(ErrorDescription));

      ErrorDescription* description = new(memblock) ErrorDescription(
          error_code, loc_file, loc_line, trace_link, empty_message);
      if (length)
        std::memcpy(description->m_message, message, length);
      description->m_message[length] = 0;
      return shared_ptr(description);
    }

  static shared_ptr create(const char* message, int error_code)
    {
      return create(message, error_code, NULL, 0, false);
    }

  ~ErrorDescription() throw() {}

  const char* what() const throw() { return (m_empty ? NULL : m_message); }
  int code() const throw() { return m_errno; }
  const char* file() const throw() { return m_loc_file; }
  long line() const throw() { return m_loc_line; }
  bool trace() const throw() { return m_trace; }
  shared_ptr& nested() throw() { return m_nested; }
  const shared_ptr& nested() const throw() { return m_nested; }

private:
  ErrorDescription(int error_code,
                   const char *loc_file, long loc_line,
                   bool trace_link, bool empty_message) throw()
    : m_loc_file(loc_file),
      m_loc_line(loc_line),
      m_trace(trace_link),
      m_empty(empty_message),
      m_errno(error_code)
    {}

  const char* m_loc_file;
  long m_loc_line;
  bool m_trace;
  bool m_empty;

  shared_ptr m_nested;

  int m_errno;                  ///< The (SVN or APR) error code.
  char m_message[1];            ///< The error message
};

} // namespace detail

//
// Class InternalError
//

InternalError::InternalError(const char* description)
  : m_description(detail::ErrorDescription::create(description, 0))
{}

InternalError::InternalError(const InternalError& that) throw()
  : m_description(that.m_description)
{}

InternalError& InternalError::operator=(const InternalError& that) throw()
{
  if (this == &that)
    return *this;

  // This in-place destroy+copy implementation of the assignment
  // operator is safe because both the destructor and the copy
  // constructor do not throw exceptions.
  this->~InternalError();
  return *new(this) InternalError(that);
}

InternalError::~InternalError() throw() {}

const char* InternalError::what() const throw()
{
  return m_description->what();
}

InternalError::InternalError(description_ptr description) throw()
  : m_description(description)
{}

//
// Class Error
//

Error::Error(const Error& that) throw()
  : InternalError(that.m_description)
{}

Error& Error::operator=(const Error& that) throw()
{
  if (this == &that)
    return *this;

  // This in-place destroy+copy implementation of the assignment
  // operator is safe because both the destructor and the copy
  // constructor do not throw exceptions.
  this->~Error();
  return *new(this) Error(that);
}

Error::~Error() throw() {}

int Error::code() const throw()
{
  return m_description->code();
}

namespace {
const char* get_generic_message(apr_status_t error_code,
                                const APR::Pool& scratch_pool)
{
  char errorbuf[512];

  // Is this a Subversion-specific error code?
  if (error_code > APR_OS_START_USEERR && error_code <= APR_OS_START_CANONERR)
    return svn_strerror(error_code, errorbuf, sizeof(errorbuf));
  // Otherwise, this must be an APR error code.
  else
    {
      const char* generic;
      svn_error_t* err = svn_utf_cstring_to_utf8(
          &generic,
          apr_strerror(error_code, errorbuf, sizeof(errorbuf)),
          scratch_pool.get());
      if (!err)
        return generic;

      // Use fuzzy transliteration instead.
      svn_error_clear(err);
      return svn_utf_cstring_from_utf8_fuzzy(errorbuf, scratch_pool.get());
    }
}

void handle_one_error(Error::MessageList& ml, bool show_traces,
                      const detail::ErrorDescription* descr,
                      const APR::Pool& pool)
{
  const int error_code = descr->code();

  if (show_traces && descr->file())
    {
      const char* file_utf8 = NULL;
      svn_error_t* err =
        svn_utf_cstring_to_utf8(&file_utf8, descr->file(), pool.get());
      if (err)
        {
          svn_error_clear(err);
          file_utf8 = NULL;
        }
      std::ostringstream buffer;
      if (file_utf8)
        buffer << file_utf8 << ':' << descr->line();
      else
        buffer << "svn:<undefined>";
      if (descr->trace())
        buffer << ',';
      else
        {
#ifdef SVN_DEBUG
          if (const char* symbolic_name = svn_error_symbolic_name(error_code))
            buffer << ": (apr_err=" << symbolic_name << ')';
          else
#endif
            buffer << ": (apr_err=" << error_code << ')';
        }
      ml.push_back(Error::Message(error_code, buffer.str(), true));
    }

  if (descr->trace())
    return;

  const char *description = descr->what();
  if (!description)
    description = get_generic_message(error_code, pool);
  ml.push_back(Error::Message(error_code, std::string(description), false));
}
} // anonymous namespace

Error::MessageList Error::compile_messages(bool show_traces) const
{
  // Determine the maximum size of the returned list
  MessageList::size_type max_length = 0;
  for (const detail::ErrorDescription* description = m_description.get();
       description; description = description->nested().get())
    {
      if (show_traces && description->file())
        ++max_length;                   // We will display an error location
      if (!description->trace())
        ++max_length;                   // Traces do not emit a message line
    }
  MessageList ml;
  ml.reserve(max_length);

  // This vector holds a list of all error codes that we've printed
  // the generic description for.  See svn_handle_error2 for details.
  std::vector<int> empties;
  empties.reserve(max_length);

  APR::IterationPool iterbase;
  for (const detail::ErrorDescription* description = m_description.get();
       description; description = description->nested().get())
    {
      APR::Pool::Iteration iterpool(iterbase);

      if (!description->what())
        {
          // Non-specific messages are printed only once.
          std::vector<int>::iterator it = std::find(
              empties.begin(), empties.end(), description->code());
          if (it != empties.end())
            continue;
          empties.push_back(description->code());
        }
      handle_one_error(ml, show_traces, description, iterpool.pool());
    }
  return ml;
}

const char* Error::Message::generic_message() const
{
  APR::Pool pool;
  return get_generic_message(m_errno, pool);
}

namespace detail {
void checked_call(svn_error_t* err)
{
  if (!err)
    return;

  struct ErrorBuilder : public Error
  {
    explicit ErrorBuilder (ErrorDescription::shared_ptr description)
      : Error(description)
      {}
  };

  struct CancelledBuilder : public Cancelled
  {
    explicit CancelledBuilder (ErrorDescription::shared_ptr description)
      : Cancelled(description)
      {}
  };

  ErrorDescription::shared_ptr description;
  ErrorDescription::shared_ptr* current = &description;

  bool cancelled = false;
  for (svn_error_t* next = err; next; next = next->child)
    {
      *current = ErrorDescription::create(next->message, next->apr_err,
                                          next->file, next->line,
                                          svn_error__is_tracing_link(next));
      current = &(*current)->nested();
      if (next->apr_err == SVN_ERR_CANCELLED)
        cancelled = true;
    }
  svn_error_clear(err);

  if (cancelled)
    throw CancelledBuilder(description);
  else
    throw ErrorBuilder(description);
}
} // namespace detail

} // namespace cxxhl
} // namespace subversion
} // namespace apache