summaryrefslogtreecommitdiffstats
path: root/src/vnet/error.c
blob: 7ae32962132ade00256678e9ece059998c106638 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/* SPDX-License-Identifier: Apache-2.0
 * Copyright(c) 2022 Cisco Systems, Inc.
 */

#include <vppinfra/error.h>
#include <vnet/api_errno.h>

static char *error_strings[] = {
#define _(a, b, c) [-(b)] = c,
  foreach_vnet_error
#undef _
};

clib_error_t *
vnet_error (vnet_error_t rv, char *fmt, ...)
{
  clib_error_t *e, *err = 0;
  va_list va;
  vec_add2 (err, e, 1);
  e->what = format (e->what, "%s", error_strings[-rv]);

  if (fmt)
    {
      vec_add1 (e->what, ' ');
      vec_add1 (e->what, '(');
      va_start (va, fmt);
      e->what = va_format (e->what, fmt, &va);
      vec_add1 (e->what, ')');
      va_end (va);
    }

  e->code = rv;
  return err;
}

u8 *
format_vnet_api_errno (u8 *s, va_list *args)
{
  vnet_api_error_t api_error = va_arg (*args, vnet_api_error_t);

  if (0 == api_error)
    return format (s, "Success");

#ifdef _
#undef _
#endif
#define _(a, b, c)                                                            \
  case b:                                                                     \
    s = format (s, "%s", c);                                                  \
    break;
  switch (api_error)
    {
      foreach_vnet_error default : s = format (s, "UNKNOWN");
      break;
    }
  return s;
#undef _
}