Skip to content

Commit

Permalink
consistent error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Jun 21, 2024
1 parent 9efc5d8 commit 5f5aff1
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 28 deletions.
1 change: 0 additions & 1 deletion R/ncurl.R
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,3 @@ as.promise.ncurlAio <- function(x) {
#' @exportS3Method promises::is.promising
#'
is.promising.ncurlAio <- function(x) TRUE

45 changes: 32 additions & 13 deletions src/aio.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,28 @@ typedef struct nano_handle_s {
nng_tls_config *cfg;
} nano_handle;

static SEXP mk_error_data(const int xc) {
static SEXP mk_error_aio(const int xc, SEXP env) {

const char *names[] = {xc < 0 ? "result" : "data", "value", ""};
SEXP out = PROTECT(Rf_mkNamed(VECSXP, names));
SEXP err = Rf_ScalarInteger(abs(xc));
SEXP err = PROTECT(Rf_ScalarInteger(xc));
Rf_classgets(err, nano_error);
SET_VECTOR_ELT(out, 0, err);
SET_VECTOR_ELT(out, 1, err);
Rf_defineVar(nano_ValueSymbol, err, env);
Rf_defineVar(nano_AioSymbol, R_NilValue, env);
UNPROTECT(1);
return out;
return err;

}

static SEXP mk_error_aio(const int xc, SEXP env) {
static SEXP mk_error_data(const int xc) {

SEXP err = PROTECT(Rf_ScalarInteger(xc));
SEXP env, err;
PROTECT(env = Rf_allocSExp(ENVSXP));
Rf_classgets(env, xc < 0 ? nano_sendAio : nano_recvAio);
PROTECT(err = Rf_ScalarInteger(abs(xc)));
Rf_classgets(err, nano_error);
Rf_defineVar(nano_ValueSymbol, err, env);
Rf_defineVar(nano_AioSymbol, R_NilValue, env);
UNPROTECT(1);
return err;
Rf_defineVar(xc < 0 ? nano_ResultSymbol : nano_DataSymbol, err, env);
UNPROTECT(2);
return env;

}

Expand All @@ -87,6 +88,24 @@ static SEXP mk_error_haio(const int xc, SEXP env) {

}

static SEXP mk_error_ncurl(const int xc) {

SEXP env, err;
PROTECT(env = Rf_allocSExp(ENVSXP));
NANO_CLASS2(env, "ncurlAio", "recvAio");
PROTECT(err = Rf_ScalarInteger(xc));
Rf_classgets(err, nano_error);
Rf_defineVar(nano_ResultSymbol, err, env);
Rf_defineVar(nano_StatusSymbol, err, env);
Rf_defineVar(nano_ProtocolSymbol, err, env);
Rf_defineVar(nano_HeadersSymbol, err, env);
Rf_defineVar(nano_ValueSymbol, err, env);
Rf_defineVar(nano_DataSymbol, err, env);
UNPROTECT(2);
return env;

}

// aio completion callbacks ----------------------------------------------------

static void pipe_cb_signal(nng_pipe p, nng_pipe_ev ev, void *arg) {
Expand Down Expand Up @@ -797,7 +816,7 @@ SEXP rnng_send_aio(SEXP con, SEXP data, SEXP mode, SEXP timeout, SEXP clo) {

SEXP env, fun;
PROTECT(env = Rf_allocSExp(ENVSXP));
Rf_classgets(env, Rf_mkString("sendAio"));
Rf_classgets(env, nano_sendAio);
Rf_defineVar(nano_AioSymbol, aio, env);

PROTECT(fun = R_mkClosure(R_NilValue, nano_aioFuncRes, clo));
Expand Down
3 changes: 3 additions & 0 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ SEXP nano_precious;
SEXP nano_recvAio;
SEXP nano_reqAio;
SEXP nano_refHook;
SEXP nano_sendAio;
SEXP nano_success;
SEXP nano_unresolved;

Expand Down Expand Up @@ -107,6 +108,7 @@ static void PreserveObjects(void) {
SET_STRING_ELT(nano_reqAio, 0, Rf_mkChar("mirai"));
SET_STRING_ELT(nano_reqAio, 1, Rf_mkChar("recvAio"));
R_PreserveObject(nano_refHook = Rf_list2(R_NilValue, R_NilValue));
R_PreserveObject(nano_sendAio = Rf_mkString("sendAio"));
R_PreserveObject(nano_success = Rf_ScalarInteger(0));
R_PreserveObject(nano_unresolved = Rf_shallow_duplicate(Rf_ScalarLogical(NA_LOGICAL)));
Rf_classgets(nano_unresolved, Rf_mkString("unresolvedValue"));
Expand All @@ -116,6 +118,7 @@ static void PreserveObjects(void) {
static void ReleaseObjects(void) {
R_ReleaseObject(nano_unresolved);
R_ReleaseObject(nano_success);
R_ReleaseObject(nano_sendAio);
R_ReleaseObject(nano_refHook);
R_ReleaseObject(nano_reqAio);
R_ReleaseObject(nano_recvAio);
Expand Down
2 changes: 1 addition & 1 deletion src/nanonext.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ void eln2dummy(void (*)(void *), void *, double, int);
SEXP nano_PreserveObject(SEXP);
void nano_ReleaseObject(SEXP);
SEXP mk_error(const int);
SEXP mk_error_ncurl(const int);
nano_buf nano_char_buf(const SEXP);
SEXP nano_decode(unsigned char *, const size_t, const int);
void nano_encode(nano_buf *, const SEXP);
Expand Down Expand Up @@ -309,6 +308,7 @@ extern SEXP nano_precious;
extern SEXP nano_recvAio;
extern SEXP nano_reqAio;
extern SEXP nano_refHook;
extern SEXP nano_sendAio;
extern SEXP nano_success;
extern SEXP nano_unresolved;

Expand Down
22 changes: 9 additions & 13 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,17 @@

// internals -------------------------------------------------------------------

SEXP mk_error_ncurl(const int xc) {
static SEXP mk_error_ncurl(const int xc) {

SEXP env;
PROTECT(env = Rf_allocSExp(ENVSXP));
NANO_CLASS2(env, "ncurlAio", "recvAio");
SEXP err = PROTECT(Rf_ScalarInteger(xc));
const char *names[] = {"status", "headers", "data", ""};
SEXP out = PROTECT(Rf_mkNamed(VECSXP, names));
SEXP err = Rf_ScalarInteger(xc);
Rf_classgets(err, nano_error);
Rf_defineVar(nano_ResultSymbol, err, env);
Rf_defineVar(nano_StatusSymbol, err, env);
Rf_defineVar(nano_ProtocolSymbol, err, env);
Rf_defineVar(nano_HeadersSymbol, err, env);
Rf_defineVar(nano_ValueSymbol, err, env);
Rf_defineVar(nano_DataSymbol, err, env);
UNPROTECT(2);
return env;
SET_VECTOR_ELT(out, 0, err);
SET_VECTOR_ELT(out, 1, err);
SET_VECTOR_ELT(out, 2, err);
UNPROTECT(1);
return out;

}

Expand Down

0 comments on commit 5f5aff1

Please sign in to comment.