Skip to content

Commit 2ddaecd

Browse files
committed
api: extend usage of nng_err
This replaces the int, and we will expand this further, as this makes it clear that the int is actually an error code and helps in debuggers that can provide symbolic values.
1 parent d88484c commit 2ddaecd

File tree

10 files changed

+168
-169
lines changed

10 files changed

+168
-169
lines changed

Diff for: docs/ref/api/aio.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ is safe to call from _aio_'s own callback.
8181
## Cancellation
8282

8383
```c
84-
void nng_aio_abort(nng_aio *aio, int err);
84+
void nng_aio_abort(nng_aio *aio, nng_err err);
8585
void nng_aio_cancel(nng_aio *aio);
8686
void nng_aio_stop(nng_aio *aio);
8787
```
@@ -169,7 +169,7 @@ This is the same test used internally by [`nng_aio_wait`].
169169
## Result of Operation
170170

171171
```c
172-
int nng_aio_result(nng_aio *aio);
172+
nng_err nng_aio_result(nng_aio *aio);
173173
size_t nng_aio_count(nng_aio *aio);
174174
```
175175

Diff for: docs/ref/api/errors.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
11
# Errors
22

3+
```c
4+
typedef enum ... nng_err;
5+
```
6+
37
Many _NNG_ functions can fail for a variety of reasons.
48
These functions tend to return either zero on success,
59
or a non-zero error code to indicate failure.
610
{{footnote: This convention goes back to UNIX system calls,
711
which behave the same way, but _NNG_ does not use a separate
812
_errno_ variable.}}
913

10-
All these error codes are `int`.
14+
All these error codes are `nng_err`.
15+
16+
> [!NOTE]
17+
> Many APIs are still using `int`, but the `nng_err` enumeration can be used
18+
> instead, and will help with debugging.
1119
1220
Not every possible error code is defined here, as sometimes
1321
an underlying system or library error code is "wrapped".
1422

1523
## Human Readable Error Message
1624

1725
```c
18-
const char *nng_strerror(int err);
26+
const char *nng_strerror(nng_err err);
1927
```
2028
2129
The {{i:`nng_strerror`}} returns the human-readable description of the

Diff for: docs/ref/api/http.md

+8-17
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ set when issuing the transaction.
5656
### HTTP URI
5757
5858
```c
59-
int nng_http_set_uri(nng_http *conn, const char *uri, const char *query);
59+
nng_err nng_http_set_uri(nng_http *conn, const char *uri, const char *query);
6060
const char *nng_http_get_uri(nng_http *conn);
6161
```
6262

@@ -76,7 +76,7 @@ will have any query concentated, for example "/api/get_user.cgi?name=garrett".
7676
### HTTP Version
7777

7878
```c
79-
int nng_http_set_version(nng_http *conn, const char *version);
79+
nng_err nng_http_set_version(nng_http *conn, const char *version);
8080
const char *nng_http_get_version(nng_http *conn);
8181
```
8282
@@ -210,8 +210,8 @@ The scan can be rest by setting _next_ to `NULL`.
210210
### Modifying Headers
211211
212212
```c
213-
int nng_http_add_header(nng_http *conn, const char *key, const char *val);
214-
int nng_http_set_header(nng_http *conn, const char *key, const char *val);
213+
nng_err nng_http_add_header(nng_http *conn, const char *key, const char *val);
214+
nng_err nng_http_set_header(nng_http *conn, const char *key, const char *val);
215215
void nng_http_del_header(nng_http *conn, const char *key);
216216
```
217217

@@ -323,7 +323,7 @@ They can be used to transfer request or response body data as well.
323323
### Hijacking Connections
324324
325325
```c
326-
void nng_http_hijack(nng_http_conn *conn);
326+
nng_err nng_http_hijack(nng_http *conn);
327327
```
328328

329329
TODO: This API will change to convert the conn into a stream object.
@@ -387,8 +387,8 @@ of its resources.
387387
### Client TLS
388388

