Debian Codegraph @

c/src/core/error.c

raw
1 /*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 */
21
22 #include <proton/error.h>
23 #include <proton/object.h>
24
25 #include "memory.h"
26 #include "platform/platform.h"
27 #include "util.h"
28
29 #include <proton/connection.h>
30 #include <proton/link.h>
31 #include <proton/session.h>
32
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <assert.h>
37
38 struct pn_error_t {
39 char *text;
40 int code;
41 };
42
43 PN_STRUCT_CLASSDEF(pn_error)
44
45 pn_error_t *pn_error()
46 {
47 pn_error_t *error = (pn_error_t *) pni_mem_allocate(PN_CLASSCLASS(pn_error), sizeof(pn_error_t));
48 if (error != NULL) {
49 error->code = 0;
50 error->text = NULL;
51 }
52 return error;
53 }
54
55 void pn_error_free(pn_error_t *error)
56 {
57 if (error) {
58 pni_mem_subdeallocate(PN_CLASSCLASS(pn_error), error, error->text);
59 pni_mem_deallocate(PN_CLASSCLASS(pn_error), error);
60 }
61 }
62
63 void pn_error_clear(pn_error_t *error)
64 {
65 if (error) {
66 error->code = 0;
67 pni_mem_subdeallocate(PN_CLASSCLASS(pn_error), error, error->text);
68 error->text = NULL;
69 }
70 }
71
72 int pn_error_set(pn_error_t *error, int code, const char *text)
73 {
74 assert(error);
75 pn_error_clear(error);
76 if (code) {
77 error->code = code;
78 error->text = pn_strdup(text);
79 }
80 return code;
81 }
82
83 int pn_error_vformat(pn_error_t *error, int code, const char *fmt, va_list ap)
84 {
85 assert(error);
86 char text[1024];
87 int n = vsnprintf(text, 1024, fmt, ap);
88 if (n >= 1024) {
89 text[1023] = '\0';
90 }
91 return pn_error_set(error, code, text);
92 }
93
94 int pn_error_format(pn_error_t *error, int code, const char *fmt, ...)
95 {
96 assert(error);
97 va_list ap;
98 va_start(ap, fmt);
99 int rcode = pn_error_vformat(error, code, fmt, ap);
100 va_end(ap);
101 return rcode;
102 }
103
104 int pn_error_code(pn_error_t *error)
105 {
106 assert(error);
107 return error->code;
108 }
109
110 const char *pn_error_text(pn_error_t *error)
111 {
112 assert(error);
113 return error->text;
114 }
115
116 int pn_error_copy(pn_error_t *error, pn_error_t *src)
117 {
118 assert(error);
119 if (src) {
120 return pn_error_set(error, pn_error_code(src), pn_error_text(src));
121 } else {
122 pn_error_clear(error);
123 return 0;
124 }
125 }
126
127 const char *pn_code(int code)
128 {
129 switch (code)
130 {
131 case 0: return "<ok>";
132 case PN_EOS: return "PN_EOS";
133 case PN_ERR: return "PN_ERR";
134 case PN_OVERFLOW: return "PN_OVERFLOW";
135 case PN_UNDERFLOW: return "PN_UNDERFLOW";
136 case PN_STATE_ERR: return "PN_STATE_ERR";
137 case PN_ARG_ERR: return "PN_ARG_ERR";
138 case PN_TIMEOUT: return "PN_TIMEOUT";
139 case PN_INTR: return "PN_INTR";
140 case PN_INPROGRESS: return "PN_INPROGRESS";
141 case PN_OUT_OF_MEMORY: return "PN_OUT_OF_MEMORY";
142 case PN_ABORTED: return "PN_ABORTED";
143 default: return "<unknown>";
144 }
145 }
146
147 // Deprecated ABI compatibility stubs
148
149 // Constant to make sure that no one tries to change this.
150 static const pn_error_t pn_error_null = {NULL, 0};
151
152 pn_error_t *pn_connection_error(pn_connection_t *c) {return (pn_error_t*) &pn_error_null;}
153 pn_error_t *pn_session_error(pn_session_t *c) {return (pn_error_t*) &pn_error_null;}
154 pn_error_t *pn_link_error(pn_link_t *c) {return (pn_error_t*) &pn_error_null;}