| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "proton_bits.hpp" |
| 21 |
#include "proton/error_condition.hpp" |
| 22 |
|
| 23 |
#include <string> |
| 24 |
#include <ostream> |
| 25 |
|
| 26 |
#include <proton/condition.h> |
| 27 |
#include <proton/error.h> |
| 28 |
#include <proton/object.h> |
| 29 |
|
| 30 |
namespace proton { |
| 31 |
|
| 32 |
std::string error_str(long code) { |
| 33 |
switch (code) |
| 34 |
{ |
| 35 |
case 0: return "ok"; |
| 36 |
case PN_EOS: return "end of data stream"; |
| 37 |
case PN_ERR: return "error"; |
| 38 |
case PN_OVERFLOW: return "overflow"; |
| 39 |
case PN_UNDERFLOW: return "underflow"; |
| 40 |
case PN_STATE_ERR: return "invalid state"; |
| 41 |
case PN_ARG_ERR: return "invalid argument"; |
| 42 |
case PN_TIMEOUT: return "timeout"; |
| 43 |
case PN_INTR: return "interrupt"; |
| 44 |
default: return "unknown error code"; |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
std::string error_str(pn_error_t* err, long code) { |
| 49 |
if (err && pn_error_code(err)) { |
| 50 |
const char* text = pn_error_text(err); |
| 51 |
return text ? std::string(text) : error_str(pn_error_code(err)); |
| 52 |
} |
| 53 |
return error_str(code); |
| 54 |
} |
| 55 |
|
| 56 |
std::ostream& operator<<(std::ostream& o, const inspectable& object) { |
| 57 |
pn_string_t* str = pn_string(""); |
| 58 |
pn_inspect(object.value, str); |
| 59 |
o << pn_string_get(str); |
| 60 |
pn_free(str); |
| 61 |
return o; |
| 62 |
} |
| 63 |
|
| 64 |
void set_error_condition(const error_condition& e, pn_condition_t *c) { |
| 65 |
pn_condition_clear(c); |
| 66 |
|
| 67 |
if (!e.name().empty()) { |
| 68 |
pn_condition_set_name(c, e.name().c_str()); |
| 69 |
} |
| 70 |
if (!e.description().empty()) { |
| 71 |
pn_condition_set_description(c, e.description().c_str()); |
| 72 |
} |
| 73 |
value(pn_condition_info(c)) = e.properties(); |
| 74 |
} |
| 75 |
|
| 76 |
} |