Damjan Marion | 81bb6fc | 2022-01-16 22:47:55 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright(c) 2022 Cisco Systems, Inc. |
| 3 | */ |
| 4 | |
| 5 | #include <vppinfra/error.h> |
| 6 | #include <vnet/api_errno.h> |
| 7 | |
| 8 | static char *error_strings[] = { |
| 9 | #define _(a, b, c) [-(b)] = c, |
| 10 | foreach_vnet_error |
| 11 | #undef _ |
| 12 | }; |
| 13 | |
| 14 | clib_error_t * |
| 15 | vnet_error (vnet_error_t rv, char *fmt, ...) |
| 16 | { |
| 17 | clib_error_t *e, *err = 0; |
| 18 | va_list va; |
| 19 | vec_add2 (err, e, 1); |
| 20 | e->what = format (e->what, "%s", error_strings[-rv]); |
| 21 | |
| 22 | if (fmt) |
| 23 | { |
| 24 | vec_add1 (e->what, ' '); |
| 25 | vec_add1 (e->what, '('); |
| 26 | va_start (va, fmt); |
| 27 | e->what = va_format (e->what, fmt, &va); |
| 28 | vec_add1 (e->what, ')'); |
| 29 | va_end (va); |
| 30 | } |
| 31 | |
| 32 | e->code = rv; |
| 33 | return err; |
| 34 | } |
| 35 | |
| 36 | u8 * |
| 37 | format_vnet_api_errno (u8 *s, va_list *args) |
| 38 | { |
| 39 | vnet_api_error_t api_error = va_arg (*args, vnet_api_error_t); |
| 40 | #ifdef _ |
| 41 | #undef _ |
| 42 | #endif |
| 43 | #define _(a, b, c) \ |
| 44 | case b: \ |
| 45 | s = format (s, "%s", c); \ |
| 46 | break; |
| 47 | switch (api_error) |
| 48 | { |
| 49 | foreach_vnet_error default : s = format (s, "UNKNOWN"); |
| 50 | break; |
| 51 | } |
| 52 | return s; |
| 53 | #undef _ |
| 54 | } |