File: error.c

package info (click to toggle)
libsigrok 0.3.0-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, sid, stretch
  • size: 4,220 kB
  • ctags: 4,609
  • sloc: ansic: 40,159; sh: 11,626; makefile: 324
file content (137 lines) | stat: -rw-r--r-- 3,595 bytes parent folder | download
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
 * This file is part of the libsigrok project.
 *
 * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */

#include "libsigrok.h"

/**
 * @file
 *
 * Error handling in libsigrok.
 */

/**
 * @defgroup grp_error Error handling
 *
 * Error handling in libsigrok.
 *
 * libsigrok functions usually return @ref SR_OK upon success, or a negative
 * error code on failure.
 *
 * @{
 */

/**
 * Return a human-readable error string for the given libsigrok error code.
 *
 * @param error_code A libsigrok error code number, such as SR_ERR_MALLOC.
 *
 * @return A const string containing a short, human-readable (English)
 *         description of the error, such as "memory allocation error".
 *         The string must NOT be free'd by the caller!
 *
 * @see sr_strerror_name
 *
 * @since 0.2.0
 */
SR_API const char *sr_strerror(int error_code)
{
	/*
	 * Note: All defined SR_* error macros from libsigrok.h must have
	 * an entry in this function, as well as in sr_strerror_name().
	 */

	switch (error_code) {
	case SR_OK:
		return "no error";
	case SR_ERR:
		return "generic/unspecified error";
	case SR_ERR_MALLOC:
		return "memory allocation error";
	case SR_ERR_ARG:
		return "invalid argument";
	case SR_ERR_BUG:
		return "internal error";
	case SR_ERR_SAMPLERATE:
		return "invalid samplerate";
	case SR_ERR_NA:
		return "not applicable";
	case SR_ERR_DEV_CLOSED:
		return "device closed but should be open";
	case SR_ERR_TIMEOUT:
		return "timeout occurred";
	case SR_ERR_CHANNEL_GROUP:
		return "no channel group specified";
	default:
		return "unknown error";
	}
}

/**
 * Return the "name" string of the given libsigrok error code.
 *
 * For example, the "name" of the SR_ERR_MALLOC error code is "SR_ERR_MALLOC",
 * the name of the SR_OK code is "SR_OK", and so on.
 *
 * This function can be used for various purposes where the "name" string of
 * a libsigrok error code is useful.
 *
 * @param error_code A libsigrok error code number, such as SR_ERR_MALLOC.
 *
 * @return A const string containing the "name" of the error code as string.
 *         The string must NOT be free'd by the caller!
 *
 * @see sr_strerror
 *
 * @since 0.2.0
 */
SR_API const char *sr_strerror_name(int error_code)
{
	/*
	 * Note: All defined SR_* error macros from libsigrok.h must have
	 * an entry in this function, as well as in sr_strerror().
	 */

	switch (error_code) {
	case SR_OK:
		return "SR_OK";
	case SR_ERR:
		return "SR_ERR";
	case SR_ERR_MALLOC:
		return "SR_ERR_MALLOC";
	case SR_ERR_ARG:
		return "SR_ERR_ARG";
	case SR_ERR_BUG:
		return "SR_ERR_BUG";
	case SR_ERR_SAMPLERATE:
		return "SR_ERR_SAMPLERATE";
	case SR_ERR_NA:
		return "SR_ERR_NA";
	case SR_ERR_DEV_CLOSED:
		return "SR_ERR_DEV_CLOSED";
	case SR_ERR_TIMEOUT:
		return "SR_ERR_TIMEOUT";
	case SR_ERR_CHANNEL_GROUP:
		return "SR_ERR_CHANNEL_GROUP";
	default:
		return "unknown error code";
	}
}

/** @} */