File Coverage

bson/bson-error.c
Criterion Covered Total %
statement 0 16 0.0
branch 0 4 0.0
condition n/a
subroutine n/a
pod n/a
total 0 20 0.0


line stmt bran cond sub pod time code
1             /*
2             * Copyright 2013 MongoDB, Inc.
3             *
4             * Licensed under the Apache License, Version 2.0 (the "License");
5             * you may not use this file except in compliance with the License.
6             * You may obtain a copy of the License at
7             *
8             * http://www.apache.org/licenses/LICENSE-2.0
9             *
10             * Unless required by applicable law or agreed to in writing, software
11             * distributed under the License is distributed on an "AS IS" BASIS,
12             * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13             * See the License for the specific language governing permissions and
14             * limitations under the License.
15             */
16              
17              
18             #include
19             #include
20              
21             #include "bson-compat.h"
22             #include "bson-config.h"
23             #include "bson-error.h"
24             #include "bson-memory.h"
25             #include "bson-string.h"
26             #include "bson-types.h"
27              
28              
29             /*
30             *--------------------------------------------------------------------------
31             *
32             * bson_set_error --
33             *
34             * Initializes @error using the parameters specified.
35             *
36             * @domain is an application specific error domain which should
37             * describe which module initiated the error. Think of this as the
38             * exception type.
39             *
40             * @code is the @domain specific error code.
41             *
42             * @format is used to generate the format string. It uses vsnprintf()
43             * internally so the format should match what you would use there.
44             *
45             * Parameters:
46             * @error: A #bson_error_t.
47             * @domain: The error domain.
48             * @code: The error code.
49             * @format: A printf style format string.
50             *
51             * Returns:
52             * None.
53             *
54             * Side effects:
55             * @error is initialized.
56             *
57             *--------------------------------------------------------------------------
58             */
59              
60             void
61 0           bson_set_error (bson_error_t *error, /* OUT */
62             uint32_t domain, /* IN */
63             uint32_t code, /* IN */
64             const char *format, /* IN */
65             ...) /* IN */
66             {
67             va_list args;
68              
69 0 0         if (error) {
70 0           error->domain = domain;
71 0           error->code = code;
72              
73 0           va_start (args, format);
74 0           bson_vsnprintf (error->message, sizeof error->message, format, args);
75 0           va_end (args);
76              
77 0           error->message[sizeof error->message - 1] = '\0';
78             }
79 0           }
80              
81              
82             /*
83             *--------------------------------------------------------------------------
84             *
85             * bson_strerror_r --
86             *
87             * This is a reentrant safe macro for strerror.
88             *
89             * The resulting string may be stored in @buf.
90             *
91             * Returns:
92             * A pointer to a static string or @buf.
93             *
94             * Side effects:
95             * None.
96             *
97             *--------------------------------------------------------------------------
98             */
99              
100             char *
101 0           bson_strerror_r (int err_code, /* IN */
102             char *buf, /* IN */
103             size_t buflen) /* IN */
104             {
105             static const char *unknown_msg = "Unknown error";
106 0           char *ret = NULL;
107              
108             #if defined(_WIN32)
109             if (strerror_s (buf, buflen, err_code) != 0) {
110             ret = buf;
111             }
112             #elif defined(__GNUC__) && defined(_GNU_SOURCE)
113 0           ret = strerror_r (err_code, buf, buflen);
114             #else /* XSI strerror_r */
115             if (strerror_r (err_code, buf, buflen) == 0) {
116             ret = buf;
117             }
118             #endif
119              
120 0 0         if (!ret) {
121 0           bson_strncpy (buf, unknown_msg, buflen);
122 0           ret = buf;
123             }
124              
125 0           return ret;
126             }
127