xref: /minix/external/bsd/kyua-testers/dist/error.h (revision 11be35a1)
1*11be35a1SLionel Sambuc // Copyright 2012 Google Inc.
2*11be35a1SLionel Sambuc // All rights reserved.
3*11be35a1SLionel Sambuc //
4*11be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
5*11be35a1SLionel Sambuc // modification, are permitted provided that the following conditions are
6*11be35a1SLionel Sambuc // met:
7*11be35a1SLionel Sambuc //
8*11be35a1SLionel Sambuc // * Redistributions of source code must retain the above copyright
9*11be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer.
10*11be35a1SLionel Sambuc // * Redistributions in binary form must reproduce the above copyright
11*11be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer in the
12*11be35a1SLionel Sambuc //   documentation and/or other materials provided with the distribution.
13*11be35a1SLionel Sambuc // * Neither the name of Google Inc. nor the names of its contributors
14*11be35a1SLionel Sambuc //   may be used to endorse or promote products derived from this software
15*11be35a1SLionel Sambuc //   without specific prior written permission.
16*11be35a1SLionel Sambuc //
17*11be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*11be35a1SLionel Sambuc // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*11be35a1SLionel Sambuc // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*11be35a1SLionel Sambuc // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*11be35a1SLionel Sambuc // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*11be35a1SLionel Sambuc // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*11be35a1SLionel Sambuc // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*11be35a1SLionel Sambuc // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*11be35a1SLionel Sambuc // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*11be35a1SLionel Sambuc // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*11be35a1SLionel Sambuc // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc 
29*11be35a1SLionel Sambuc /// \file error.h
30*11be35a1SLionel Sambuc /// High-level representation of error conditions.
31*11be35a1SLionel Sambuc ///
32*11be35a1SLionel Sambuc /// The error module provides a mechanism to represent error conditions in an
33*11be35a1SLionel Sambuc /// efficient manner.  In the case of a successful operation, an error is
34*11be35a1SLionel Sambuc /// internally represented as a NULL pointer and thus has no overhead.  In the
35*11be35a1SLionel Sambuc /// case of an actual error, the representation is more complex and costly than
36*11be35a1SLionel Sambuc /// a traditional libc error number, but is also more verbose.  Because errors
37*11be35a1SLionel Sambuc /// are not (or should not be!) in the critical path, this is not a concern.
38*11be35a1SLionel Sambuc 
39*11be35a1SLionel Sambuc #if !defined(KYUA_ERROR_H)
40*11be35a1SLionel Sambuc #define KYUA_ERROR_H
41*11be35a1SLionel Sambuc 
42*11be35a1SLionel Sambuc #include "error_fwd.h"
43*11be35a1SLionel Sambuc 
44*11be35a1SLionel Sambuc #include <stdbool.h>
45*11be35a1SLionel Sambuc #include <stddef.h>
46*11be35a1SLionel Sambuc #include <stdio.h>
47*11be35a1SLionel Sambuc 
48*11be35a1SLionel Sambuc #include "defs.h"
49*11be35a1SLionel Sambuc 
50*11be35a1SLionel Sambuc 
51*11be35a1SLionel Sambuc /// Type of the per-error formatting function.
52*11be35a1SLionel Sambuc ///
53*11be35a1SLionel Sambuc /// These functions take three arguments: the error to be formatted, a pointer
54*11be35a1SLionel Sambuc /// to the output buffer and the size of the output buffer.  The return value
55*11be35a1SLionel Sambuc /// indicates how many bytes were written to the output buffer, or a negative
56*11be35a1SLionel Sambuc /// value in case of an error.
57*11be35a1SLionel Sambuc typedef int (*kyua_error_format_callback)(
58*11be35a1SLionel Sambuc     struct kyua_error* const, char* const, const size_t);
59*11be35a1SLionel Sambuc 
60*11be35a1SLionel Sambuc 
61*11be35a1SLionel Sambuc /// Representation of an error.
62*11be35a1SLionel Sambuc struct kyua_error {
63*11be35a1SLionel Sambuc     /// Whether the error object has to be released or not.
64*11be35a1SLionel Sambuc     ///
65*11be35a1SLionel Sambuc     /// Sometimes (the oom error), a kyua_error_t object may point to a
66*11be35a1SLionel Sambuc     /// statically allocated error.  Such object cannot be freed.
67*11be35a1SLionel Sambuc     bool needs_free;
68*11be35a1SLionel Sambuc 
69*11be35a1SLionel Sambuc     /// Name of the type.
70*11be35a1SLionel Sambuc     const char* type_name;
71*11be35a1SLionel Sambuc 
72*11be35a1SLionel Sambuc     /// Opaquet error-specific data.
73*11be35a1SLionel Sambuc     void* data;
74*11be35a1SLionel Sambuc 
75*11be35a1SLionel Sambuc     /// Method to generate a textual representation of the error.
76*11be35a1SLionel Sambuc     kyua_error_format_callback format_callback;
77*11be35a1SLionel Sambuc };
78*11be35a1SLionel Sambuc 
79*11be35a1SLionel Sambuc 
80*11be35a1SLionel Sambuc kyua_error_t kyua_error_new(const char*, void*, size_t,
81*11be35a1SLionel Sambuc                             kyua_error_format_callback);
82*11be35a1SLionel Sambuc void kyua_error_free(kyua_error_t);
83*11be35a1SLionel Sambuc kyua_error_t kyua_error_subsume(kyua_error_t, kyua_error_t);
84*11be35a1SLionel Sambuc 
85*11be35a1SLionel Sambuc kyua_error_t kyua_error_ok(void);
86*11be35a1SLionel Sambuc bool kyua_error_is_set(const kyua_error_t);
87*11be35a1SLionel Sambuc bool kyua_error_is_type(const kyua_error_t, const char*);
88*11be35a1SLionel Sambuc 
89*11be35a1SLionel Sambuc const void* kyua_error_data(const kyua_error_t);
90*11be35a1SLionel Sambuc int kyua_error_format(const kyua_error_t, char* const, size_t);
91*11be35a1SLionel Sambuc void kyua_error_err(const int, const kyua_error_t, const char*, ...)
92*11be35a1SLionel Sambuc     KYUA_DEFS_NORETURN KYUA_DEFS_FORMAT_PRINTF(3, 4);
93*11be35a1SLionel Sambuc void kyua_error_fprintf(FILE*, const kyua_error_t, const char*, ...);
94*11be35a1SLionel Sambuc void kyua_error_warn(const kyua_error_t, const char*, ...);
95*11be35a1SLionel Sambuc 
96*11be35a1SLionel Sambuc 
97*11be35a1SLionel Sambuc extern const char* const kyua_generic_error_type;
98*11be35a1SLionel Sambuc kyua_error_t kyua_generic_error_new(const char* , ...)
99*11be35a1SLionel Sambuc     KYUA_DEFS_FORMAT_PRINTF(1, 2);
100*11be35a1SLionel Sambuc 
101*11be35a1SLionel Sambuc 
102*11be35a1SLionel Sambuc extern const char* const kyua_libc_error_type;
103*11be35a1SLionel Sambuc kyua_error_t kyua_libc_error_new(int, const char* , ...)
104*11be35a1SLionel Sambuc     KYUA_DEFS_FORMAT_PRINTF(2, 3);
105*11be35a1SLionel Sambuc int kyua_libc_error_errno(const kyua_error_t);
106*11be35a1SLionel Sambuc 
107*11be35a1SLionel Sambuc 
108*11be35a1SLionel Sambuc extern const char* const kyua_oom_error_type;
109*11be35a1SLionel Sambuc kyua_error_t kyua_oom_error_new(void);
110*11be35a1SLionel Sambuc 
111*11be35a1SLionel Sambuc 
112*11be35a1SLionel Sambuc extern const char* const kyua_usage_error_type;
113*11be35a1SLionel Sambuc kyua_error_t kyua_usage_error_new(const char* , ...)
114*11be35a1SLionel Sambuc     KYUA_DEFS_FORMAT_PRINTF(1, 2);
115*11be35a1SLionel Sambuc 
116*11be35a1SLionel Sambuc 
117*11be35a1SLionel Sambuc #endif  // !defined(KYUA_ERROR_H)
118