diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2018-03-15 12:13:04 +0100 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2018-03-15 12:13:04 +0100 |
commit | 8e7d1fb864520425006363fbfdff6b96a5015ed2 (patch) | |
tree | 75bd927c22936a51d2f7b3a8ab7bee7085926a79 /ffi | |
parent | dd257663217d6d2c098d5a57d8a3f8c5a830310f (diff) |
ffi: Improve error reporting when creating contexts.
- Usually, we report complex errors by attaching them to the ffi
context. However, when we create the contexts, there is no
context to attach the error to. Therefore, we add an explicit
error argument here.
Diffstat (limited to 'ffi')
-rw-r--r-- | ffi/examples/configure.c | 6 | ||||
-rw-r--r-- | ffi/examples/example.c | 6 | ||||
-rw-r--r-- | ffi/examples/keyserver.c | 6 | ||||
-rw-r--r-- | ffi/examples/reader.c | 6 | ||||
-rw-r--r-- | ffi/include/sequoia/core.h | 10 | ||||
-rw-r--r-- | ffi/src/core.rs | 38 |
6 files changed, 48 insertions, 24 deletions
diff --git a/ffi/examples/configure.c b/ffi/examples/configure.c index 731d7a4c..45c56ff1 100644 --- a/ffi/examples/configure.c +++ b/ffi/examples/configure.c @@ -9,15 +9,17 @@ int main (int argc, char **argv) { + sq_error_t err; sq_config_t cfg; sq_context_t ctx; sq_keyserver_t ks; cfg = sq_context_configure ("org.sequoia-pgp.example"); sq_config_network_policy (cfg, SQ_NETWORK_POLICY_OFFLINE); - ctx = sq_config_build (cfg); + ctx = sq_config_build (cfg, &err); if (ctx == NULL) - error (1, 0, "Initializing sequoia failed."); + error (1, 0, "Initializing sequoia failed: %s", + sq_error_string (err)); ks = sq_keyserver_sks_pool (ctx); if (ks == NULL) diff --git a/ffi/examples/example.c b/ffi/examples/example.c index ed4789ca..eac7d817 100644 --- a/ffi/examples/example.c +++ b/ffi/examples/example.c @@ -16,15 +16,17 @@ main (int argc, char **argv) struct stat st; int fd; char *b; + sq_error_t err; sq_context_t ctx; sq_tpk_t tpk; if (argc != 2) error (1, 0, "Usage: %s <file>", argv[0]); - ctx = sq_context_new("org.sequoia-pgp.example"); + ctx = sq_context_new("org.sequoia-pgp.example", &err); if (ctx == NULL) - error (1, 0, "Initializing sequoia failed."); + error (1, 0, "Initializing sequoia failed: %s", + sq_error_string (err)); if (stat (argv[1], &st)) error (1, errno, "%s", argv[1]); diff --git a/ffi/examples/keyserver.c b/ffi/examples/keyserver.c index 343ee163..fbc6fe9a 100644 --- a/ffi/examples/keyserver.c +++ b/ffi/examples/keyserver.c @@ -8,14 +8,16 @@ int main (int argc, char **argv) { + sq_error_t err; sq_context_t ctx; sq_keyid_t id; sq_keyserver_t ks; sq_tpk_t tpk; - ctx = sq_context_new ("org.sequoia-pgp.example"); + ctx = sq_context_new ("org.sequoia-pgp.example", &err); if (ctx == NULL) - error (1, 0, "Initializing sequoia failed."); + error (1, 0, "Initializing sequoia failed: %s", + sq_error_string (err)); ks = sq_keyserver_sks_pool (ctx); if (ks == NULL) diff --git a/ffi/examples/reader.c b/ffi/examples/reader.c index 439d6510..1587979b 100644 --- a/ffi/examples/reader.c +++ b/ffi/examples/reader.c @@ -16,6 +16,7 @@ main (int argc, char **argv) struct stat st; int fd; uint8_t *b; + sq_error_t err; sq_context_t ctx; sq_reader_t reader; sq_tpk_t tpk; @@ -23,9 +24,10 @@ main (int argc, char **argv) if (argc != 2) error (1, 0, "Usage: %s <file>", argv[0]); - ctx = sq_context_new("org.sequoia-pgp.example"); + ctx = sq_context_new("org.sequoia-pgp.example", &err); if (ctx == NULL) - error (1, 0, "Initializing sequoia failed."); + error (1, 0, "Initializing sequoia failed: %s", + sq_error_string (err)); if (stat (argv[1], &st)) error (1, errno, "%s", argv[1]); diff --git a/ffi/include/sequoia/core.h b/ffi/include/sequoia/core.h index da6732aa..d693bb0c 100644 --- a/ffi/include/sequoia/core.h +++ b/ffi/include/sequoia/core.h @@ -134,9 +134,10 @@ typedef enum sq_ipc_policy { /// suggested to use a reversed fully qualified domain name that is /// associated with your application. `domain` must not be `NULL`. /// -/// Returns `NULL` on errors. +/// Returns `NULL` on errors. If `errp` is not `NULL`, the error is +/// stored there. /*/ -sq_context_t sq_context_new(const char *domain); +sq_context_t sq_context_new(const char *domain, sq_error_t *errp); /*/ /// Frees a context. @@ -192,9 +193,10 @@ uint8_t sq_context_ephemeral(const sq_context_t ctx); /*/ /// Finalizes the configuration and return a `Context`. /// -/// Consumes `cfg`. Returns `NULL` on errors. +/// Consumes `cfg`. Returns `NULL` on errors. Returns `NULL` on +/// errors. If `errp` is not `NULL`, the error is stored there. /*/ -sq_context_t sq_config_build(sq_config_t cfg); +sq_context_t sq_config_build(sq_config_t cfg, sq_error_t *errp); /*/ /// Sets the directory containing shared state. diff --git a/ffi/src/core.rs b/ffi/src/core.rs index bd2638ff..7d7de46d 100644 --- a/ffi/src/core.rs +++ b/ffi/src/core.rs @@ -79,19 +79,26 @@ pub extern "system" fn sq_string_free(s: *mut c_char) { /// suggested to use a reversed fully qualified domain name that is /// associated with your application. `domain` must not be `NULL`. /// -/// Returns `NULL` on errors. +/// Returns `NULL` on errors. If `errp` is not `NULL`, the error is +/// stored there. #[no_mangle] -pub extern "system" fn sq_context_new(domain: *const c_char) +pub extern "system" fn sq_context_new(domain: *const c_char, + errp: Option<&mut *mut failure::Error>) -> *mut Context { assert!(! domain.is_null()); let domain = unsafe { CStr::from_ptr(domain).to_string_lossy() }; - if let Ok(context) = core::Context::new(&domain) { - Box::into_raw(Box::new(Context::new(context))) - } else { - ptr::null_mut() + match core::Context::new(&domain) { + Ok(context) => + box_raw!(Context::new(context)), + Err(e) => { + if let Some(errp) = errp { + *errp = box_raw!(e); + } + ptr::null_mut() + }, } } @@ -171,17 +178,24 @@ pub extern "system" fn sq_context_ephemeral(ctx: Option<&Context>) -> uint8_t { /// Finalizes the configuration and return a `Context`. /// -/// Consumes `cfg`. Returns `NULL` on errors. +/// Consumes `cfg`. Returns `NULL` on errors. Returns `NULL` on +/// errors. If `errp` is not `NULL`, the error is stored there. #[no_mangle] -pub extern "system" fn sq_config_build(cfg: Option<&mut Config>) +pub extern "system" fn sq_config_build(cfg: Option<&mut Config>, + errp: Option<&mut *mut failure::Error>) -> *mut Context { assert!(cfg.is_some()); let cfg = unsafe { Box::from_raw(cfg.unwrap()) }; - if let Ok(context) = cfg.build() { - Box::into_raw(Box::new(Context::new(context))) - } else { - ptr::null_mut() + match cfg.build() { + Ok(context) => + box_raw!(Context::new(context)), + Err(e) => { + if let Some(errp) = errp { + *errp = box_raw!(e); + } + ptr::null_mut() + }, } } |