389389
```c
390-
int nng_http_client_get_tls(nng_http_client *client, nng_tls_config **tlsp);
391-
int nng_http_client_set_tls(nng_http_client *client, nng_tls_config *tls);
390+
nng_err nng_http_client_get_tls(nng_http_client *client, nng_tls_config **tlsp);
391+
nng_err nng_http_client_set_tls(nng_http_client *client, nng_tls_config *tls);
392392
```
393393
394394
The {{i:`nng_http_client_get_tls`}} and {{i:`nng_http_client_set_tls`}} functions are used to
@@ -456,15 +456,6 @@ if ((rv = nng_aio_result(aio)) != 0) {
456456
457457
### Preparing a Transaction
458458
459-
```c
460-
int nng_http_set_version(nng_http *conn, const char *version);
461-
int nng_http_set_uri(nng_http *conn, const char *uri);
462-
```
463-
464-
The {{i:`nng_http_set_uri`}} function provides a URI for the transaction. This will be used to
465-
set the URI for the request. The URI typically appears like a path, starting with "/", although
466-
it may also contain a query string.
467-
468459
### Request Body
469460
470461
### Sending the Request
@@ -510,7 +501,7 @@ It does _not_ transfer any response body. To do that, use [`nng_http_read_all`]
510501
### Submitting the Transaction
511502
512503
```c
513-
int nng_http_transact(nng_http *conn, nng_aio *aio);
504+
void nng_http_transact(nng_http *conn, nng_aio *aio);
514505
```
515506

516507
The HTTP request is issued, and the response processed, asynchronously by the {{i:`nng_http_transact`}} function.

Diff for: include/nng/http.h

+25-25
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ NNG_DECL void nng_http_reset(nng_http *);
147147
// include an optional query string, either inline in the URI to start,
148148
// or as a separate argument. If the query string already exists
149149
// and one is also supplied here, it will be appended (separated with &).
150-
NNG_DECL int nng_http_set_uri(nng_http *, const char *, const char *);
150+
NNG_DECL nng_err nng_http_set_uri(nng_http *, const char *, const char *);
151151

152152
// nng_http_get_uri returns the URI. It will be NULL if not set.
153153
NNG_DECL const char *nng_http_get_uri(nng_http *);
@@ -167,7 +167,7 @@ NNG_DECL void nng_http_set_status(nng_http *, uint16_t, const char *);
167167
// nng_http_set_version is used to change the version of a request.
168168
// Normally the version is "HTTP/1.1". Note that the framework does
169169
// not support HTTP/2 at all. Null sets the default ("HTTP/1.1").
170-
NNG_DECL int nng_http_set_version(nng_http *, const char *);
170+
NNG_DECL nng_err nng_http_set_version(nng_http *, const char *);
171171

172172
// nng_http_get_version is used to get the version of a request.
173173
NNG_DECL const char *nng_http_get_version(nng_http *);
@@ -184,8 +184,8 @@ NNG_DECL const char *nng_http_get_method(nng_http *);
184184
// a header to either the request or response. Clients modify the request
185185
// headers, and servers (and callbacks on the server) modify response headers.
186186
// These can return NNG_ENOMEM, NNG_MSGSIZE, etc.
187-
NNG_DECL int nng_http_set_header(nng_http *, const char *, const char *);
188-
NNG_DECL int nng_http_add_header(nng_http *, const char *, const char *);
187+
NNG_DECL nng_err nng_http_set_header(nng_http *, const char *, const char *);
188+
NNG_DECL nng_err nng_http_add_header(nng_http *, const char *, const char *);
189189

190190
// nng_http_del_header removes all of the headers for the given header.
191191
// For clients this is done on the request headers, for servers its the
@@ -205,7 +205,7 @@ NNG_DECL void nng_http_set_body(nng_http *, void *, size_t);
205205

206206
// nng_http_copy_body sets the body to send out in the next exchange, but
207207
// makes a local copy. It can fail due to NNG_ENOMEM.
208-
NNG_DECL int nng_http_copy_body(nng_http *, const void *, size_t);
208+
NNG_DECL nng_err nng_http_copy_body(nng_http *, const void *, size_t);
209209

210210
// nng_http_handler is a handler used on the server side to handle HTTP
211211
// requests coming into a specific URL.
@@ -230,7 +230,7 @@ typedef struct nng_http_handler nng_http_handler;
230230
// completion by nng_aio_finish. The second argument to this function is the
231231
// handler data that was optionally set by nng_handler_set_data.
232232
typedef void (*nng_http_handler_func)(nng_http *, void *, nng_aio *);
233-
NNG_DECL int nng_http_handler_alloc(
233+
NNG_DECL nng_err nng_http_handler_alloc(
234234
nng_http_handler **, const char *, nng_http_handler_func);
235235

236236
// nng_http_handler_free frees the handler. This actually just drops a
@@ -241,19 +241,19 @@ NNG_DECL void nng_http_handler_free(nng_http_handler *);
241241
// nng_http_handler_alloc_file creates a "file" based handler, that
242242
// serves up static content from the given file path. The content-type
243243
// supplied is determined from the file name using a simple built-in map.
244-
NNG_DECL int nng_http_handler_alloc_file(
244+
NNG_DECL nng_err nng_http_handler_alloc_file(
245245
nng_http_handler **, const char *, const char *);
246246

247247
// nng_http_handler_alloc_static creates a static-content handler.
248248
// The last argument is the content-type, which may be NULL (in which case
249249
// "application/octet-stream" is assumed.)
250-
NNG_DECL int nng_http_handler_alloc_static(
250+
NNG_DECL nng_err nng_http_handler_alloc_static(
251251
nng_http_handler **, const char *, const void *, size_t, const char *);
252252

253253
// nng_http_handler_alloc_redirect creates an HTTP redirect handler.
254254
// The status is given, along with the new URL. If the status is 0,
255255
// then 301 will be used instead.
256-
NNG_DECL int nng_http_handler_alloc_redirect(
256+
NNG_DECL nng_err nng_http_handler_alloc_redirect(
257257
nng_http_handler **, const char *, uint16_t, const char *);
258258

259259
// nng_http_handler_alloc_file creates a "directory" based handler, that
@@ -262,7 +262,7 @@ NNG_DECL int nng_http_handler_alloc_redirect(
262262
// directory content, otherwise a suitable error page is returned (the server
263263
// does not generate index pages automatically.) The content-type for
264264
// files is determined from the file name using a simple built-in map.
265-
NNG_DECL int nng_http_handler_alloc_directory(
265+
NNG_DECL nng_err nng_http_handler_alloc_directory(
266266
nng_http_handler **, const char *, const char *);
267267

268268
// nng_http_handler_set_method sets the method that the handler will be
@@ -310,7 +310,7 @@ typedef struct nng_http_server nng_http_server;
310310
// from the URL. If a server already exists, then a hold is placed on it, and
311311
// that instance is returned. If no such server exists, then a new instance
312312
// is created.
313-
NNG_DECL int nng_http_server_hold(nng_http_server **, const nng_url *);
313+
NNG_DECL nng_err nng_http_server_hold(nng_http_server **, const nng_url *);
314314

315315
// nng_http_server_release releases the hold on the server. If this is the
316316
// last instance of the server, then it is shutdown and resources are freed.
@@ -319,7 +319,7 @@ NNG_DECL void nng_http_server_release(nng_http_server *);
319319
// nng_http_server_start starts the server handling HTTP. Once this is
320320
// called, it will not be possible to change certain parameters (such as
321321
// any TLS configuration).
322-
NNG_DECL int nng_http_server_start(nng_http_server *);
322+
NNG_DECL nng_err nng_http_server_start(nng_http_server *);
323323

324324
// nng_http_server_stop stops the server. No new client connections are
325325
// accepted after this returns. Once a server is stopped fully, the
@@ -331,14 +331,14 @@ NNG_DECL void nng_http_server_stop(nng_http_server *);
331331
// This function will return NNG_EADDRINUSE if a conflicting handler
332332
// is already registered (i.e. a handler with the same value for Host,
333333
// Method, and URL.)
334-
NNG_DECL int nng_http_server_add_handler(
334+
NNG_DECL nng_err nng_http_server_add_handler(
335335
nng_http_server *, nng_http_handler *);
336336

337337
// nni_http_del_handler removes the given handler. The caller is
338338
// responsible for finalizing it afterwards. If the handler was not found
339339
// (not registered), NNG_ENOENT is returned. In this case it is unsafe
340340
// to make assumptions about the validity of the handler.
341-
NNG_DECL int nng_http_server_del_handler(
341+
NNG_DECL nng_err nng_http_server_del_handler(
342342
nng_http_server *, nng_http_handler *);
343343

344344
// nng_http_server_set_tls adds a TLS configuration to the server,
@@ -347,36 +347,36 @@ NNG_DECL int nng_http_server_del_handler(
347347
// server client, so the caller must have configured it reasonably.
348348
// This API is not recommended unless the caller needs complete control
349349
// over the TLS configuration.
350-
NNG_DECL int nng_http_server_set_tls(nng_http_server *, nng_tls_config *);
350+
NNG_DECL nng_err nng_http_server_set_tls(nng_http_server *, nng_tls_config *);
351351

352352
// nng_http_server_get_tls obtains the TLS configuration if one is present,
353353
// or returns NNG_EINVAL. The TLS configuration is invalidated if the
354354
// nng_http_server_set_tls function is called, so be careful.
355-
NNG_DECL int nng_http_server_get_tls(nng_http_server *, nng_tls_config **);
355+
NNG_DECL nng_err nng_http_server_get_tls(nng_http_server *, nng_tls_config **);
356356

357357
// nng_http_server_get_addr obtains the address with which the server was
358358
// initialized or returns NNG_EINVAL. Useful for instance when the port has
359359
// been automatically assigned.
360-
NNG_DECL int nng_http_server_get_addr(nng_http_server *, nng_sockaddr *);
360+
NNG_DECL nng_err nng_http_server_get_addr(nng_http_server *, nng_sockaddr *);
361361

362362
// nng_http_server_set_error_page sets a custom error page (HTML) content
363363
// to be sent for the given error code. This is used when the error is
364364
// generated internally by the framework.
365-
NNG_DECL int nng_http_server_set_error_page(
365+
NNG_DECL nng_err nng_http_server_set_error_page(
366366
nng_http_server *, uint16_t, const char *);
367367

368368
// nng_http_server_set_error_file works like nng_http_server_error_page,
369369
// except that the content is loaded from the named file path. The contents
370370
// are loaded at the time this function is called, so this function should be
371371
// called anytime the contents of the named file have changed.
372-
NNG_DECL int nng_http_server_set_error_file(
372+
NNG_DECL nng_err nng_http_server_set_error_file(
373373
nng_http_server *, uint16_t, const char *);
374374

375-
// nng_http_server_res_error takes replaces the body of the response with
375+
// nng_http_server_error takes replaces the body of the response with
376376
// a custom error page previously set for the server, using the status
377377
// of the response. The response must have the status set first using
378378
// nng_http_res_set_status.
379-
NNG_DECL int nng_http_server_error(nng_http_server *, nng_http *);
379+
NNG_DECL nng_err nng_http_server_error(nng_http_server *, nng_http *);
380380

381381
// nng_http_hijack is intended to be called by a handler that wishes to
382382
// take over the processing of the HTTP session -- usually to change protocols
@@ -389,7 +389,7 @@ NNG_DECL int nng_http_server_error(nng_http_server *, nng_http *);
389389
// of the request structure. (Some hijackers may keep the request for
390390
// further processing.)
391391

392-
NNG_DECL int nng_http_hijack(nng_http *);
392+
NNG_DECL nng_err nng_http_hijack(nng_http *);
393393

394394
// nng_http_client represents a "client" object. Clients can be used
395395
// to create HTTP connections. At present, connections are not cached
@@ -398,7 +398,7 @@ typedef struct nng_http_client nng_http_client;
398398

399399
// nng_http_client_alloc allocates a client object, associated with
400400
// the given URL.
401-
NNG_DECL int nng_http_client_alloc(nng_http_client **, const nng_url *);
401+
NNG_DECL nng_err nng_http_client_alloc(nng_http_client **, const nng_url *);
402402

403403
// nng_http_client_free frees the client. Connections created by the
404404
// the client are not necessarily closed.
@@ -408,12 +408,12 @@ NNG_DECL void nng_http_client_free(nng_http_client *);
408408
// the entire TLS configuration on the client, so the caller must have
409409
// configured it reasonably. This API is not recommended unless the
410410
// caller needs complete control over the TLS configuration.
411-
NNG_DECL int nng_http_client_set_tls(nng_http_client *, nng_tls_config *);
411+
NNG_DECL nng_err nng_http_client_set_tls(nng_http_client *, nng_tls_config *);
412412

413413
// nng_http_client_get_tls obtains the TLS configuration if one is present,
414414
// or returns NNG_EINVAL. The supplied TLS configuration object may
415415
// be invalidated by any future calls to nni_http_client_set_tls.
416-
NNG_DECL int nng_http_client_get_tls(nng_http_client *, nng_tls_config **);
416+
NNG_DECL nng_err nng_http_client_get_tls(nng_http_client *, nng_tls_config **);
417417

418418
// nng_http_client_connect establishes a new connection with the server
419419
// named in the URL used when the client was created. Once the connection

0 commit comments

Comments
 (0)